Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-09
5
Medium Priority
?
250 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:tfcallahan
  • 3
  • 2
5 Comments
 

Author Comment

by:tfcallahan
ID: 17909887
p.s. The results should look something like:

FK_ID   Total    Rejected   Reviewed
1234    6          1             5
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17909892
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

0
 
LVL 29

Accepted Solution

by:
Nightman earned 2000 total points
ID: 17909924
SElECT x as FK_ID,
(SELECT COUNT(*) FROM mytable WHERE FK_ID=x) as Total,
(SELECT COUNT(*) FROM mytable WHERE FK_ID=x AND REVIEWED=1) as Rejected,
(SELECT COUNT(*) FROM mytable WHERE FK_ID=x AND REJECTED=1) as Reviewed
0
 

Author Comment

by:tfcallahan
ID: 17909955
I just had to swap the Reviewed and Rejected expression labels and works like a charm.
Thanks!
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17909973
lol - tired eyes. Must be bed time ;)

Glad to help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question