Re: Help with a query please



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
.



Relevant Pages

  • RE: "The operation was cancelled at the users request" error during mass insertions
    ... | I am doing the insertions in a for loop, ... | setNextUid(nextUID); //update nextUid in the table Uid ... The error message suggests that the query being executed is simply taking ...
    (microsoft.public.sqlserver.jdbcdriver)
  • Re: Bizarre subquery syntax problem
    ... With Access, the official syntax for a subquery always requires an alias, so ... SELECT p.AS UID, p.Firstname AS Firstname, p.Lastname AS Lastname, ... I am seeing bizarre problems with a subquery. ... core query runs fine- ...
    (microsoft.public.access.queries)
  • Re: Performance: Query optimizer producing sub-optimal result?
    ... Have you tried adding the query hint OPTION to your query ... Mark Ayliffe wrote: ... > SELECT UID, fnCheck ... > once per record in MyTable, not once per DISTINCT MainPart as I'd expected. ...
    (microsoft.public.sqlserver.programming)
  • Re: Performance: Query optimizer producing sub-optimal result?
    ... The simplified query you show as A. isn't a valid query, ... once per UID. ... select distinct CustomerID ... select MainPart, ...
    (microsoft.public.sqlserver.programming)
  • Re: Im confused
    ... Not sure if this causes the problem for you, and I wouldn't use a $_GET directly into a sql query, I would first check that the data is valid before inject it into the query statement. ... Affected Rows: -1 ... only the uid should be unique and a primary key. ... No, only the name column is unique, except for the id col, which is primary key and unique by default I believe ...
    (alt.php)