Solved

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

Posted on 2006-11-09
5
241 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now