dbTalk Databases Forums  

Help on query

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss Help on query in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
rs
 
Posts: n/a

Default Help on query - 09-03-2010 , 01:55 PM






Now I have this set

Col1 Col2
B 0
B 0.5
A 1
A 1.5
B 2
A 3
A 4
B 5
B 6
C 7
A 8

How do I get the last set of "B", with the smallest Col2, in this case
5.

Reply With Quote
  #2  
Old   
The Boss
 
Posts: n/a

Default Re: Help on query - 09-03-2010 , 03:13 PM






rs wrote:
Quote:
Now I have this set

Col1 Col2
B 0
B 0.5
A 1
A 1.5
B 2
A 3
A 4
B 5
B 6
C 7
A 8

How do I get the last set of "B", with the smallest Col2, in this case
5.
What do you mean with 'the last set of "B"'???
Remember in Relational Theory a 'relation' is an /unordered/ set of
'tuples', which translated into traditional database terminology corresponds
with: A 'table' is an /unordered/ set of 'rows'.

--
Jeroen

Reply With Quote
  #3  
Old   
rs
 
Posts: n/a

Default Re: Help on query - 09-03-2010 , 03:22 PM



the set will use ORDER BY Col2 as showed above

Reply With Quote
  #4  
Old   
The Boss
 
Posts: n/a

Default Re: Help on query - 09-03-2010 , 03:53 PM



rs wrote:
Quote:
the set will use ORDER BY Col2 as showed above
Still not clear what you mean by 'last set of "B" with the smallest Col2'.
The "B" row with the smallest Col2 would be the first one, having Col2 = 0.
How do you get at row {"B",5} ?

--
Jeroen

Reply With Quote
  #5  
Old   
Tonkuma
 
Posts: n/a

Default Re: Help on query - 09-03-2010 , 04:02 PM



Even if you showed the data with ORDER BY some columns, many of
DBMS(including DB2) don't assume the order.
Order of rows are supposed only in queries by specifying ORDER BY
clause explicitly.

Anyhow, if supposing order of col2 and rows with contnued same col1 as
one set,
then this may be an solution:

------------------------------ Commands Entered
------------------------------
WITH
test_data(Col1 , Col2) AS (
VALUES
('B' , 0 )
, ('B' , 0.5)
, ('A' , 1 )
, ('A' , 1.5)
, ('B' , 2 )
, ('A' , 3 )
, ('A' , 4 )
, ('B' , 5 )
, ('B' , 6 )
, ('C' , 7 )
, ('A' , 8 )
)
SELECT col1 , col2
FROM (SELECT col1 , col2
, MAX(col1)
OVER(ORDER BY col2
ROWS BETWEEN 1 PRECEDING
AND 1 PRECEDING)
AS pre_col1
FROM test_data
) s
WHERE col1 = 'B'
AND
( pre_col1 <> 'B'
OR pre_col1 IS NULL
)
ORDER BY
col2 DESC
FETCH FIRST 1 ROW ONLY
;
------------------------------------------------------------------------------

COL1 COL2
---- --------------
B 5.0

1 record(s) selected.


or if you are using DB2 9.5 or later for LUW:

------------------------------ Commands Entered
------------------------------
WITH
test_data(Col1 , Col2) AS (
VALUES
('B' , 0 )
, ('B' , 0.5)
, ('A' , 1 )
, ('A' , 1.5)
, ('B' , 2 )
, ('A' , 3 )
, ('A' , 4 )
, ('B' , 5 )
, ('B' , 6 )
, ('C' , 7 )
, ('A' , 8 )
)
SELECT col1 , col2
FROM (SELECT col1 , col2
, LAG(col1 , 1 , ' ')
OVER(ORDER BY col2)
AS pre_col1
FROM test_data
) s
WHERE col1 = 'B'
AND pre_col1 <> 'B'
ORDER BY
col2 DESC
FETCH FIRST 1 ROW ONLY
;
------------------------------------------------------------------------------

COL1 COL2
---- --------------
B 5.0

1 record(s) selected.

Reply With Quote
  #6  
Old   
Tonkuma
 
Posts: n/a

Default Re: Help on query - 09-03-2010 , 04:06 PM



If you need only the value of col2:

SELECT MAX(col2) AS col2
FROM (SELECT col1 , col2
, LAG(col1 , 1 , ' ')
OVER(ORDER BY col2)
AS pre_col1
FROM test_data
) s
WHERE col1 = 'B'
AND pre_col1 <> 'B'
;
------------------------------------------------------------------------------

COL2
--------------
5.0

1 record(s) selected.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.