I'm doing the select:
select id,
status,
count(status) as qtd
from user
group by id, status;
The return is:
id | status | qtd
1 YES 5
1 NO 3
2 YES 3
2 NO 1
I want this:
id | YES | NO
1 5 3
2 3 1
Thanks.
NOTE:
you can use case logic to do what you want.. basically you want to pivot the results and to pivot them you have to use aggregates with conditionals to fake a pivot table since mysql doesn't have a way to accomplish that
QUERY:
SELECT
id,
SUM(CASE status WHEN 'Yes' THEN 1 ELSE 0 END) as 'YES',
SUM(CASE status WHEN 'No' THEN 1 ELSE 0 END) as 'NO'
FROM user
GROUP BY id;
DEMO
OUTPUT:
+----+-----+----+
| id | YES | NO |
+----+-----+----+
| 1 | 5 | 3 |
| 2 | 3 | 1 |
+----+-----+----+
You can do so,using expression in sum() like sum(status ='Yes') will result as boolean (0/1) and thus you can have your count based on your criteria you provide in sum function
select id,
sum(status ='Yes') as `YES`,
sum(status ='No') as `NO`
from user
group by id;
Related
So I have a table like this:
________________________________________
phone_number | phone_vendor | status
67834 | A | OPEN
23836 | B | OPEN
87483 | C | CLOSED
72377 | A | OPEN
63637 | B | OPEN
I need to find the count of phone_numbers of each unique phone_vendor in OPEN status, something like:
A | 2
B | 2
C | 0
using group by I don't get the vendors whose OPEN count is already exhausted (as C in above example)
What could be a suitable query?
Also, I can't use CASE as there will be multiple vendors.
Query attempt:
SELECT status, count(phone_number) AS count
FROM phone_number_table
WHERE status = 'OPEN'
GROUP BY phone_vendor, status
Try this:
SELECT SUM(CASE WHEN status = 'OPEN' THEN 1 ELSE 0 END) AS count, phone_vendor
FROM yourtable
Group by phone_vendor
SQL Demo: http://sqlfiddle.com/#!9/17e753/4
You could check for status and sum
select phone_vendor, sum(case status when 'OPEN' then 1 else 0 end) my_count
from my_table
group by phone_vendor
If I have a table of users:
USER ID | GENDER | VOTE
----------------------------------
1 Male Yes
2 Female No
3 Male No
4 Male Yes
5 Female Yes
How can I write a query that outputs the genders by their vote breakdown:
GENDER | YES | NO
----------------------------------
Male 2 1
Female 1 1
Is this possible to do it in SQL or should I use PHP to build these totals?
Any help is appreciated! :)
This type of transformation of data is known as pivot. MySQL doesn't have a PIVOT function but you can replicate the function by using an aggregate function with a CASE expression:
select gender,
sum(case when vote = 'Yes' then 1 else 0 end) Yes,
sum(case when vote = 'No' then 1 else 0 end) No
from yourtable
group by gender;
See SQL Fiddle with Demo. Since you are using MySQL you could also use the IF() control-flow operator:
select gender,
sum(if(vote = 'Yes', 1, 0)) Yes,
sum(if(vote = 'No', 1, 0)) No
from yourtable
group by gender;
See SQL Fiddle with Demo. These both give a result:
| GENDER | YES | NO |
|--------|-----|----|
| Female | 1 | 1 |
| Male | 2 | 1 |
I'm currently trying to make a mysql query that will count the number of zeros and ones per item, in the following way:
Table:
ID | PollID | Value
------------------------------------
1 | 1 | 1
2 | 1 | 1
3 | 2 | 0
4 | 2 | 1
5 | 1 | 0
And the result I want is:
Poll | one | zero
----------------------------------
1 | 2 | 1
2 | 1 | 1
Thanks for the help!
This is the shortest possible answer in MySQL because it supports boolean arithmetic.
SELECT PollID,
SUM(value = 1) AS `One`,
SUM(value = 0) AS `Zero`
FROM tableName
GROUP BY PollID
SQLFiddle Demo
select z.pollid,z.ones,s.zeros
from (select a.pollid,count(a.value) as ones from test a
where a.value=1
group by a.pollid) z
left join
(select b.pollid,count(b.value) as zeros from test b
where b.value=0 group by b.pollid) s
on z.pollid=s.pollid;
try this
select table.pollid,
Switch(table.value Like 1, 1)AS one,
Switch(table.value Like 0, 1)AS zero
from table
group by pollid
cusID | Name | status | Date
---------------------------------
1 | AA | 0 | 2013-01-25
2 | BB | 1 | 2013-01-23
3 | CC | 1 | 2013-01-20
SELECT COUNT(cusID) FROM customer WHERE STATUS=0;
SELECT COUNT(cusID) FROM customer WHERE STATUS=1;
Is there a way of combing such two sql and return the results as one. Because want to avoid calling to DB everytime. I tried UNION of two statments, but only showing one result.
This is the shortest possible solution in MySQL.
SELECT SUM(status = 1) totalActive,
SUM(status = 0) totalInactive
FROM tableName
SQLFiddle Demo
and this is the CASE version
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) totalActive,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) totalInactive
FROM tableName
SQLFiddle Demo
I have this table [Table 1]
cid | arrived | date_arrived
The [arrived field can have a value of [T] or [F], the value is [F] the date arrived field is NULL
1 records may appear only up to maximum of 2 (1 record for arrived=T and another record for arrived=F) But there are also records that may appear only once
1 | T | 2012-02-01
2 | F | [Null]
1 | F | [Null]
3 | T | 2012-03-05
I need a query that will show something like this
cid | arrived | not_arrived
1 Yes Yes
2 No Yes
3 Yes No
This works:
SELECT
cid,
SUM(arrived = 'T') > 0 as arrived,
SUM(arrived = 'F') > 0 as not_arrived
FROM [Table 1]
GROUP BY cid;
You can try it here: http://sqlfiddle.com/#!2/2b5a7/1/0
try
select cid,
case when find_in_set('T', group_concat(arrived)) > 0 then 'yes' else 'no' end as arrived,
case when find_in_set('F', group_concat(arrived)) > 0 then 'yes' else 'no' end as not_arrived
from table1
group by cid