|other|last|first|program|
|cat|Acker|Bob A|
|dog|Acker|Bob|B|
|reptile|Acker|Bob|A|
|cat|Birch|Patty|B|
|bunny|Calhoun|Peter|C|
BECOMES
|other|last|first|program|
|cat|Acker|Bob A|
|dog|Acker|Bob|B|
|cat|Birch|Patty|B|
|bunny|Calhoun|Peter|C|
How do you get the above to work? I'm a sql novice. I'm trying to get a total count of records by one field based on certain values in other fields but not others. In the above Tables, I basically want sql to pull out reptile Acker Bob A, because A is already represented as a program by Bob. But I don't want the following removed for Bob because these programs are represented exactly once:
cat Acker Bob A
dog Acker Bob B
I've tried this but I can't get it to work:
SELECT program, total, sum(program) as allTotal
FROM mytable
GROUP BY program
HAVING count(program) <= 1
I want my end result to look like this:
program total allTotal
A 2 5
B 1 5
C 1 5
Is this what you want?
select
program,
count(*) total,
(select count(*) from mytable) all_total
from mytable
group by program
It sounds like you want
SELECT program, COUNT(distinct CONCAT(first, last)) AS total, (SELECT COUNT(*) FROM mytable) AS allTotal
FROM mytable
GROUP BY program
By only counting the distinct CONCAT(first, last) it will not count Bob twice in Program A.
The equivalent for some DBs without CONCAT
:
SELECT program, COUNT(distinct first||last) AS total, (SELECT COUNT(*) FROM mytable) AS allTotal
FROM mytable
GROUP BY program
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.