is there any way to split single field value into multiple column - mysql

i have a situation where i supposed to display columnar fashion
im using this query
SELECT status, count(*) as total_
FROM companydata
WHERE status = 'Not Emailed' or status = 'Not Emailed' or status = 'Email Sent' or status = 'Response Received' or status = 'Interested' or status =
'Not Interested'
GROUP BY status
it return something like this
status total_
-|--------------------|--------------------------
Not Emailed 9562
| Interested | 2
Response Received 45
| Email Sent | 3000
is there any way to make them look columnar fashion
Not Emailed Interested Response Received Email Sent
9562 2 45 3000

Alter your original query to count conditionally
SELECT Count(CASE WHEN status = 'Not Emailed' THEN 1 END) AS `Not Emailed`,
Count(CASE WHEN status = 'Email Sent' THEN 1 END) AS `Email Sent`,
Count(CASE WHEN status = 'Response Received' THEN 1 END) AS `Response Received`,
Count(CASE WHEN status = 'Not Interested' THEN 1 END) AS `Not Interested`
FROM yourtable
WHERE status IN ( 'Not Emailed', 'Not Emailed', 'Response Received', 'Interested', 'Not Interested' )
Alternatively instead of COUNT we can use SUM aggregate as well
Sum(CASE WHEN status = 'Not Emailed' THEN 1 ELSE 0 END) AS `Not Emailed`

This is an elaboration of prdp's answer. In MySQL, I think the easiest method is:
SELECT SUM(status = 'Not Emailed') AS `Not Emailed`,
SUM(status = 'Email Sent') AS `Email Sent`,
SUM(status = 'Response Received') AS `Response Received`,
SUM(status = 'Not Interested') AS `Not Interested`
FROM yourtable
WHERE status IN ( 'Not Emailed', 'Not Emailed', 'Response Received', 'Interested', 'Not Interested' ) ;
CASE is also correct -- and portable to most databases. This is just a convenient short-hand in MySQL.

Related

MySQL CASE , SUM, GROUP BY

I want sum of payment status from two differnt columns based on paymentstatus value - but this query returns null for sum. Why is it not working?
select payment_status,
CASE
WHEN 'PAID' THEN sum(paid_amount)
when 'Not Paid' then sum(total_amount_due )
END
from monthly_fee
group by payment_status;
select sum(CASE WHEN payment_status = 'PAID' THEN paid_amount else 0 end) as paid,
sum(CASE WHEN payment_status = 'Not Paid' THEN total_amount_due else 0 end) as due
from monthly_fee
If you want this conditionally, you need to include the column in the case:
select payment_status,
(case payment_status
when 'Paid' then sum(paid_amount)
when 'Not Paid' then sum(total_amount_due )
end)
from monthly_fee
group by payment_status;
This seems like a strange way to write the query, unless you really want two rows.
Your WHEN clauses aren't a condition.
I'd expect to see something like
select payment_status,
CASE
WHEN payment_status = 'PAID' THEN sum(paid_amount)
when payment_status = 'Not Paid' then sum(total_amount_due )
END
from monthly_fee
group by payment_status;
You can try the following query:
select sum(if(payment_status = 'PAID', paid_amount, 0)
+ if(payment_status = 'Not Paid', total_amount_due, 0))
from monthly_fee
group by payment_status;

MySQL Merge rows into columns

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

MySQL: Case Query within a Case Query?

I have a query that gets unit count and it tagging if completed or not.
SELECT distinct location,
case when id is NULL then 'Not Started'
when '1' then 'Completed'
else 'In Progress' end as Remarks,
count(name) as CountName
FROM table
group by location,
case when id is NULL then 'Not Started'
when '1' then 'Completed'
else 'In Progress' end;
Result:
But I want to summarized this as below image:
Condition is when there are more that two(2) Remarks in Location it should be tagged as "In-progress" and sum the CountName. But when only one Remarks for a Location, gets the Remarks as its tagged.
Not sure on this (sample data would help), but try this:
SELECT Location,
case when count(id) > 1 then 'In Progress'
when max(id) is null then 'Not Started'
when max(id) = 1 then 'Completed'
else 'In Progress' end As Remarks,
count(name) as CountName
FROM table
GROUP BY location
Like you said, a case within a case:
select location,
case when count(distinct case when id is null then 'Not Started'
when id = '1' then 'Completed'
else 'In Progress' end) > 1
then 'In Progress'
else max(case when id is null then 'Not Started'
when id = '1' then 'Completed'
else 'In Progress' end)
end as remarks,
count(*) as CountName
from tbl
group by location
SQLFiddle Demo
Try This query:-
SELECT location,
if(count(1) > 1, 'In Progress', Remarks) Remarks,
sum(countName) countName
FROM location
group by location

MySQL: Count rows with conditional value

I want to know how much the user is login and how much they fail to and success to.
The category column is varchar (the stupidity of the ex programmer) while the value is only 1 or 0 only.
If 1 they success, if 0 it means fail.
$loginForced = " SELECT
Count(categori) AS login,
Count(CASE WHEN categori = '1' THEN categori ELSE 0 END) AS success,
Count(CASE WHEN categori = '0' THEN categori ELSE 0 END) AS fail
FROM
log
WHERE email = '".$personal."' ";
Use sum with condition:
$loginForced = "select count(`categori`) as `login`,
sum(`categori` = '1') as `success`,
sum(`categori` = '0') as `fail`
from `log`
where `email` = '".$personal."' ";
You probably want t just skip the ELSE in the CASE expression:
SELECT Count(categori) AS login,
Count(CASE WHEN categori = '1' THEN categori END) AS success,
Count(CASE WHEN categori = '0' THEN categori END) AS fail
FROM log
WHERE email = '".$personal."' ";
COUNT takes into account all non-null values, so using ELSE 0 is essentially pointless, since 0 is going to be counted just like any other value.

Compare columns from SUM(CASE...END) in the same query

I'm trying to add a column named Ready to my query that checks if the sum of Complete is = to the sum of Total and places 'Y' if True and 'N' if FALSE.
Here is my query that works producing 4 columns
SELECT pc.Sub,
SUM(CASE WHEN `SheetStatus` LIKE 'Complete' THEN 1 ELSE 0 END) AS 'Complete',
SUM(CASE WHEN `SheetStatus` LIKE 'Not started' THEN 1 ELSE 0 END) AS 'Not Started',
SUM(CASE WHEN `CheckSheet` LIKE '%' THEN 1 ELSE 0 END) AS 'Total'
FROM `pc`
Group By pc.Sub WITH ROLLUP
I just cant figure out how to create the extra column if it is possible at all.
Regards
Try outer SELECT
SELECT `Complete`,
`Not Started`,
`Total`,
CASE WHEN `Complete` = `Total` THEN 'Y' ELSE 'N' END `Ready`
FROM (
SELECT pc.Sub,
SUM(CASE WHEN `SheetStatus` LIKE 'Complete' THEN 1 ELSE 0 END) AS `Complete`,
SUM(CASE WHEN `SheetStatus` LIKE 'Not started' THEN 1 ELSE 0 END) AS `Not Started`,
SUM(CASE WHEN `CheckSheet` LIKE '%' THEN 1 ELSE 0 END) AS `Total`
FROM `pc`
GROUP BY pc.Sub WITH ROLLUP) t
Try this:
SELECT pc.Sub,
SUM(IF(`SheetStatus`='Complete',1,0) AS 'Complete',
SUM(IF(`SheetStatus`='Not started',1,0) AS 'Not Started',
SUM(IF(`CheckSheet` LIKE '%',1,0) AS 'Total'
IF(SUM(IF(`CheckSheet` LIKE '%',1,0) = SUM(IF(`SheetStatus`='Complete',1,0),"Y","N") AS Ready
FROM `pc`
Group By pc.Sub WITH ROLLUP