Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL query to sum data


SQL query to sum data

Author
Message
jamie_collins
jamie_collins
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 225
I have a table with database sizes in it that tracks the size of the database for every day of the past year:

Table:
Server Database Date Size
CS1 cust 1/1/2013 1200
CS1 cust 1/2/2013 1210
CS1 prog 1/1/2013 800
CS1 prog 1/2/2013 820
.
.
CS1 cust 8/1/2013 1700
CS1 cust 8/2/2013 1800
CS1 prog 8/1/2013 1000
CS1 prog 8/2/2013 1020

I have a query that gives me the highest size per month for a specific database:


SELECT MONTH(Date) AS Month, max(Size) AS Size
FROM DatabaseSize_Archive
WHERE Server = 'CS1'
AND Database = 'cust'
GROUP BY MONTH(Date)
ORDER BY Month DESC



Returns:
Month Size
1 1210
8 1800

But I need a query that gets me the Sum of that highest size for the server per month...

Results:
Month Size
1 2030
8 1840

Obviously changing MAX in the above query to just SUM gets me the sum of every size and that's not what I need.

Any assistance would be appreciated.
Thanks

(Sorry for the formatting, not sure how to insert table data)
jamie_collins
jamie_collins
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 225
Got it:


with cte as
(
SELECT Database, MONTH(Date) AS Month, max(Size) AS Size
FROM DatabaseSize_Archive
WHERE Server = 'CS1'
GROUP BY MONTH(Date)
)

SELECT Month, SUM(Size)
FROM cte
GROUP BY Month
ORDER BY Month DESC


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search