Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Go Back   DeveloperBarn Forums > Databases > SQL Development

Sponsored Links

Discuss "Select latest dates" in the SQL Development forum.

SQL Development - Structured Query Language (SQL) is the talk of databases. Discuss topics such as joins, triggers and other advanced SQL topics.


Closed Thread
 
LinkBack Thread Tools Display Modes
  #1  
Old April 11th, 2008, 09:07 AM
Wolffy's Avatar
Slaprentice of Wolves
 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 176
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default Select latest dates

Every once in a while, I have a query that I just can't wrap my head around. I figure by posting here 1 of 2 things will happen. Somebody will come up with the query or I'll figure it out 30 seconds after I hit save.

I need to SELECT the latest record of type INIT for each user. Each user may have multiple INIT records, but only the latest one has any meaning. The table has a compound key consisting of Bldg, UserNo (yup, each building maintains its own records of employees) and a full time stamp. Thus:
Code:
Bldg  ID  TS                    Type  OtherStuff
A       1  03-01-2001-07:30:25 INIT <more columns...> --1
A       2  02-01-2000-12:30:22 INIT <more columns...> --2
A       1  03-02-2001-11:12:52 ADJ  <more columns...> --3
B       1  02-03-2002-15:00:12 INIT <more columns...> --4
A       1  04-17-2005-00:12:12 INIT <more columns...> --5
In this example, I need records 2, 4 and 5. Note, I need the entire record, including the <more columns...> stuff.

Can't seem to sort this one for some reason.
Sponsored Links
  #2  
Old April 11th, 2008, 09:22 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 952
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 6
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

By the looks of it, the same user could be in multiple buildings, correct? In your post, User ID 1 is in building A and B. Hmmm...that makes it a little more tricky. At first I was thinking a SELECT DISTINCT would help, but then you wouldn't get the other user record if that user belonged to more than one building....
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

  #3  
Old April 11th, 2008, 09:25 AM
Wolffy's Avatar
Slaprentice of Wolves
 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 176
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

Yup, about 2 minutes after I posted, I had a flash of insight.

Code:
SELECT a.* 
  FROM aTable a
JOIN 
(select bldg, id, max(ts) as lastDate from aTable
where type = 'INIT'
group by  bldg,id) k
ON (a.bldg= k.bldg and a.id = k.id and ts = lastDate)
The table was on a remote server, so each attempt was painful.
Closed Thread

  DeveloperBarn Forums > Databases > SQL Development

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -4. The time now is 10:20 AM.



Content Relevant URLs by vBSEO 3.2.0