Solved

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

Posted on 2006-11-09
5
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
SQL Convert rows to columns 5 34
SQL syntax for max(date) 3 39
sql trace 4 29
Delete duplicates from SQL Server table 2 27
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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

752 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