473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Jul 20 '05 #1
3 2583

"Roman" <ro******@verizon.net> wrote in message
news:fa**************************@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
Jul 20 '05 #2
This didnt quite work for me, but I finanally figured out a working
solution. Thanks.

"Simon Hayes" <sq*@hayes.ch> wrote in message news:<41**********@news.bluewin.ch>...
"Roman" <ro******@verizon.net> wrote in message
news:fa**************************@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

Jul 20 '05 #3
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)
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: leegold2 | last post by:
Let's I do a mysql query and then I do a, for( $i = 1; $row = mysql_fetch_array($result); $i++ ) {...} and it gives me this: PageID Title URL Description 1 lee's ...
2
by: news | last post by:
This is a tough question to ask. I need to pull up a report of all the orders in our e-commerce site, that has more than one item attributed to it. orders.ordernum, orderitems.itemnumber...
13
by: Nick Coe \(UK\) | last post by:
I'm seriously considering setting up the future development of AccHelp as an open source project on sourceforge. Why? I just don't have the time (for various personal and professional reasons)...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
28
by: Arial | last post by:
My SQL string is kind of wierd one. In my application, I need to select things from an unknown name table. But I know the table name before the SQL command is executed. For instance, Dim...
118
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely...
7
by: Dan | last post by:
I am trying to create a query (in either sql or the design view) to determine which two (or more I suppose if it's not too complicated) baseball players were teammates the longest. The database...
8
by: rhaazy | last post by:
tblOrgSystem OrgSystemID OrgSystem 1 USA 2 CANADA tblOrgSystemNode OrgSystemID OrgNodeID OrgNode
5
by: steven.fafel | last post by:
I am running 2 versions of a correlated subquery. The two version differ slightly in design but differ tremendously in performance....if anyone can answer this, you would be awesome. The "bad"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.