Link to home
Start Free TrialLog in
Avatar of tfcallahan
tfcallahanFlag for United States of America

asked on

Query to get total counts of multiple bit fields in each row

I  have a table with the following fields

FK_ID - foriegn key ID
PK_ID - unique PK
REVIEWED - bit
REJECTED - bit

I would like a query that, for a particular FK_ID value, returns a count of:

a. total number of rows matching the FK_ID
b. total number of rows matching the FK_ID that have REVIEWED=1
c. total number rows matching the FK_ID that have REJECTED=1

I'm sure the answer is already out there but a search didn't find this specific case.

Thanks,

Tim
Avatar of tfcallahan
tfcallahan
Flag of United States of America image

ASKER

p.s. The results should look something like:

FK_ID   Total    Rejected   Reviewed
1234    6          1             5
SELECT 'Total', COUNT(*) FROM table WHERE FK_ID=x
UNION ALL
SELECT 'REVIEWED',COUNT(*) FROM table WHERE FK_ID=x AND REVIEWED=1
UNION ALL
SELECT 'REJECTED',COUNT(*) FROM table WHERE FK_ID=x AND REJECTED=1

ASKER CERTIFIED SOLUTION
Avatar of Nightman
Nightman
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just had to swap the Reviewed and Rejected expression labels and works like a charm.
Thanks!
lol - tired eyes. Must be bed time ;)

Glad to help.