Unix Technical Forum

Please help with this tough SQL query

This is a discussion on Please help with this tough SQL query within the SQL Server forums, part of the Microsoft SQL Server category; --> I've been trying this one for 2-3 hours and can't figure it out. I'de appreciate any help or pointers ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 03:52 AM
Roman
 
Posts: n/a
Default Please help with this tough SQL query

I've been trying this one for 2-3 hours and can't figure it out. I'de
appreciate any help or pointers in the right direction. Thanks.

Query
I need the query to return me all the lottery names and results that
have the latest date in the database for that particular game and for
the state [AZ]. So the return data from the data below data would be:

Result:
--------------------------
AZ Atlantic 6/49 2004-08-07 3-6-8-12-19-24 18
AZ Atlantic PayDay 2004-08-05 15-51-59-75
AZ Atlantic Tag 2004-08-08 4-6-1-6-7-6



Example Table "Lottery":
----------------------------------------------------
State|Game | Date | Results

AZ Atlantic 6/49 2004-08-04 5-16-17-26-38-44 46
AZ Atlantic 6/49 2004-08-07 3-6-8-12-19-24 18
AZ Atlantic PayDay 2004-07-29 2-23-62-77
AZ Atlantic PayDay 2004-08-05 15-51-59-75
AZ Atlantic Tag 2004-08-04 5-8-9-1-2-3
AZ Atlantic Tag 2004-08-08 4-6-1-6-7-6
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 03:52 AM
Simon Hayes
 
Posts: n/a
Default Re: Please help with this tough SQL query


"Roman" <roman.ny@verizon.net> wrote in message
news:faaa6b3e.0408090912.600b9db5@posting.google.c om...
> I've been trying this one for 2-3 hours and can't figure it out. I'de
> appreciate any help or pointers in the right direction. Thanks.
>
> Query
> I need the query to return me all the lottery names and results that
> have the latest date in the database for that particular game and for
> the state [AZ]. So the return data from the data below data would be:
>
> Result:
> --------------------------
> AZ Atlantic 6/49 2004-08-07 3-6-8-12-19-24 18
> AZ Atlantic PayDay 2004-08-05 15-51-59-75
> AZ Atlantic Tag 2004-08-08 4-6-1-6-7-6
>
>
>
> Example Table "Lottery":
> ----------------------------------------------------
> State|Game | Date | Results
>
> AZ Atlantic 6/49 2004-08-04 5-16-17-26-38-44 46
> AZ Atlantic 6/49 2004-08-07 3-6-8-12-19-24 18
> AZ Atlantic PayDay 2004-07-29 2-23-62-77
> AZ Atlantic PayDay 2004-08-05 15-51-59-75
> AZ Atlantic Tag 2004-08-04 5-8-9-1-2-3
> AZ Atlantic Tag 2004-08-08 4-6-1-6-7-6


Here is one possible solution:

select l.state, l.game, l.gamedate, l.results
from Lottery l
join (
select state, game, max(gamedate) as gamedate
from Lottery
group by state, game
) dt
on l.state = dt.state
and l.game = dt.game
and l.gamedate = dt.gamedate

For future reference, you will usually get a better answer if you post your
table information and data in the form of a CREATE TABLE statement plus
INSERTs for the data. That way, other people can simply copy, paste and run
your code in Query Analyzer, and you also avoid any confusion about data
types, constraints etc.

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 03:53 AM
Roman
 
Posts: n/a
Default Re: Please help with this tough SQL query

This didnt quite work for me, but I finanally figured out a working
solution. Thanks.

"Simon Hayes" <sql@hayes.ch> wrote in message news:<4117b432$1_2@news.bluewin.ch>...
> "Roman" <roman.ny@verizon.net> wrote in message
> news:faaa6b3e.0408090912.600b9db5@posting.google.c om...
> > I've been trying this one for 2-3 hours and can't figure it out. I'de
> > appreciate any help or pointers in the right direction. Thanks.
> >
> > Query
> > I need the query to return me all the lottery names and results that
> > have the latest date in the database for that particular game and for
> > the state [AZ]. So the return data from the data below data would be:
> >
> > Result:
> > --------------------------
> > AZ Atlantic 6/49 2004-08-07 3-6-8-12-19-24 18
> > AZ Atlantic PayDay 2004-08-05 15-51-59-75
> > AZ Atlantic Tag 2004-08-08 4-6-1-6-7-6
> >
> >
> >
> > Example Table "Lottery":
> > ----------------------------------------------------
> > State|Game | Date | Results
> >
> > AZ Atlantic 6/49 2004-08-04 5-16-17-26-38-44 46
> > AZ Atlantic 6/49 2004-08-07 3-6-8-12-19-24 18
> > AZ Atlantic PayDay 2004-07-29 2-23-62-77
> > AZ Atlantic PayDay 2004-08-05 15-51-59-75
> > AZ Atlantic Tag 2004-08-04 5-8-9-1-2-3
> > AZ Atlantic Tag 2004-08-08 4-6-1-6-7-6

>
> Here is one possible solution:
>
> select l.state, l.game, l.gamedate, l.results
> from Lottery l
> join (
> select state, game, max(gamedate) as gamedate
> from Lottery
> group by state, game
> ) dt
> on l.state = dt.state
> and l.game = dt.game
> and l.gamedate = dt.gamedate
>
> For future reference, you will usually get a better answer if you post your
> table information and data in the form of a CREATE TABLE statement plus
> INSERTs for the data. That way, other people can simply copy, paste and run
> your code in Query Analyzer, and you also avoid any confusion about data
> types, constraints etc.
>
> Simon

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 03:53 AM
Ross Presser
 
Posts: n/a
Default Re: Please help with this tough SQL query

On 9 Aug 2004 10:12:15 -0700, Roman wrote:

> I've been trying this one for 2-3 hours and can't figure it out. I'de
> appreciate any help or pointers in the right direction. Thanks.
>
> Query
> I need the query to return me all the lottery names and results that
> have the latest date in the database for that particular game and for
> the state [AZ]. So the return data from the data below data would be:
>
> Result:
> --------------------------
> AZ Atlantic 6/49 2004-08-07 3-6-8-12-19-24 18
> AZ Atlantic PayDay 2004-08-05 15-51-59-75
> AZ Atlantic Tag 2004-08-08 4-6-1-6-7-6
>
>
>
> Example Table "Lottery":
> ----------------------------------------------------
> State|Game | Date | Results
>
> AZ Atlantic 6/49 2004-08-04 5-16-17-26-38-44 46
> AZ Atlantic 6/49 2004-08-07 3-6-8-12-19-24 18
> AZ Atlantic PayDay 2004-07-29 2-23-62-77
> AZ Atlantic PayDay 2004-08-05 15-51-59-75
> AZ Atlantic Tag 2004-08-04 5-8-9-1-2-3
> AZ Atlantic Tag 2004-08-08 4-6-1-6-7-6



Inferred DDL statement:

CREATE TABLE "Lottery" (
"State" char(2),
"Game" char(30),
"Date" datetime,
"Results" char(255),
CONSTRAINT pk_Lottery PRIMARY KEY (State,Game,Date)
)
INSERT INTO Lottery (State,Game,Date,Results)
VALUES ('AZ', 'Atlantic 6/49', '2004-08-04', '5-16-17-26-38-44 46')
INSERT INTO Lottery (State,Game,Date,Results)
VALUES ('AZ', 'Atlantic 6/49', '2004-08-07', '3-6-8-12-19-24 18')
INSERT INTO Lottery (State,Game,Date,Results)
VALUES ('AZ', 'Atlantic PayDay', '2004-07-29', '2-23-62-77')
INSERT INTO Lottery (State,Game,Date,Results)
VALUES ('AZ', 'Atlantic PayDay', '2004-08-05', '15-51-59-75')
INSERT INTO Lottery (State,Game,Date,Results)
VALUES ('AZ', 'Atlantic Tag', '2004-08-04', '5-8-9-1-2-3')
INSERT INTO Lottery (State,Game,Date,Results)
VALUES ('AZ', 'Atlantic Tag', '2004-08-08', '4-6-1-6-7-6')

Here's the query you need:

SELECT A.State, A.Game, A.[Date], a.Results
From Lottery A
INNER JOIN (
SELECT b1.State, b1.Game, Max(b1.[Date]) "MaxDate"
FROM Lottery B1
GROUP BY b1.State, b1.Game ) B
ON A.State = B.State
AND A.Game = B.Game
AND A.Date = B.MaxDate
WHERE A.State = 'AZ'

Results:

AZ Atlantic Tag 2004-08-08 4-6-1-6-7-6
AZ Atlantic PayDay 2004-08-05 15-51-59-75
AZ Atlantic 6/49 2004-08-07 3-6-8-12-19-24 18

(3 row(s) affected)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:02 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com