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

Help getting total rows from outer select statement Expand / Collapse
Author
Message
Posted Friday, August 13, 2010 3:19 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 10, 2012 12:19 PM
Points: 103, Visits: 301
I have a view that I would like to query and get a percent ranking of a certain field, but I am having trouble trying to determine the number of rows that are going to be returned by my query to be used in the same query, so I don't have to hit this view twice, because it takes quite a while to read from. Below is some test data:

DECLARE @testdata TABLE
(
ExpDate DATETIME,
Employee nvarchar(10),
Contaminant nvarchar(10),
Reading float
)
INSERT INTO @testdata (ExpDate,Employee,Contaminant,Reading)
SELECT '2010-01-01','Dave','Alpha',0.276 UNION ALL
SELECT '2010-01-13','Dave','Alpha',0.356 UNION ALL
SELECT '2010-01-26','Dave','Alpha',0.158 UNION ALL
SELECT '2010-01-02','Dave','Betas',0.636 UNION ALL
SELECT '2010-01-18','Dave','Betas',0.215 UNION ALL
SELECT '2010-01-21','Dave','Betas',0.116 UNION ALL

SELECT '2010-01-02','Beth','Alpha',0.438 UNION ALL
SELECT '2010-01-13','Beth','Alpha',0.168 UNION ALL
SELECT '2010-01-24','Beth','Alpha',0.835 UNION ALL
SELECT '2010-01-01','Beth','Betas',0.756 UNION ALL
SELECT '2010-01-18','Beth','Betas',0.157 UNION ALL
SELECT '2010-01-27','Beth','Betas',0.384 UNION ALL

SELECT '2010-01-05','Mr.T','Alpha',0.834 UNION ALL
SELECT '2010-01-13','Mr.T','Alpha',0.453 UNION ALL
SELECT '2010-01-26','Mr.T','Alpha',0.758 UNION ALL
SELECT '2010-01-01','Mr.T','Betas',0.453 UNION ALL
SELECT '2010-01-18','Mr.T','Betas',0.135 UNION ALL
SELECT '2010-01-21','Mr.T','Betas',0.125

Now the first part of my query will sum the readings for each contaminant for each person between a specified date, and also give a total of the readings that person has in that time period. Its mainly a cross tab query, the code to run it would be this:

SELECT  
Employee,
AlphaExp = SUM(CASE WHEN Contaminant = 'Alpha' THEN Reading ELSE 0 END),
BetasExp = SUM(CASE WHEN Contaminant = 'Betas' THEN Reading ELSE 0 END),
TotalExp = SUM(Reading)
FROM
@TestData
WHERE
expdate between '2010-01-01' AND '2010-01-31'
GROUP BY
Employee

Now I have it in the form I want, but I want to assign a Percent Ranking and only show the top N percent, I could use the following code, but I need some way to show the 'XX%' for each record that is shown:

SELECT TOP 10 PERCENT
*
FROM

(
SELECT
Employee,
AlphaExp = SUM(CASE WHEN Contaminant = 'Alpha' THEN Reading ELSE 0 END),
BetasExp = SUM(CASE WHEN Contaminant = 'Betas' THEN Reading ELSE 0 END),
TotalExp = SUM(Reading)
FROM
@TestData
WHERE
expdate between '2010-01-01' AND '2010-01-31'
GROUP BY
Employee
)AS TEMP

ORDER BY TotalExp DESC

The only way I figure to do this would be using the Rank() OVER(ORDER BY TotalExp ASC) to get the rank, but then I need some way to get the total count of all the records, I've tried to use Count(*) OVER(PARTITION BY 1) and it seems to work, but is there a better way to do this? This is my working solution so far, I can further select the TOP 'N' percent either by using an expression in the WHERE clause, or ordering the results by the reading field and using the SELECT TOP 'N' PERCENT.

The other way to do it would be to use a CTE and hit the view once off the start with the exact same WHERE criteria and get the total count of records that will be returned, and then use it as a derived table, but this means reading from the view twice which nearly double execution time in my situation.

SELECT 
Employee,
AlphaExp,
BetasExp,
TotalExp,
[PercentRanking] = 100E0 * (rank() OVER(ORDER BY TotalExp ASC)) / (Count(*) OVER(PARTITION BY 1))
FROM

(
SELECT
Employee,
AlphaExp = SUM(CASE WHEN Contaminant = 'Alpha' THEN Reading ELSE 0 END),
BetasExp = SUM(CASE WHEN Contaminant = 'Betas' THEN Reading ELSE 0 END),
TotalExp = SUM(Reading)
FROM
@TestData
WHERE
expdate between '2010-01-01' AND '2010-01-31'
GROUP BY
Employee
) AS Temp





Post #969289
Posted Friday, August 13, 2010 3:27 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 10, 2012 12:19 PM
Points: 103, Visits: 301
Actually, after running this code, using the Window function divided by the window function requires a lot of processing power and time, so I'm going to decide against that, so still need to determine a solution if anyone has done this before.

p.s. placing @@rowcount in the outer select statement only returns the amount of rows read by the inner select statement, so that doesn't work either.
Post #969293
Posted Friday, August 13, 2010 4:31 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, December 31, 2010 9:46 AM
Points: 274, Visits: 473
Not exactly sure what you are basing "rank" and "percentage" on, but maybe you could combine your approaches and reduce the overhead enough to be acceptable:

SELECT  
Employee,
AlphaExp,
BetasExp,
TotalExp,
[PercentRanking] = 100E0 * (rank() OVER(ORDER BY TotalExp ASC)) / (Count(*) OVER(PARTITION BY 1))
FROM

(

SELECT TOP 10 PERCENT
*
FROM

(
SELECT
Employee,
AlphaExp = SUM(CASE WHEN Contaminant = 'Alpha' THEN Reading ELSE 0 END),
BetasExp = SUM(CASE WHEN Contaminant = 'Betas' THEN Reading ELSE 0 END),
TotalExp = SUM(Reading)
FROM
@TestData
WHERE
expdate between '2010-01-01' AND '2010-01-31'
GROUP BY
Employee
)AS TEMP

) AS derived

ORDER BY TotalExp DESC



Scott Pletcher, SQL Server MVP 2008-2010
Post #969309
Posted Friday, August 13, 2010 5:05 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 10, 2012 12:19 PM
Points: 103, Visits: 301
Tried the above mentioned solution already, it doesn't help.

I am pretty much just interested in a record and it associated 'TotalExp' I want to do a percentage ranking on this column. That is to say, I want the to have a way to see who has the 10% highest ranked values for 'TotalExp'. To calculate this manual that means you need:

A.) The total number of unique records (this is what I can get very efficiently.
B.) A numerical ranking of the 'TotalExp' for each record.

Once you have those two values, you can calculate the percent ranking by using the formula: ( Ranked-TotalExp / TotalRecordsUsed ) * 100

This is infact what using the TOP N PERCENT does when ordering by 'TotalExp' DESC does, but then the user cannot see what the Percent Ranking is for each individual record. There must be an easy way to do this?? I mean, using the TOP N PERCENT works fast, and its calculating these Percent Rankings just not displaying them, so there must be a way to do this in one easy step.
Post #969315
Posted Friday, August 13, 2010 5:11 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 10, 2012 12:19 PM
Points: 103, Visits: 301
After looking more into this, I almost wonder if it is something I should be getting the report to be doing. This is for a report after all, but I'm trying to simplify the steps by doing most of the hard and dirty work server side first.
Post #969317
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse