Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Sum then Divide Expand / Collapse
Author
Message
Posted Monday, April 1, 2013 10:50 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 6:31 AM
Points: 23, Visits: 93
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
Post #1437524
Posted Monday, April 1, 2013 10:59 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 9:10 AM
Points: 13,230, Visits: 12,709
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)
Post #1437532
Posted Monday, April 1, 2013 11:05 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 6:31 AM
Points: 23, Visits: 93
The query successfully completed with your suggestion. Howerver, it returned a zero.
Post #1437536
Posted Monday, April 1, 2013 11:07 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:09 AM
Points: 20,815, Visits: 32,749
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



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)
Post #1437537
Posted Monday, April 1, 2013 11:48 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 6:31 AM
Points: 23, Visits: 93
Still provides 0's. The numbers that I am trying to divide are:

t_complete = 176420
Count = 5265365

Format is tinyint.

Thoughts?


Post #1437551
Posted Monday, April 1, 2013 11:55 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:09 AM
Points: 20,815, Visits: 32,749
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?



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)
Post #1437555
Posted Monday, April 1, 2013 12:06 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 9:10 AM
Points: 13,230, Visits: 12,709
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)
Post #1437556
Posted Monday, April 1, 2013 12:06 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:09 AM
Points: 20,815, Visits: 32,749
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





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)
Post #1437557
Posted Monday, April 1, 2013 3:19 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 6:31 AM
Points: 23, Visits: 93
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?
Post #1437631
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse