I could swear we just lately had a discussion on this but I can't find it.
I have a (simplified) table: subj_id, date_checked, status and I want to
retrieve the latest status for each subj_id within a given range of
dates. The primary key is subj_id, date_checked.
select * from t as t2
where t2.subj_id=:subject
and date_checked=(
select max(date_checked)
from t
where subj_id=t2.subj_id
and date_ckecked between :start_date and :end_date)
would do the trick for any given :subject, but I want to wrap it in a
query of the form:
select * from t
where subj_id in (:list)
I'm missing something obvious, but it is eluding me. 5 1759
Bob Stearns wrote: I could swear we just lately had a discussion on this but I can't find it.
I have a (simplified) table: subj_id, date_checked, status and I want to retrieve the latest status for each subj_id within a given range of dates. The primary key is subj_id, date_checked.
select * from t as t2 where t2.subj_id=:subject and date_checked=( select max(date_checked) from t where subj_id=t2.subj_id and date_ckecked between :start_date and :end_date)
would do the trick for any given :subject, but I want to wrap it in a query of the form:
select * from t where subj_id in (:list)
I'm missing something obvious, but it is eluding me.
You are messing ROW_NUMBER() OVER(ORDER BY date_checked DESC PARTITION
BY subj_id). Then you can filter out all but the first.
Depending on your indexing you may still want to do a self join to pull
up extraneous columns after filtering.
Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Serge Rielau wrote: Bob Stearns wrote:
I could swear we just lately had a discussion on this but I can't find it.
I have a (simplified) table: subj_id, date_checked, status and I want to retrieve the latest status for each subj_id within a given range of dates. The primary key is subj_id, date_checked.
select * from t as t2 where t2.subj_id=:subject and date_checked=( select max(date_checked) from t where subj_id=t2.subj_id and date_ckecked between :start_date and :end_date)
would do the trick for any given :subject, but I want to wrap it in a query of the form:
select * from t where subj_id in (:list)
I'm missing something obvious, but it is eluding me.
You are messing ROW_NUMBER() OVER(ORDER BY date_checked DESC PARTITION BY subj_id). Then you can filter out all but the first. Depending on your indexing you may still want to do a self join to pull up extraneous columns after filtering.
Cheers Serge
That appears to have the right functionality. The OVER phrases must be
in the order PARTITION BY then ORDER BY according to the SQL Reference
Vol. 1.
I have a complex ORDER BY that is failing with:
SQL0104N An unexpected token "DESC" was found following "END)".
Expected tokens may include: "". SQLSTATE=42601
It is:
ROW_NUMBER() OVER(
PARTITION BY (t1.bhid)
ORDER BY ((CASE t1.bred_type
when 'I' then t1.BRED_DATE + 275 days
else t1.BRED_DATE + 283 days
END) DESC)
) AS rowx
It seem legal according to the syntax diagrams (I have the same CASE
defining a column earlier). Any ideas?
Remove parentheses surrounding (CASE ... DESC):
ROW_NUMBER() OVER(
PARTITION BY (t1.bhid)
ORDER BY (CASE t1.bred_type
when 'I' then t1.BRED_DATE + 275 days
else t1.BRED_DATE + 283 days
END) DESC
) AS rowx
Note: Though they are not syntax error, another two parentheses are not
required.
(t1.bhid)
(CASE .... END)
Tonkuma wrote: Remove parentheses surrounding (CASE ... DESC): ROW_NUMBER() OVER( PARTITION BY (t1.bhid) ORDER BY (CASE t1.bred_type when 'I' then t1.BRED_DATE + 275 days
else t1.BRED_DATE + 283 days END) DESC ) AS rowx
Note: Though they are not syntax error, another two parentheses are not required. (t1.bhid) (CASE .... END)
Exactly the same error:
An unexpected token "DESC" was found following "END". Expected tokens
may include: "". SQLSTATE=42601
It worked on my DB2 UDB V8.2
------------------------------ Commands Entered
------------------------------
connect to SAMPLE user db2admin using ********;
----------------------------------------------------------------------
Database Connection Information
Database server = DB2/NT 8.2.0
SQL authorization ID = DB2ADMIN
Local database alias = SAMPLE
------------------------- Commands Entered --------------------------
SELECT id, bhid, bred_type, BRED_DATE,
ROW_NUMBER() OVER(
PARTITION BY (t1.bhid)
ORDER BY (CASE t1.bred_type
when 'I' then t1.BRED_DATE + 275 days
else t1.BRED_DATE + 283 days
END) DESC
) AS rowx
FROM (VALUES (1, 1, 'I', CURRENT DATE)
,(2, 1, 'A', CURRENT DATE)
,(3, 1, 'I', CURRENT DATE)
) t1 (id, bhid, bred_type, BRED_DATE);
--------------------------------------------------------------------
ID BHID BRED_TYPE BRED_DATE ROWX
----------- ----------- --------- ---------- --------------------
2 1 A 2005-10-23 1
1 1 I 2005-10-23 2
3 1 I 2005-10-23 3
3 record(s) selected. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: karthik |
last post by:
Hi,
I having problems returning only the first row of data for each GROUP
for the query below. Can someone please help? Thanks.
SELECT bid, xact_date, xact_time
FROM badge_history bh...
|
by: Dwight |
last post by:
Hi all,
How do you capitalize the first letter of each word in a field. I can format
the field to UPPER case the last name of a person, but I can't UPPER case
just the first letter of each name....
|
by: Henrik Larsson |
last post by:
Hi,
I need help with selecting the following rows from a table looking like this:
ID IP
Query 1 -> 1 2.2.2.2
(ie first IP 1 1.1.1.1 <- Query 2
for each...
|
by: captain_2010 |
last post by:
hi,
>From below table want only the first value of each item id.
and how to get it, tried to use top n function in select statement
it takes only the first record.
itemid qty
1 10
1...
|
by: Marcus |
last post by:
Say a number of people have 2 or 3 addresses each in a query result.
How do I select only the first address displayed for each person.
For example:
John Taylor 33 Dundas Ave.
John Taylor 55...
|
by: kieran |
last post by:
Hi,
I have a string Array called Groups and this has x number of groups.
Each row of the Array is divided up by ';' and has the name of the
group in the second position (i.e. after the first...
|
by: sangram.0149 |
last post by:
hi,
can someone plz help me on this one
i need to fetch the first record from every group of records with the
same emp id.
i cannot use group by because i want to fetch all the fields...
|
by: IcedDante |
last post by:
Working with a sorted group, the inability to use following-sibling
(which uses Document Order) and convert an RTF (not avaible with the
Parser that we are using) hampered our ability to solve the...
|
by: tomlebold |
last post by:
When using Access security can each group have only one password?
So all members of the group have the same and/or one password?
Then in order to have each member of a group have their own password...
|
by: KDawg44 |
last post by:
Hi,
I am new to Python and am trying to write a little front end to
another application in Python.
What I want is to have a gui pop up listing some items with several
buttons. The guts of...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |