Select more than 1 columns on condition - mysql

Tell please how can select 2 or more columns on condition? I trying this, but not works
SELECT
CASE
WHEN 1=1 THEN 'A', 'B'
ELSE 'C', 'D'
END
If trying select only 1 value
WHEN 1=1 THEN 'A'
ELSE 'C'
it works

Use two CASE expressions:
SELECT
CASE WHEN 1 = 1 THEN 'A' ELSE 'C' END,
CASE WHEN 1 = 1 THEN 'B' ELSE 'D' END

You need to write separately.
SELECT
IF(1=1, 'A', 'C'),
IF(1=1, 'B', 'D')

Related

MYSQL show case value even if the case does not exist in dababase

http://sqlfiddle.com/#!9/027e1e/19
sample dataset here:
CREATE TABLE IF NOT EXISTS `test` (
`mode` varchar(10),
`value` int(3)
);
INSERT INTO `test` (`mode`, `value`) VALUES
('xx-a', '1'),
('xx-a', '1'),
('xx-c', '2'),
('xx-d', '3');
SQL query here:
SELECT CASE
WHEN mode LIKE '%-a' THEN 'a'
WHEN mode LIKE '%-b' THEN 'b'
ELSE 'c'
END AS channel,
IFNULL(SUM(value), 0)
FROM test
GROUP BY channel
When the value '%-b' does not exist in the database, it returns only 2 rows with 'a' and 'c'.
I need it to return 3 rows for all a, b, and c where b shows 0.
Thanks
A solution would be to use union all:
select
'a' channel,
sum(case when mode like '%-a' then value else 0 end) sum_value
from mytable
union all select
'b',
sum(case when mode like '%-b' then value else 0 end)
from mytable
union all select
'c',
sum(case when mode not like '%-a' and mode not like '%-b' then value else 0)
from mytable
Try this please
SELECT
CASE
WHEN mode LIKE '%-a' THEN 'a'
WHEN mode LIKE '%-b' THEN 'b'
ELSE 'c'
END AS channel,
IFNULL(SUM(value), 0),
IFNULL(COUNT(value), 0)
FROM database GROUP BY channel
UNION
SELECT 'b',0,0 FROM dual
WHERE not exists
(SELECT 1 FROM database WHERE mode LIKE '%-b')

Query to Get Counts of Ids under 2 different Conditions with Case When Statement

So I Have a Table Structure like the one below.
Here are the Conditions:
I call 'Tag' as the IDs with none of Condition(x) as NULL.
I call 'UnTag' as the IDs with any of the Condition(x) is NULL.
I want to Classify 'Classification1' in the below Format:
I want the output in the following structure:
I wrote a code but somehow it is not giving me the desired result.
I Used Case when to Classify the Classification1 and then ran a sub-query for taking out Count.
Please help if there is a different Approach and how to incorporate the Case when in Group by statement.
I wrote this query:
SELECT
CASE
WHEN UPPER(CM1.Classification1) IN ('A', 'C','G') THEN 'X'
WHEN UPPER(CM1.Classification1) IN ('B', 'F') THEN 'Y'
WHEN UPPER(CM1.Classification1) IN ('D', 'E') THEN 'Z'
ELSE 'Undefined' END AS New_Classification,
(SELECT
COUNT(CASE
WHEN UPPER(CM2.Classification1) IN ('A', 'C','G') THEN 'X'
WHEN UPPER(CM2.Classification1) IN ('B', 'F') THEN 'Y'
WHEN UPPER(CM2.Classification1) IN ('D', 'E') THEN 'Z'
ELSE 'Undefined' END)
FROM TABLE1 CM2 WHERE
CM2.Condition1 IS NOT NULL AND
CM2.Condition2 IS NOT NULL AND
CM2.Condition3 IS NOT NULL AND
CM2.Condition4 IS NOT NULL AND
CM1.ID=CM2.ID) AS Count_of_tagged,
(SELECT
COUNT(CASE
WHEN UPPER(CM3.Classification1) IN ('A', 'C','G') THEN 'X'
WHEN UPPER(CM3.Classification1) IN ('B', 'F') THEN 'Y'
WHEN UPPER(CM3.Classification1) IN ('D', 'E') THEN 'Z'
ELSE 'Undefined' END)
FROM TABLE1 CM3 WHERE
(CM3.Condition1 IS NULL OR
CM3.Condition2 IS NULL OR
CM3.Condition3 IS NULL OR
CM3.Condition4 IS NULL) AND
CM1.ID=CM2.ID) AS Count_of_untagged
FROM TABLE1 CM1
You shouldn't need a subquery, as a starting point I came up with:
SELECT CASE Classification1
WHEN 'A' THEN 'X'
WHEN 'B' THEN 'Y'
WHEN 'C' THEN 'X'
WHEN 'D' THEN 'Z'
WHEN 'E' THEN 'Z'
WHEN 'F' THEN 'Y'
WHEN 'G' THEN 'X'
END new_classification,
SUM(
Condition1 IS NOT NULL AND
Condition2 IS NOT NULL AND
Condition3 IS NOT NULL AND
Condition4 IS NOT NULL /* 1 for true, 0 for false */
) tagged_count,
SUM(
Condition1 IS NULL OR
Condition2 IS NULL OR
Condition3 IS NULL OR
Condition4 IS NULL
) untagged_count,
FROM table_name
GROUP BY new_classification
Can give a try at the following sql and tell me if this is what you need
select
CASE
WHEN classification ='A' THEN 'X'
WHEN classification ='B' THEN 'Y'
WHEN classification ='C' THEN 'X'
WHEN classification ='D' THEN 'Z'
WHEN classification ='E' THEN 'Z'
WHEN classification ='F' THEN 'Y'
WHEN classification ='G' THEN 'X'
END as classification,
sum(case when condition1 is not null and condition2 is not null and condition3 is not null and condition4 is not null then 1 else 0 end) as tagged
sum(case when condition1 is null or condition2 is null or condition3 is null or condition4 is null then 1 else 0 end) as untagged
from table
GROUP BY CASE
WHEN classification ='A' THEN 'X'
WHEN classification ='B' THEN 'Y'
WHEN classification ='C' THEN 'X'
WHEN classification ='D' THEN 'Z'
WHEN classification ='E' THEN 'Z'
WHEN classification ='F' THEN 'Y'
WHEN classification ='G' THEN 'X'
END ;

MySQL converting enum values to integer values

I have a table which includes an ENUM field with values 'B', 'N', 'F', and 'V'. I would like to assign each of these letters a weight:
B: -2
N: -1
F: 1
V: 2
These weights should appear in a column in a select statement. Finally, I want to add these values together. Is this possible?
The goal is to do this in django, but using MySQL will also work.
Use a CASE expression:
select sum(case enum_field
when 'B' then -2
when 'N' then -1
when 'F' then 1
when 'V' then 2
end) as total_sum
from your_table
select sum(case when enum_field = 'B' then -2
when enum_field = 'N' then -1
when enum_field = 'F' then 1
when enum_field = 'V' then 2
end) as total_sum
from your_table

how to perform a count on a column where a value is X

is there a way of performing a count operation with SQL where the column equals X
and ideally a separate count for when the same column equals Y and again for Z?
SELECT yourcolumn, COUNT(*) AS cnt
FROM yourtable
WHERE yourcolumn IN ('X', 'Y', 'Z')
GROUP BY yourcolumn
Something like this ?
select
sum(case when val = 'x' then 1 else 0 end) as countX
,sum(case when val = 'y' then 1 else 0 end) as countY
,sum(case when val = 'z' then 1 else 0 end) as countZ
from table
I believe the following (shorter) statement should work for MySQL:
SELECT COUNT(val='X'), COUNT(val='Y'), COUNT(val='Z') FROM ...
I would suggest the following:
SELECT YourColumn, COUNT(<Any Column of your table>)
FROM YourTable
GROUP BY YourColumn

if else query in mysql

i need an example of nested if-else condition in mysql query
You could also use case statements for if-else conditions
SELECT
(CASE field1
WHEN 'A' THEN 'value is A'
WHEN 'B' THEN 'value is B'
ELSE 'value is neither A or B'
END)
FROM your_table;
or
SELECT
(CASE
WHEN (field1 IS NULL) THEN 'value is NULL'
WHEN (field1 = 1) THEN 'value is 1'
ELSE 'value is neither NULL or 1'
END)
FROM your_table;
You mean the IF(expr, expr, expr) function as defined here? An example would be:
SELECT
name, ID,
IF(category = 'fulltime', 1,
IF(category = 'parttime', loading, 0)) AS equivloading
FROM
person