473,796 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query Help: Select Max of Grouped Sum Data

28 New Member
I have a database that has a Date column (1/1/2007) and a Data column that has numerical data. I am currently running a query to sum the Data column for a certain month and grouped by day (There is more than items of Data for a specific Day). Here is my current query:

SELECT SUM(Data) as Total
FROM database
WHERE Month(Date) = 10
And Year(Date) = 2007
Group by Day(Date)

This displays the summed data which is grouped by the Days of the month. This works fine, but I want to display the MAX out of these results. So, the MAX data for a Day of the specific Month. This is what I tried but am getting errors:

SELECT MAX(SUM(Data)) as Max_Total
FROM database
WHERE Month(Date) = 10
And Year(Date) = 2007
Group by Day(Date)

I'm not sure what I am doing wrong. Any ideas? Thanks!
Oct 3 '07 #1
8 30064
davash6
9 New Member
I have a database that has a Date column (1/1/2007) and a Data column that has numerical data. I am currently running a query to sum the Data column for a certain month and grouped by day (There is more than items of Data for a specific Day). Here is my current query:

SELECT SUM(Data) as Total
FROM database
WHERE Month(Date) = 10
And Year(Date) = 2007
Group by Day(Date)

This displays the summed data which is grouped by the Days of the month. This works fine, but I want to display the MAX out of these results. So, the MAX data for a Day of the specific Month. This is what I tried but am getting errors:

SELECT MAX(SUM(Data)) as Max_Total
FROM database
WHERE Month(Date) = 10
And Year(Date) = 2007
Group by Day(Date)

I'm not sure what I am doing wrong. Any ideas? Thanks!


SELECT SUM(Data) as Max_Total INTO TEMP_TABLE
FROM database
WHERE Month(Date) = 10
And Year(Date) = 2007
Group by Day(Date)
GO
SELECT MAX(Max_Total) FROM TEMP_TABLE
GO
DROP TABLE TEMP_TABLE
GO
Oct 4 '07 #2
nico3334
28 New Member
SELECT SUM(Data) as Max_Total INTO TEMP_TABLE
FROM database
WHERE Month(Date) = 10
And Year(Date) = 2007
Group by Day(Date)
GO
SELECT MAX(Max_Total) FROM TEMP_TABLE
GO
DROP TABLE TEMP_TABLE
GO
Thanks for your reply. But, is there a way to do it without temp tables?
Oct 5 '07 #3
Motoma
3,237 Recognized Expert Specialist
I have a database that has a Date column (1/1/2007) and a Data column that has numerical data. I am currently running a query to sum the Data column for a certain month and grouped by day (There is more than items of Data for a specific Day). Here is my current query:

SELECT SUM(Data) as Total
FROM database
WHERE Month(Date) = 10
And Year(Date) = 2007
Group by Day(Date)

This displays the summed data which is grouped by the Days of the month. This works fine, but I want to display the MAX out of these results. So, the MAX data for a Day of the specific Month. This is what I tried but am getting errors:

SELECT MAX(SUM(Data)) as Max_Total
FROM database
WHERE Month(Date) = 10
And Year(Date) = 2007
Group by Day(Date)

I'm not sure what I am doing wrong. Any ideas? Thanks!
You will need to use a subquery:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     MAX(intermediate.Total)
  3. FROM 
  4.     (
  5.         SELECT SUM(Data) as Total
  6.         FROM database
  7.         WHERE Month(Date) = 10 
  8.         And Year(Date) = 2007
  9.         Group by Day(Date)
  10.     ) intermediate
  11.  
  12.  
  13.  
Oct 5 '07 #4
nico3334
28 New Member
You will need to use a subquery:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     MAX(intermediate.Total)
  3. FROM 
  4.     (
  5.         SELECT SUM(Data) as Total
  6.         FROM database
  7.         WHERE Month(Date) = 10 
  8.         And Year(Date) = 2007
  9.         Group by Day(Date)
  10.     ) intermediate
  11.  
  12.  
  13.  

Thank you that works great!

One more thing, would it be possible to also select the "day" of the date that is associated with that Max value?

I tried something like this, but it didn't work:

SELECT
MAX(intermediat e.Total), Max_Day
FROM
(
SELECT SUM(Data) as Total
Day(Date) As Max_Day
FROM database
WHERE Month(Date) = 10
And Year(Date) = 2007
Group by Day(Date)
) intermediate
Oct 5 '07 #5
Motoma
3,237 Recognized Expert Specialist
Try:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     MAX(intermediate.Total), Max_Day
  3. FROM 
  4.     (
  5.         SELECT SUM(Data) as Total
  6.         Day(Date) As Max_Day
  7.         FROM database
  8.         WHERE Month(Date) = 10 
  9.         And Year(Date) = 2007
  10.         Group by Day(Date)
  11.         HAVING Total = MAX(Total)
  12.     ) intermediate
  13.  
And please use code tags in the future.
Oct 6 '07 #6
nico3334
28 New Member
Try:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     MAX(intermediate.Total), Max_Day
  3. FROM 
  4.     (
  5.         SELECT SUM(Data) as Total
  6.         Day(Date) As Max_Day
  7.         FROM database
  8.         WHERE Month(Date) = 10 
  9.         And Year(Date) = 2007
  10.         Group by Day(Date)
  11.         HAVING Total = MAX(Total)
  12.     ) intermediate
  13.  
And please use code tags in the future.
Thank you so much for your help. I'm sorry, I'm still getting an error. In SQL Query Analyzer, I get the following message:

Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'Total'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'Total'.

So, it's not recognizing the Total Columns in the 2nd last line:

HAVING Total = MAX(Total)

Thanks again :)
Oct 8 '07 #7
iburyak
1,017 Recognized Expert Top Contributor
Try this:


Expand|Select|Wrap|Line Numbers
  1. SELECT Top 1 SUM(DATA) AS Total
  2.              Day(Date) AS Max_Day
  3. FROM DATABASE
  4. WHERE Month(Date) = 10 
  5. AND Year(Date) = 2007
  6. GROUP BY Day(Date)
  7. order by 1 desc
Good Luck.
Oct 8 '07 #8
nico3334
28 New Member
Try this:


Expand|Select|Wrap|Line Numbers
  1. SELECT Top 1 SUM(DATA) AS Total
  2.              Day(Date) AS Max_Day
  3. FROM DATABASE
  4. WHERE Month(Date) = 10 
  5. AND Year(Date) = 2007
  6. GROUP BY Day(Date)
  7. order by 1 desc
Good Luck.

Thank you so much for your help!
Oct 8 '07 #9

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

Similar topics

4
1657
by: NotGiven | last post by:
I have two tables, Client and Project, related by clientID. I want to display a table with total CURRENT projects and total COMPLETED projects per client. I'd like for it to show like this: Client Name Current Proj Completed Proj Stan's Dormers 4 12 Larry Lincoln Dealership 2 1
6
3198
by: Pedro Fonseca | last post by:
Greetings! Can someone please help me to crack this problem? I have 4 tables: Subject, Forum, Topic and Post. A Subject groups various Forums, a Forum groups various Topics, and a Topic groups several Posts. The references inside the tables are: +-----------+ +-----------+ +-----------+ +-----------+ | Subject | | Forum | | Topic | | Post | +-----------+ +-----------+ +-----------+ +-----------+
9
3138
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
5
1347
by: Alexander Korovyev | last post by:
Suppose I have two tables: CREATE TABLE Tab1 ( NOT NULL, NOT NULL, NOT NULL, NOT NULL) CREATE TABLE Tab2 ( NOT NULL,
2
1472
by: Gregory Konev | last post by:
Hello, All! Example. I have a table with information about payments (id, amount, pay_date, customer_id). Can I select two sums for two different date ranges, grouped by 'customer_id' (for each customer) in one query? -- With best regards, Gregory Konev. E-mail: voyage@ml.net.ua
2
2363
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20 00:00:00.000 Subject_1 877 1900-01-01 00:00:00.000 Subject_2 877 2004-12-20 00:00:00.000 Subject_3
14
611
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN PartsJournal ON PartLocations.LocationID = PartsJournal.LocationID GROUP BY PartLocations.LocationName, PartsJournal.PartsLotNumber, PartsJournal.PartNumber, PartsJournal.LocationID HAVING (((Sum(PartsJournal.Quantity))>0) AND...
6
2398
by: Ajay | last post by:
Hi I am trying to do the following. I have a set of records Example 1 Test 2 test 3 TEST 4 ACCESS 5 EXCEL
5
2281
by: BillCo | last post by:
I'm having a problem with a union query, two simple queries joined with a union statement. It's created in code based on parameters. Users were noticing some inconsistant data and when I analysed the query produced and opened it from a MS Query it started giving strange results. The first query when run alone returns 22 records, some of which have identical values in all fields. This is 100% correct. The second query returns nothing....
0
10231
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9054
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.