Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 6th, 2007, 04:23 PM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding COUNTs

I want to add the COUNT results of two statements. The problem I'm having is the WHERE clause used for each COUNT uses the same field, but has different criteria. How would I add the COUNTs based on different criteria?

SELECT COUNT(DISTINCT(mxssn))
FROM mx
WHERE mxtots > 0 AND mxssn <> '999-99-9999'

SELECT COUNT(member)
FROM mx
WHERE mxssn = '666-66-6666'
Reply With Quote
  #2 (permalink)  
Old February 6th, 2007, 04:30 PM
Registered User
 
Join Date: Jan 2007
Location: Scottsdale, AZ, .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You said "I want to Add"

 A) Are you inserting 2 different resluts into another table?
 B) Have you tried to UNION these 2 scripts?
 C) Or am I completely missing the picture?

Regards,
Daniel

Reply With Quote
  #3 (permalink)  
Old February 6th, 2007, 05:06 PM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's one way of doing it:

SELECT A.DistinctCount + B.MemberCount
FROM
(SELECT COUNT(DISTINCT(mxssn)) AS DistinctCount
FROM mx
WHERE mxtots > 0 AND mxssn <> '999-99-9999') A,
(SELECT COUNT(member) AS MemberCount
FROM mx
WHERE mxssn = '666-66-6666') B

Here's another way of doing it:

SELECT SUM(RecordCount)
FROM (
SELECT COUNT(DISTINCT(mxssn)) AS RecordCount
FROM mx
WHERE mxtots > 0 AND mxssn <> '999-99-9999'
UNION ALL
SELECT COUNT(member) AS RecordCount
FROM mx
WHERE mxssn = '666-66-6666') A

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
Reply With Quote
  #4 (permalink)  
Old February 6th, 2007, 05:16 PM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I want to put a single total of the two into another table. sshelper's statement does what I need.

Thanks!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Counts hugh@kmcnetwork.com SQL Language 2 October 27th, 2004 04:01 PM
adding counts to search results gilgalbiblewheel Classic ASP Databases 0 August 6th, 2004 11:35 AM
join two counts field erin SQL Language 2 October 29th, 2003 04:50 AM
How to HavePrint Counts amar Pro VB 6 0 July 13th, 2003 10:05 AM
Get Counts harpua Classic ASP Databases 3 June 10th, 2003 12:45 AM



All times are GMT -4. The time now is 06:55 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.