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.
Related
I want to select the sum of T_No where Transactions are equal to R and subtract it by T_No where Transactions are equal to D and the answer of this should greater than zero for a CustomerID which would be a input (an int input declared in a stored procedure)
((Sum(T_No) where Transactions = R - Sum(T_No) where Transactions = D ) > 0) where CoustomerID = #input
Example : for ID = 1 it would be ((20+15) - 10) > 0
I Have tried so many things but either syntax is wrong, wrong value or it does not accept, and I am literally Stuck, this was my final attempt
SELECT
(select ( select Sum(T_No) where Transactions = R) - (select Sum(T_No) where Transactions = D) as C_T )
FROM CustomerTrans WHERE C_T > 0 ;
Conditional aggregation should help:
SELECT
SUM(CASE WHEN Transaction = 'R' THEN t_no ELSE 0 END) - SUM(CASE WHEN Transaction = 'D' THEN t_no ELSE 0 END)
FROM CustomerTrans
WHERE CoustomerID = #yourCustomerIdVariable
As you're writing a sproc you can assign the result of this to a variable and then decide what to do if the result is negative. (I would personally log an error for example, rather than just hide those results). If the result is null, then there were no transactions for that customer
ps; I used Transaction because that's what your screenshot showed, and I figured a screenshot is less likely to contain a typo than code with syntax errors. Adjust if required
you where kinda close, I would sum like you, only the syntax is a bit off, you can't have aggregate fields in Where, thats why you should use having, also case when syntax is not correct.
Select
CoustomerID,
Sum(case when Transactions = 'R' then T_No else 0 end) -
Sum(case when Transactions = 'D' then T_No else 0 end) as C_T
FROM CustomerTrans
group by CoustomerID
having (Sum(case when Transactions = 'R' then T_No else 0 end) -
Sum(case when Transactions = 'D' then T_No else 0 end))>0
I have a table of phone call activity for a client. In the table, I have one column for the length of the call (in seconds), and another column for "first time call" (true / false). I was hoping to find a way to get the average call length of first time calls separated from the average time of non first time calls? Is this doable in a singe mySQL query?
SELECT location,
count(*) AS total,
sum(case when firstCall = 'true' then 1 else 0 end) AS firstCall,
sum(case when answered = 'Yes' then 1 else 0 end) AS answered,
sum(case when tags like '%Lead%' then 1 else 0 end) as lead,
sum(case when tags like '%arbage%' then 1 else 0 end) as garbage,
avg(case when duration........firstTime = True???)
FROM staging
GROUP BY location
SELECT location,
count(*) AS total,
sum(case when firstCall = 'true' then 1 else 0 end) AS firstCall,
sum(case when answered = 'Yes' then 1 else 0 end) AS answered,
sum(case when tags like '%Lead%' then 1 else 0 end) as lead,
sum(case when tags like '%arbage%' then 1 else 0 end) as garbage,
sum(case when firstCall='true' then duration else 0 end)/sum(case when firstCall = 'true' then 1 else 0 end) as first_call_true_average,
sum(case when firstCall='false' then duration else 0 end)/sum(case when firstCall = 'false' then 1 else 0 end) as first_call_false_average
FROM staging
GROUP BY location
I would phrase this as:
select
location,
count(*) as total,
sum(firstcall = 'true' ) as cnt_firstcall,
sum(answered = 'Yes' ) as cnt_answered,
sum(tags like '%Lead%' ) as cnt_lead,
sum(tags like '%arbage%') as cnt_garbage,
avg(case when firstcall = 'true' then duration end) as avg_first_call,_duration
avg(case when firstcall = 'false' then duration end) as avg_non_first_call_duration
from staging
group by location
Rationale:
MySQL interpret true/false conditions as 1/0 values in numeric context, which greatly shortens the conditional sum()s
avg() ignores null values, so a simple case expression is sufficient to compute the conditional averages
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.
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
How do I get the Total value of Yes, No, Other fields of each username?
I like to add Total field.
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE NULL END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE NULL END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE NULL END) as Other
//How to get total of Yes/No/Other
FROM table
WHERE source = 'CompanyName' ";
Also the highest Total goes at the top order.
use 0 instead of NULL, add the missing group by and use COUNT(*) to get the total of each group and order the result:
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE 0 END) as Other,
COUNT(*) as TOTAL
FROM table
WHERE source = 'CompanyName'
group by Username
order by TOTAL desc;
This assumes that type can only be 'Yes', 'No' or ''.
Use a sub query to sum up your results and add a sort:
select yes, no, other, yes + no + other as Total
from (
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE 0 END) as Other
FROM table
WHERE source = 'CompanyName'
)
order by (yes + no + other) desc
Don't use SUM(null), the SUM of (1,1,1,null) = null, not 3.
SELECT s.*, s.yes+s.no+s.other as all FROM (
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE 0 END) as Other
FROM table
WHERE source = 'CompanyName'
GROUP BY Username
) s
ORDER BY all DESC