473,396 Members | 1,918 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,396 software developers and data experts.

Select Max query with two group by's

78
Tried to look at other examples, but couldn't quite get this done. I have the table Fuel with columns Fuel ID (pk), FuelType, Price, Discount, Vendor, Date. The end result is a query that shows the Fuel ID, Price, and Discount on the max date for each fuel type for each vendor (Fuel ID's are unique/autonumber; three different FuelTypes; multiple vendors, multiple dates). I've seen how to use max of date for one group by, but cannot figure out how to do that for two, or if I'm even on the right track. Thanks for the help!
Mar 28 '08 #1
9 1688
PianoMan64
374 Expert 256MB
Tried to look at other examples, but couldn't quite get this done. I have the table Fuel with columns Fuel ID (pk), FuelType, Price, Discount, Vendor, Date. The end result is a query that shows the Fuel ID, Price, and Discount on the max date for each fuel type for each vendor (Fuel ID's are unique/autonumber; three different FuelTypes; multiple vendors, multiple dates). I've seen how to use max of date for one group by, but cannot figure out how to do that for two, or if I'm even on the right track. Thanks for the help!
Can you give me an example of how your data is stored in the table as well as field types for each field.

and I don't understand what you mean when you say that you want to do it on more than one date? Do you mean you want to display it for more than one day, or is there another date field in the table structure that you haven't included in you example?

Let me know by write back to my email,

hope that helps,

Joe P.
Mar 29 '08 #2
kpfunf
78
Below is sample data (very small sample):

Fuel ID FuelType Price Discount Vendor Date
297 SelfServe $5.35 $0.00 Air Wing 3/27/2008
287 AvGas $5.78 $0.00 Air Wing 3/27/2008
58 AvGas $5.46 $0.00 Air Wing 1/17/2008
59 SelfServe $5.05 $0.00 Air Wing 1/17/2008
67 AvGas $4.89 $0.20 Air Services 1/17/2008
69 JetA $5.09 $0.00 Air Services 1/17/2008
199 AvGas $4.95 $0.00 Air Services 2/28/2008
200 SelfServe $4.80 $0.00 Air Services 2/28/2008

The result would look like this:
Fuel ID FuelType Price Discount Vendor Date
297 SelfServe $5.35 $0.00 Air Wing 3/27/2008
287 AvGas $5.78 $0.00 Air Wing 3/27/2008
69 JetA $5.09 $0.00 Air Services 1/17/2008
199 AvGas $4.95 $0.00 Air Services 2/28/2008
200 SelfServe $4.80 $0.00 Air Services 2/28/2008

I need to go by date and not max FuelID in case of backdating. The "more than one date" is there could be more than one "Max Date" for each vendor because there are different fuel types (ex. Air Services).

Below is the data type for each column (FuelType and Vendor are numbered foreign keys to other tables with descriptions):
Fuel ID FuelType Price Discount Vendor Date
Autonumber Number Currency Currency Number Date/Time
Mar 31 '08 #3
PianoMan64
374 Expert 256MB
Below is sample data (very small sample):

Fuel ID FuelType Price Discount Vendor Date
297 SelfServe $5.35 $0.00 Air Wing 3/27/2008
287 AvGas $5.78 $0.00 Air Wing 3/27/2008
58 AvGas $5.46 $0.00 Air Wing 1/17/2008
59 SelfServe $5.05 $0.00 Air Wing 1/17/2008
67 AvGas $4.89 $0.20 Air Services 1/17/2008
69 JetA $5.09 $0.00 Air Services 1/17/2008
199 AvGas $4.95 $0.00 Air Services 2/28/2008
200 SelfServe $4.80 $0.00 Air Services 2/28/2008

The result would look like this:
Fuel ID FuelType Price Discount Vendor Date
297 SelfServe $5.35 $0.00 Air Wing 3/27/2008
287 AvGas $5.78 $0.00 Air Wing 3/27/2008
69 JetA $5.09 $0.00 Air Services 1/17/2008
199 AvGas $4.95 $0.00 Air Services 2/28/2008
200 SelfServe $4.80 $0.00 Air Services 2/28/2008

I need to go by date and not max FuelID in case of backdating. The "more than one date" is there could be more than one "Max Date" for each vendor because there are different fuel types (ex. Air Services).

Below is the data type for each column (FuelType and Vendor are numbered foreign keys to other tables with descriptions):
Fuel ID FuelType Price Discount Vendor Date
Autonumber Number Currency Currency Number Date/Time
Ok, I got your email and have figured out how to display this with your two groupby statements. It's simply a matter of removing from your groupby statement anything that isn't needed to evaluate the problem.

Here is the SQL Statement that you need to use:

Expand|Select|Wrap|Line Numbers
  1. SELECT Fuels.FuelType, Max(Fuels.Price) AS MaxOfPrice, Fuels.Vendor, Max(Fuels.Date) AS MaxOfDate
  2. FROM Fuels
  3. GROUP BY Fuels.FuelType, Fuels.Vendor;
  4.  
Mar 31 '08 #4
kpfunf
78
PianoMan,
That works great. The question I now have is how to add the other data in. If I use what you wrote as a subquery, do I just join on each field to the Fuel table for a master query with all info (all columns). The unique field of FuelID, when added into the query you wrote, throws it out of whack (which makes sense; as you said, don't use things not in the evaluation). But how do I tie back without the unique field?

PS Trying to join on all fields dropped records.
Mar 31 '08 #5
PianoMan64
374 Expert 256MB
PianoMan,
That works great. The question I now have is how to add the other data in. If I use what you wrote as a subquery, do I just join on each field to the Fuel table for a master query with all info (all columns). The unique field of FuelID, when added into the query you wrote, throws it out of whack (which makes sense; as you said, don't use things not in the evaluation). But how do I tie back without the unique field?

PS Trying to join on all fields dropped records.
Still working on that part. Will get back to you.
Apr 1 '08 #6
kpfunf
78
Figured out the dropped records part: Max price. I don't want the max price, only the max date. With that max date (for each fuel type, for each vendor), I want the row's price, discount, and fuel ID.
Apr 1 '08 #7
kpfunf
78
Figured out the dropped records part: Max price. I don't want the max price, only the max date. With that max date (for each fuel type, for each vendor), I want the row's price, discount, and fuel ID.
BUMP .
Apr 3 '08 #8
kpfunf
78
Solved. Used max date query, and joined to original query on all fields.
Apr 7 '08 #9
PianoMan64
374 Expert 256MB
Solved. Used max date query, and joined to original query on all fields.
Thanks for letting me know. Happy Programming...;-)

Joe P.
Apr 8 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ian T | last post by:
Hi, I've got what I think (probably incorrectly) should be a simple SELECT : Two colums with data like col1 col2 1 50 1 51 2 50
5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
4
by: Mick White | last post by:
mysql> select * from guestbook; +----+--------+---------+-----------------+----------------+ | id | fname | lname | comments | time_in |...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
1
by: jtwright | last post by:
I've got a view that creates a parent child relationship, this view is used in Analysis Services to create a dimension in a datastore. This query tends to deadlock after about 10 days of running...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
8
by: Jean | last post by:
Hello all, I have the following data, that was queried and sorted to columns PROBLEM_ID and then by STATUSDATE (ascending): STATUS_ID STATUSDATE PROBLEM_ID --------- ---------- ...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
0
by: djflow | last post by:
Hi! II was wondering if you can help me with SQL query.. Below 7 separated select query works fine(only when they are retrieved separately) But I want to combined them together and so that i...
1
by: zafm86 | last post by:
Hi everyone! I'm sure my problem is pretty easy to solve but I've been working on it for a long and my my brain is not working correctly anymore. I'm working with an AS400 and I mhave to do an...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.