Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

query for row count on latest or max date

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
rakesh17684

New User


Joined: 08 Oct 2006
Posts: 59
Location: Atlanta

PostPosted: Wed Aug 11, 2010 10:57 pm    Post subject: query for row count on latest or max date
Reply with quote

I have a table(itemlist) containing

name date item
rakesh 2007-10-02 shoe
Suresh 2008-12-03 book
vinodh 2009-11-06 mobile
rakesh 2009-07-06 shoe
Suresh 2008-12-03 pen
rakesh 2009-07-06 bike
Mahesh 2010-02-06 bag
vinodh 2004-16-09 mobile

I want to get all the names and number of items they have bought lately
The result will be like
name max date count
rakesh 2009-07-06 2
Suresh 2008-12-03 2
Mahesh 2010-02-06 1
vinodh 2009-11-06 1

The query i wrote was

Code:
select name,max(date),count(*) from itemlistgroup by name;

but this gives me
the entire count and not count for the latest date

name max date count
rakesh 2009-07-06 3
Suresh 2008-12-03 2
Mahesh 2010-02-06 1
vinodh 2009-11-06 2
can you please point out what i might be missing or where i am going wrong in the query ?
Thanks in advance
Back to top
View user's profile Send private message

sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1013
Location: India

PostPosted: Thu Aug 12, 2010 6:38 am    Post subject:
Reply with quote

Rakesh,

When does the latest date start ?

After knowing that, you can put a condition based on that.

Sushanth
Back to top
View user's profile Send private message
rakesh17684

New User


Joined: 08 Oct 2006
Posts: 59
Location: Atlanta

PostPosted: Thu Aug 12, 2010 8:20 am    Post subject:
Reply with quote

i might have confused a bit .
i just want to know how much each person has bought in their last shopping date.
For example.
though rakesh (the one in the example provided) had bought 1 item on 2007-10-02 . i just want to know how much he had bought on 2009-07-06 which happens to be his last date he had gone for shopping
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1278
Location: Belgium

PostPosted: Thu Aug 12, 2010 12:02 pm    Post subject:
Reply with quote

Code:
select name, date_col, count(*)
  from tab1 A
 where date_col = (select max(date_col) from tab1 B where a.name = b.name)
 group by name,date

The best performance would be if there was an index on or beginning with (name,date_col)
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us