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


Help getting total rows from outer select statement


Help getting total rows from outer select statement

Author
Message
loki1049
loki1049
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 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



loki1049
loki1049
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 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.
scott.pletcher
scott.pletcher
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 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
loki1049
loki1049
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 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.
loki1049
loki1049
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 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.
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