Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 WHERE statement for only one column

Author  Topic 

rjacko10
Starting Member

2 Posts

Posted - 2014-10-07 : 12:54:31
I use MS Access.

I know WHERE should come after FROM. But I don't want the WHERE to impact all columns on the table. So how can I handle this?

This is what I originally tried (I've had many more attempts):

SELECT Count(table1.causeDeath),
Count(table1.causeDeath) WHERE table1.causeDeath='Bleeding'
FROM table1
GROUP BY table1.sitename;

Thanks for your help :)

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-07 : 13:39:02
SELECT Count(table1.causeDeath),
Count(table1.causeDeath)
FROM table1
WHERE table1.causeDeath='Bleeding'
GROUP BY table1.sitename;
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2014-10-07 : 13:58:06
I am assuming you only want to count the cause of death - when that cause is 'Bleeding'. If so...

SELECT table1.sitename,
count(table1.causeDeath) As Total,
sum(Case When table1.causeDeath = 'Bleeding' Then 1 Else 0 End) As TotalBleeding
FROM table1
GROUP BY table1.sitename;

However, you might actually want this:

SELECT table.sitename,
table1.causeDeath,
count(*)
FROM table1
GROUP BY table1.sitename,
table1.causeDeath
WITH ROLLUP;
Go to Top of Page

rjacko10
Starting Member

2 Posts

Posted - 2014-10-08 : 10:54:47
Thank you jeffw8713. Although I believe this is for T-SQL. However your answer led me to the correct answer for MS Access which involves the 'iif' statement.
Go to Top of Page
   

- Advertisement -