max(case when ...) syntax issue - mysql

select
complaint_id
,complaint_type
,COMMUNICATION_ID
,max(case when delivery_type='deliver_once' then '1' else '0' end as IS_ADDON_REFUND)
from
complaints_order_status
group by 1,2,3
what's the issue with it?

You can't name your column inside the MAX function. Also use integers for 1 and 0. Also, if you could paste in the error message you're getting it would be helpful. But try:
select complaint_id ,
complaint_type ,
COMMUNICATION_ID ,
max(case when delivery_type='deliver_once' then 1 else 0 end) as IS_ADDON_REFUND
from complaints_order_status
group by complaint_id ,complaint_type ,COMMUNICATION_ID

I have changed two things from your code:
Use the alias in Group by. 1,2,3 don't work on all platforms.
Give alias outside max clause bracket. There is no need to give a name to
case when clause when this is not your final output.
Here is the final code:
select complaint_id ,complaint_type ,COMMUNICATION_ID ,
max(case when delivery_type='deliver_once'
then '1' else '0'
end )as IS_ADDON_REFUND
from complaints_order_status
group by complaint_id,
complaint_type,
COMMUNICATION_ID

Related

Vertical column result to horizontal

I need put a column result in to horizontal position, like SUM(CASE WHEN status = 'On Hold' THEN 1 ELSE 0 END) AS 'word' do, I tried a subquery and alias AS too but I didn't have had success. I think that JOIN is to multiple tables, and UNION to get results in same column.
I'm using this page as reference
https://www.mysqltutorial.org/mysql-case-function/
mysql Table: phpvms_pireps
pilotid|flightnum|submitdate|accepted
My sql code:
SELECT DISTINCT `phpvms_pireps`.`pilotid`
, `phpvms_pireps`.`accepted`
, `phpvms_pireps`.`flightnum`
, `phpvms_pireps`.`submitdate`
FROM phpvms_pireps
WHERE ((`phpvms_pireps`.`flightnum` in ('A-1', 'A-2', 'A-3', 'A-4')))
AND submitdate BETWEEN '2020-04-09' AND '2020-04-11'
ORDER
BY `phpvms_pireps`.`pilotid` ASC
The result:
And I want to achieve this:
I'm using php/html, How I can get that result?
Thank you very much!
You are describing a pivot table. Consider using conditional aggregation:
select
pilotid,
max(case when flightnum = 'A-1' then accepted end) a1,
max(case when flightnum = 'A-2' then accepted end) a2,
max(case when flightnum = 'A-3' then accepted end) a3,
max(case when flightnum = 'A-4' then accepted end) a4
from phpvms_pireps
where
flightnum in ('A-1', 'A-2', 'A-3', 'A-4')
and submitdate between '2020-04-09' and '2020-04-11'
group by pilotid
order by pilotid

Not able to alias column name in nested case using sql query

In this code its able to alias each case statement separately like,
SELECT
id,
SUM(CASE
WHEN (a.place = 'CHN' AND a.salary = 20000)
THEN '1'
ELSE '0'
END) AS '20K Salary',
SUM(CASE
WHEN (a.place = 'CHN' and a.salary = 35000)
THEN '1'
ELSE '0'
END) AS '35K Salary'
FROM Employee a;
but when nested CASE statement is used,
SELECT
id,
SUM(CASE
WHEN (a.place = 'CHN')
THEN (CASE
WHEN a.salary = 20000
THEN '1'
ELSE '0'
END) AS '20K Salary',
(CASE
WHEN a.salary = 35000
THEN '1'
ELSE '0'
END) AS '35K Salary'
END)
FROM Employee a;
its not possible to execute the query
In the first set of code each case statement is part of the SELECT list (i.e. they are in a comma separated list following SELECT) so each returns a column. In the second set of code there is only one case statement in the SELECT list, the fact that it is nested has no impact on this. Also the syntax for the nested case isn't correct because the THEN part is followed by two expressions separated by commas which is not allowed.
Of course this is possible to do what you want. You want to create two columns, so each needs its own logic and its own alias:
SELECT id,
SUM(CASE WHEN a.place = 'CHN' AND a.salary = 20000 THEN 1 ELSE 0
END) as Salary_20K,
SUM(CASE WHEN a.place = 'CHN' AND a.salary = 35000 THEN 1 ELSE 0
END) as Salary_35K
FROM Employee a;
Note: Don't put numeric constants in single quotes. Only use single quotes for string and date constants.

Reduce the number of queries

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;

MYSQL - Showing status input as it's own column

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

get count of two table fields in one query

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.