|
-
Dec 10th, 2003, 12:19 PM
#1
Thread Starter
Fanatic Member
Sql Query Help!!!!
Oh man.... I have been fighting with this for days now... I need help... please.....
I have this table
Price Adults idSeason
--------------------------------------- ------ -----------
50 1 155
200 2 155
150 3 155
50 1 156
125 2 156
Now.. the table is reduced in complexity and size... but pretty much what I need is this....
I need a select like this
select price, adults
from table
group by idSeason
having adults = max(adults)
do you understand what I am trying to do? I want to select only the registries that have the top adults of the season... in this case I would get (if it worked):
150 3 155
125 2 156
PLEASE HELP ME!!
The having adults = max(adults) doesnt work!
How do I do this??
THANKS!!!
"The difference between mad and genius is the success"
-
Dec 10th, 2003, 12:26 PM
#2
Code:
SELECT Price, Max(Adults) As MaxAdults, idSeason
FROM Table
GROUP BY idSeason, Price
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 10th, 2003, 12:29 PM
#3
I was too lazy to create a test table to query from.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 10th, 2003, 12:39 PM
#4
Thread Starter
Fanatic Member
nope... none of them work!!
having adults=max(adults) doesnt filter anything... actually... adults and max(adults) if you put them in the select list .. they are the same value!!
"The difference between mad and genius is the success"
-
Dec 10th, 2003, 12:55 PM
#5
I am on my second cup of coffee now.
Solution.
Create two queries.
Code:
'Query1:
SELECT Table1.idSeason, Max(Table1.Adults) AS MaxOfAdults
FROM Table1
GROUP BY Table1.idSeason;
'Query2:
SELECT Query1.idSeason, Query1.MaxOfAdults, Table1.Price
FROM Query1
INNER JOIN Table1 ON (Query1.MaxOfAdults = Table1.Adults) AND (Query1.idSeason = Table1.idSeason)
GROUP BY Query1.idSeason, Query1.MaxOfAdults, Table1.Price;
'Results:
idSeason MaxOfAdults Price
155 3 150
156 2 125
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 10th, 2003, 01:05 PM
#6
Thread Starter
Fanatic Member
can it be done in one?!?!?
"The difference between mad and genius is the success"
-
Dec 10th, 2003, 01:36 PM
#7
Thread Starter
Fanatic Member
I guess not..
"The difference between mad and genius is the success"
-
Dec 10th, 2003, 01:40 PM
#8
Lively Member
-
Dec 10th, 2003, 02:39 PM
#9
Thread Starter
Fanatic Member
SQL SERVER 7.0

Thanks....
"The difference between mad and genius is the success"
-
Dec 10th, 2003, 03:03 PM
#10
SQL Server 7.0 Solution in one query.
Code:
SELECT
Q2.idSeason,
Q2.MaxAdults,
Table1.Price
FROM
(
SELECT
Q1.idSeason,
Q1.MaxAdults
FROM
(SELECT
Table1.idSeason,
Max(Table1.Adults) AS MaxAdults
FROM
Table1
GROUP BY
Table1.idSeason
) AS Q1
)
AS Q2
INNER JOIN Table1 ON (Q2.MaxAdults=Table1.Adults) AND (Q2.idSeason=Table1.idSeason)
GROUP BY
Q2.idSeason,
Q2.MaxAdults,
Table1.Price
RESULTS:
idSeason Adults Price
-------------------------------------
155 3 150
156 2 125
PS. I am on my thrid cup of coffee.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 10th, 2003, 04:02 PM
#11
With your query modified from tablename to table1...
Code:
Select Price, Adults, idSeason
from table1
group by idseason
having adults=max(adults)
I get these errors on SQL 2000 actually.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'table1.Price' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'table1.Adults' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8121, Level 16, State 1, Line 1
Column 'table1.Adults' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|