I have done some research but still couldnt solve my issue here.
I have this table in next image link:
Table Sample
Im trying to write a query so that I can get something like this image link:
Sample I need
I did try write a query as below :
select ticket_pic, case when status = 'open' then count(tickets_id) end as open_ticket, case when status = 'close' then count(tickets_id) end as close_ticket from gt_tickets group by ticket_pic, status
But it return wrong data
Please advice. Any hint will be much appreciate.
Cheers
I might as well point out that the simplest syntax in MySQL is:
select ticket_pic,
sum(status = 'open') as open_ticket,
sum(status = 'close') as close_ticket
from gt_tickets
group by ticket_pic;
MySQL treats boolean expressions as integers, in a numeric context, with "0" for false and "1" for true.
select ticket_pic,
SUM(CASE WHEN status = 'open' THEN 1 ELSE 0 END) as open_ticket,
SUM(CASE WHEN status = 'close' THEN 1 ELSE 0 END) as close_ticket
from gt_tickets
group by ticket_pic
OR
select ticket_pic,
COUNT(CASE WHEN status = 'open' THEN 1 ELSE NULL END) as open_ticket,
COUNT(CASE WHEN status = 'close' THEN 1 ELSE NULL END) as close_ticket
from gt_tickets
group by ticket_pic
Related
I have the following query that I am trying to understand and convert
it to a db2 format:
TRANSFORM Sum(Cases) AS SumOfCases
SELECT Process, Sum(Cases) AS total
FROM tbl
GROUP BY Process
PIVOT tbl.STATUS;
Table has data like:
Process Status Cases
a Cancelled 14
a Closed 179
b Cancelled 20
b Closed 30
b Pending 10
How can I write that query to db2?
I tried the following query:
SELECT Process
, MAX(CASE WHEN STATUS = 'Cancelled' THEN CASES END) "Cancelled"
, MAX(CASE WHEN STATUS = 'Closed' THEN CASES END) "Closed"
, MAX(CASE WHEN STATUS = 'Pending' THEN CASES END) "Pending"
FROM tbl
GROUP BY Process;
Since I do not have MS Access hence I am not confident that if I had done the right thing in db2 or not.
Would appreciate if I could get some advice on this.
Your DB2 query works correctly in replicating Access's crosstab query except you missed the Total column. By the way any aggregate function would work for your CASE/WHEN statements: MIN(), MAX(), MEDIAN(), AVG(), even SUM():
SELECT Process
, SUM(CASES) AS "Total"
, MAX(CASE WHEN STATUS = 'Cancelled' THEN CASES END) AS "Cancelled"
, MAX(CASE WHEN STATUS = 'Closed' THEN CASES END) AS "Closed"
, MAX(CASE WHEN STATUS = 'Pending' THEN CASES END) AS "Pending"
FROM tbl
GROUP BY Process;
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 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;
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 wondering how to fix this query.
SELECT
SUM(`month_using`) used,
SUM(`month_cash`) paycash,
SUM(`month_profit` CASE WHEN `id` <> 'system' THEN 1 ELSE 0 END) profit
FROM `_using_month`
WHERE MONTH(`month_date`) = {month} AND YEAR(`month_date`) = {year}
Thanks
Use Group by clause to get multiple SUM:
For Example:
SELECT YEAR(month_date), MONTH(month_date),
SUM(month_using) used, SUM(month_cash) paycash,
SUM(month_profit CASE WHEN id <> 'system' THEN 1 ELSE 0 END) profit
FROM _using_month
GROUP BY YEAR(month_date), MONTH(month_date)