I have a table like this-
I want to run a query so that I can have a output like it-
I don't have a clear idea how to do it.
So, what I have done is-
SELECT
Count(* where status="Active") as count_active
Count(* where status="Inctive") as count_inactive
Count(* where status="Biase") as count_biase
FROM `subscribers`
And getting error, can anyone please help?
Can achieve this with a Case expression.
Query
select
count(case when status = 'Active' then 1 else null end) as Active_Count,
count(case when status = 'Inactive' then 1 else null end) as Inctive_Count,
count(case when status = 'Biase' then 1 else null end) as Biase_Count
from tbl_name;
SQL Fiddle
This is the way to put it in a single register:
SELECT
(SELECT count(id) FROM new_table where status = 'Active') as Active_Count,
(SELECT count(id) FROM new_table where status = 'Inactive') as Inctive_Count,
(SELECT count(id) FROM new_table where status = 'Biase') as Biase_Count;
Related
I want to get results for likes by a specific user-id and all likes and dislikes of a specific page id.
My structure looks like this:
`pages`: (id, title)
`pages_likes`: (id, page_id, uid, status)
If the status is -1 it's a dislike of a specific page, if it's 1 it's a like.
So, to get all likes this is my query:
SELECT COUNT(id) FROM pages_likes WHERE status = '1'
But now I also want to get if user-id 3 for example likes this page with
SELECT COUNT(id) FROM pages_likes WHERE status = '1' AND uid='3'
How can I achieve both in one query? I guess there has to be changed something right after the SELECT statement?
If you want to do this in a single query, use conditional aggregation:
SELECT SUM(CASE WHEN uid = '3' THEN 1 ELSE 0 END) AS threeLikes,
SUM(CASE WHEN uid <> '3' THEN 1 ELSE 0 END) AS otherLikes
FROM pages_likes
WHERE status = '1'
Another option would be to use a UNION, cf. the answer given by #bernie
Update:
If you want page likes and dislikes in the same query, you can try:
SELECT SUM(CASE WHEN uid = '3' AND status = '1' THEN 1 ELSE 0 END) AS threeLikes,
SUM(CASE WHEN uid <> '3' AND status = '1' THEN 1 ELSE 0 END) AS otherLikes
SUM(CASE WHEN uid = '3' AND status = '0' THEN 1 ELSE 0 END) AS threeDislikes,
SUM(CASE WHEN uid <> '3' AND status = '0' THEN 1 ELSE 0 END) AS otherDisikes
FROM pages_likes
I think this would work:
SELECT '' user, COUNT(id) likes
FROM pages_likes
WHERE status = '1'
GROUP BY 1
UNION ALL
SELECT uid user, COUNT(id) likes
FROM pages_likes
WHERE status = '1' AND uid='3'
GROUP BY 1
I would do it this way:
SELECT COUNT(*) AS total_likes, SUM(uid=3) AS uid3_likes
FROM pages_likes
WHERE status=1 AND page_id=1234
Re your comment:
Here's an example of showing total likes, and total dislikes. It's similar to the answer from #TimBiegeleisen.
SELECT SUM(status=1) AS total_likes,
SUM(CASE WHEN uid=3 AND status=1 THEN 1 END) AS uid3_likes
SUM(CASE WHEN uid=3 AND status=-1 THEN 1 END) AS uid3_dislikes
FROM pages_likes
WHERE page_id=1234
I have table t1:
id|id_title|action
The data in table:
1|1|like
2|1|like
3|1|unlike
4|2|share
5|2|share
So I want to get next result from query:
id|count like|count unlike|count share
1|2|1|0
2|0|0|2
I try to use next query:
SELECT id_title, ( Select Count(id) From T1 WHERE action='like') As CountOfItems FROM T1 GROUP BY id_title
But it return count of first row always. What I must do? Or maybe I must changed structure of table?
Just use conditional aggregation.
select
id_title
,sum(case when action='like' then 1 else 0 end) As Count_Like
,sum(case when action='unlike' then 1 else 0 end) As Count_Unlike
,sum(case when action='share' then 1 else 0 end) As Count_Share
FROM T1
GROUP BY id_title
SELECT id_title AS id, SUM(action='like') AS 'count like',
SUM(action='unlike') AS 'count unlike', SUM(action='share')
AS 'count share' FROM t1 GROUP BY id_title
Let me know if that worked for you.
I have seperate queries but i need to reduce the no so put all in one
select count(applicant_id) as registered from student_application where filter_status=0 AND
select count(applicant_id) as filer_select from student_application where filter_status=1 AND
select count(applicant_id) as filter_reject from student_application where filter_status=2
but this shows some errors
Use CASE expression.
Query
select
count(case when filter_status = 0 then applicant_id else null end) as registered,
count(case when filter_status = 1 then applicant_id else null end) as filer_select,
count(case when filter_status = 2 then applicant_id else null end) as filer_reject
from student_application;
SQL Fiddle
You could also use group_by, with the where clause if you're looking for a subset rather than all possible values of filter_status:
SELECT filter_status, COUNT(*)
FROM student_application
WHERE filter_status in (0, 1, 2)
GROUP BY filter_status;
I'm having trouble writing a SQL query to show the status of a row as it's own column as shown in the picture.
I was thinking of using an alias for Status as:
SELECT 'Table A.Date', 'Table A.Status' as ... FROM Table A;
But this doesn't resolve the issue on how to display each status type as their own column and number value.
Can someone point out how to do this?
Try this one. I used CASE statement to conditionally count the status as one depending on the status type given.
SELECT
Date,
SUM(CASE WHEN Status='Pending' THEN 1 ELSE 0 END)Pending,
SUM(CASE WHEN Status='Completed' THEN 1 ELSE 0 END)Completed,
SUM(CASE WHEN Status='Cancelled' THEN 1 ELSE 0 END)Cancelled
FROM Table A
WHERE Date='2014-01-01'
GROUP BY Date
Try this:
select A.date,
count(
case
when A.Status='Pending'
Then 1
Else NULL
End
) as Pending,
count(
case
when A.Status='Completed'
Then 1
Else NULL
End
) as Completed,
count(
case
when A.Status='Cancelled'
Then 1
Else NULL
End
) as Cancelled
From A
group by A.date
I am trying to get the count of females and males in the gender field of a table.
Is there a way to get the count of each in one query?
Something like:
select * from table count(where gender = 'm') as total_males, count(where gender = 'f') as total_females;
or will it require two queries?
select count(*) from table where gender = 'm';
select count(*) from table where gender = 'f';
This is basically a PIVOT. MySQL does not have a pivot so you can use an aggregate function with a CASE statement to perform this:
select
sum(case when gender = 'm' then 1 else 0 end) Total_Male,
sum(case when gender = 'f' then 1 else 0 end) Total_Female
from yourtable
See SQL Fiddle with Demo
Or using COUNT:
select
count(case when gender = 'm' then 1 else null end) Total_Male,
count(case when gender = 'f' then 1 else null end) Total_Female
from yourtable;
See SQL Fiddle with Demo
Something like this will work:
SELECT SUM(IF(t.gender='m',1,0)) AS total_males
, SUM(IF(t.gender='f',1,0)) AS total_females
FROM mytable t
The "trick" here is that we are using a conditional test to return either a 0 or a 1 for each row, and then adding up the 0's and 1's. To make this a little more clear, I am using the SUM aggregate function rather than COUNT, although COUNT could be used just as easily, though we'd need to return a NULL in place of the zero.
SELECT COUNT(IF(t.gender='m',1,NULL)) AS total_males
, COUNT(IF(t.gender='f',1,NULL)) AS total_females
FROM mytable t
Consider that the two expressions in the SELECT list of this query:
SELECT COUNT(1)
, SUM(1)
FROM mytable t
Will return the same value.
If you want to avoid the MySQL IF function, this can also be done using the ANSI SQL CASE expression:
SELECT SUM( CASE WHEN t.gender = 'm' THEN 1 ELSE 0 END )) AS total_males
, SUM( CASE WHEN t.gender = 'f' THEN 1 ELSE 0 END )) AS total_females
FROM mytable t
select sum(case when gender='m' then 1 else null end) as total_males, sum(case when gender='f' then 1 else null end) as total_females from ...
Should work just fine!
If your only issue is to avoid two queries, you can always write two queries as subselects of one query.
Select (select 1 from dual) as one, (select 2 from dual) as two from dual
This would work for your scenario, too.