Wednesday, March 21, 2012

Help with a query please

I have been strugling with for hours -
I have a table with the following columns
UID(decimal) | SD(bit) | MB(bit) | TS(bit) | Total(Decimal)
I want to run a query which will return
UID, Total if SD is True AND
UID, Total if MB is True AND
UID, Total if TS is True
UID is the userID so there would be a WHERE statement
ORDERED BY Total Descending
Many thanks in advance
Many thanks,
Rob
Xref: TK2MSFTNGP08.phx.gbl microsoft.public.sqlserver.mseq:9137
On Sun, 12 Mar 2006 03:17:27 -0800, RobA wrote:

>I have been strugling with for hours -
>I have a table with the following columns
>UID(decimal) | SD(bit) | MB(bit) | TS(bit) | Total(Decimal)
>I want to run a query which will return
>UID, Total if SD is True AND
>UID, Total if MB is True AND
>UID, Total if TS is True
>
>UID is the userID so there would be a WHERE statement
>ORDERED BY Total Descending
>Many thanks in advance
Hi Rob,
It's hard to tell exactly what you need from this description. If the
query below is not what you need, then please read www.aspfaq.com/5006
to find a better way to describe your problem.
SELECT UID,
SUM(CASE WHEN SD = CAST(1 AS bit) THEN Total ELSE 0 END),
SUM(CASE WHEN MB = CAST(1 AS bit) THEN Total ELSE 0 END),
SUM(CASE WHEN TS = CAST(1 AS bit) THEN Total ELSE 0 END)
FROM YourTable
GROUP BY UID
Hugo Kornelis, SQL Server MVP

No comments:

Post a Comment