Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

COUNT Expand / Collapse
Author
Message
Posted Wednesday, January 8, 2014 9:56 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 8:15 AM
Points: 229, Visits: 693
This is what I need to actually count.
COUNT(*)round
from CTE1
where
( (Cte1.Roundtables = '1' OR Cte1.Roundtables= 'N/A')
AND
(CTE1.IsVideo = '1' OR CTE1.IsVideo = 'N/A'))


SO, I am trying to count the # of Roundtables which have either a number or 'N/A' ( mentioned in the CTE)
and the count of IsVideo if its a number OR ;N/A.
and then do an AND on both their respective counts.

Kindly advise
Post #1528968
Posted Wednesday, January 8, 2014 10:01 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 8:15 AM
Points: 229, Visits: 693
Let me mention that when I individually execute without the AND part for each , I get the count as 3 and 8 respectively .
I am expecting the answer to be 3 but the and brings 0
Post #1528969
Posted Wednesday, January 8, 2014 10:08 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 12,995, Visits: 12,414
sharonsql2013 (1/8/2014)
This is what I need to actually count.
COUNT(*)round
from CTE1
where
( (Cte1.Roundtables = '1' OR Cte1.Roundtables= 'N/A')
AND
(CTE1.IsVideo = '1' OR CTE1.IsVideo = 'N/A'))


SO, I am trying to count the # of Roundtables which have either a number or 'N/A' ( mentioned in the CTE)
and the count of IsVideo if its a number OR ;N/A.
and then do an AND on both their respective counts.

Kindly advise


Pretty sparse on details here. Remember we can't see you screen. However I suspect you are getting 0 because your logic seems a little suspect to me. You must have a row that that RoundTables = 1 or N/A AND IsVideo = 1 or N/A.

This is the same thing as RoundTables in ('1', N/A') AND IsVideo in ('1', 'N/A'). I have a feeling that is not actually what you want though.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1528973
Posted Wednesday, January 8, 2014 10:23 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 8:15 AM
Points: 229, Visits: 693
Yes so I am counting RoundTables in ('1', N/A') AND IsVideo in ('1', 'N/A').
and then need to do an and between these to counts.

Should I separately count and then add ?
Post #1528983
Posted Wednesday, January 8, 2014 10:45 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 12,995, Visits: 12,414
sharonsql2013 (1/8/2014)
Yes so I am counting RoundTables in ('1', N/A') AND IsVideo in ('1', 'N/A').
and then need to do an and between these to counts.

Should I separately count and then add ?


Wouldn't changing the AND to OR solve the issue?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1528996
Posted Wednesday, January 8, 2014 1:16 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 2:47 AM
Points: 1,031, Visits: 6,730
Sean Lange (1/8/2014)
sharonsql2013 (1/8/2014)
Yes so I am counting RoundTables in ('1', N/A') AND IsVideo in ('1', 'N/A').
and then need to do an and between these to counts.

Should I separately count and then add ?


Wouldn't changing the AND to OR solve the issue?


I reckon Sean's nailed it here. Try running this, it will tell you:

SELECT 
Roundtables,
IsVideo,
COUNT(*)
FROM CTE1
WHERE Cte1.Roundtables IN ('1', 'N/A')
OR IsVideo IN ('1', 'N/A')
GROUP BY Roundtables, IsVideo




Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1529073
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse