Re: Is There Easier Way to Calculate This Percentage in T-SQL?
From: jj (jjj.nosspam_at_mindspring.com)
Date: 05/30/04
- Next message: Alex Clark: "Re: Unusual SELECT Statement"
- Previous message: Bojidar Alexandrov: "Re: Unusual SELECT Statement"
- In reply to: David Portas: "Re: Is There Easier Way to Calculate This Percentage in T-SQL?"
- Next in thread: Partha Mandayam: "Re: Is There Easier Way to Calculate This Percentage in T-SQL?"
- Reply: Partha Mandayam: "Re: Is There Easier Way to Calculate This Percentage in T-SQL?"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 30 May 2004 13:39:30 GMT
David, thanks VERY MUCH for the tips. I see the advantage of COUNT over
SUM. I will try this. However, if I might ask a few more questions.
1. In order to account for "division by zero", shouldn't I start my SQL
with something like this (and I usually use the "convert(decimal, ...)"
when doing division). Does this syntax look OK?
SELECT CASE WHEN (countvar5 + countvar6) = 0 THEN 0
ELSE convert(decimal, (countvar1 + countvar2 + ....) /
(countvar5 + countvar6))
FROM
(SELECT
COUNT(CASE....(continued as indicated in your example below)
2. Will this one large SQL statement also provide the 6 values I need
for CountVar1 through CountVar6? In other words, using this "nested"
style, will I no longer need to have 6 seperate COUNT(CASE....)
statements, IN ADDITION TO the one you mentioned below, to get those 6
values for CountVar1 to CountVar6? If so, that will ensure SQL Server
is not repeating the calculations, as in my original code, just to get
the percentage, right?
3. Would storing these SQL statements in a STORED PROCEDURE make things
easier and/or faster? The only issue is that with all my SQL/queries, I
have DATE parameters. I know you can use parameters in STORED
PROCEDURES, but not sure if that's the best way to go here, or worth trying.
Thanks very much for your help & advice. I really appreciate your help
and expertise.
Gary.
==================
David Portas wrote:
> If you nest your query as a derived table then you can reference the
> calculated columns by their names:
>
> SELECT (countvar1 + countvar2 + ... ) / (countvar5 + countvar6)
> FROM
> (SELECT
> COUNT(CASE
> WHEN sale_date BETWEEN '#date1#' AND '#date2#'
> AND (this_var = 'Y' OR that_var = 'Y')
> AND other_var = ' '
> THEN 1 END) AS countvar1,
> COUNT(CASE
> WHEN sale_date BETWEEN '#date3#' AND '#date4#'
> AND (this_var = 'Y' OR that_var = 'Y')
> AND other_var = ' '
> THEN 1 END) AS countvar2,
> ...
> FROM YourTable) AS T
>
> In this query COUNT has the advantage over SUM that it will return 0 rather
> than NULL if no rows match the CASE statement. That way you don't need the
> ELSE clause.
>
- Next message: Alex Clark: "Re: Unusual SELECT Statement"
- Previous message: Bojidar Alexandrov: "Re: Unusual SELECT Statement"
- In reply to: David Portas: "Re: Is There Easier Way to Calculate This Percentage in T-SQL?"
- Next in thread: Partha Mandayam: "Re: Is There Easier Way to Calculate This Percentage in T-SQL?"
- Reply: Partha Mandayam: "Re: Is There Easier Way to Calculate This Percentage in T-SQL?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|