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


Sum then Divide


Sum then Divide

Author
Message
ccmret
ccmret
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 94
Is there any easier way to do this sum/divide without having to put results of sum in a temp table first? Trying to do it all in one query give me:

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

SELECT Distinct LEFT(CONVERT(DateTime, IndexDate , 110), 11)AS IndexDate,
COUNT(*) AS No_Records,
Round(AVG(Test1),2) As Test1_AVG,
Round(AVG(Test2),2) AS Test2_AVG,
SUM(t_complete/COUNT (*)) As Percent_Complete

FROM TestScores.dbo.Analysis
Group by IndexDate
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16545 Visits: 16999
ccmret (4/1/2013)
Is there any easier way to do this sum/divide without having to put results of sum in a temp table first? Trying to do it all in one query give me:

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

SELECT Distinct LEFT(CONVERT(DateTime, IndexDate , 110), 11)AS IndexDate,
COUNT(*) AS No_Records,
Round(AVG(Test1),2) As Test1_AVG,
Round(AVG(Test2),2) AS Test2_AVG,
SUM(t_complete/COUNT (*)) As Percent_Complete

FROM TestScores.dbo.Analysis
Group by IndexDate


You can't do it like that because you have an aggregate within an aggregate.

Try this.

SUM(t_complete)/COUNT (*) As Percent_Complete



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
ccmret
ccmret
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 94
The query successfully completed with your suggestion. Howerver, it returned a zero.
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24173 Visits: 37942
ccmret (4/1/2013)
The query successfully completed with your suggestion. Howerver, it returned a zero.


Integer math, try:

SUM(t_complete)/(COUNT (*) * 1.0) As Percent_Complete

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ccmret
ccmret
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 94
Still provides 0's. The numbers that I am trying to divide are:

t_complete = 176420
Count = 5265365

Format is tinyint.

Thoughts?
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24173 Visits: 37942
ccmret (4/1/2013)
Still provides 0's. The numbers that I am trying to divide are:

t_complete = 176420
Count = 5265365

Format is tinyint.

Thoughts?




Please explain what you mean by format is tinyint?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16545 Visits: 16999
Lynn Pettis (4/1/2013)
ccmret (4/1/2013)
Still provides 0's. The numbers that I am trying to divide are:

t_complete = 176420
Count = 5265365

Format is tinyint.

Thoughts?




Please explain what you mean by format is tinyint?


I have a feeling this is being in a tinyint variable/column. Either way this pointless. Not only does tinyint not have anywhere near enough storage it will only hold whole numbers between 0 and 255. The values you posted would be less than 1 so a tinyint will have 0.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24173 Visits: 37942
Based on what you posted:



declare @t_complete int = 176420,
@myCount int = 5265365;

select @t_complete / @mycount, @t_complete / (@mycount * 1.0)


Results:
IntDivision DecimalDivision
0 0.03350574936400




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ccmret
ccmret
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 94
That worked.....I ended up doing this:
Declare @Denominator int, @NumeratorT int, @NumeratorT1 int
SET @Denominator = (SELECT COUNT(*) FROM TestScores.dbo.Analysis)
SET @Numerator = (SELECT SUM(t_complete) TestScores.dbo.Analysis)
SET @Numerator = (SELECT SUM(t1_complete) TestScores.dbo.Analysis)

SELECT Distinct LEFT(CONVERT(DateTime, IndexDate , 110), 11)AS IndexDate,
COUNT(*) AS No_Records,
Round(AVG(Test1),2) As Test1_AVG,
Round(AVG(Test2),2) AS Test2_AVG,
SUM(t_complete/COUNT (*)) As Percent_Complete
SUM(t_complete) As Initial_Completed,
CONVERT(Decimal(5,3),@NumeratorT/(@Denominator* .01)) As PercentComplete,
SUM(t1_completed)AS Repeats_Completed,
CONVERT(Decimal(5,3),@NumeratorT1/(@Denominator*.01)) AS Repeats_Percent,
FROM TestScores.dbo.Analysis
Group by IndexDate


Thoughts?
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