Replacing CASE in SQL query - sql

I got three columns with binary values. Then i got a query which makes a string based on those values. I made this work using case. But it's quite huge (it's a part of a bigger query), and i was wondering maybe there is a better way of doing this?
SELECT (CASE
WHEN TableA.flag1 = 1 AND TableA.flag2 = 1 AND TableA.flag3 = 1 THEN 'CGR'
WHEN TableA.flag1 = 1 AND TableA.flag2 = 1 THEN 'CG'
WHEN TableA.flag1 = 1 AND TableA.flag3 = 1 THEN 'CR'
WHEN TableA.flag2 = 1 AND TableA.flag3 = 1 THEN 'GR'
WHEN TableA.flag1 = 1 THEN 'C'
WHEN TableA.flag2 = 1 THEN 'G'
WHEN TableA.flag3 = 1 THEN 'R'
ELSE 'nothing'
END)
FROM TableA
Im working on MSSQL 2000 server.

You can use left() instead.
select left('C', T.flag1)+
left('G', T.flag2)+
left('R', T.flag3)
from TableA as T

I don't know if this is "better" but it is more concise:
SELECT ((CASE WHEN TableA.flag1 = 1 THEN 'C' ELSE '' END) +
(CASE WHEN TableA.flag2 = 1 THEN 'G' ELSE '' END) +
(CASE WHEN TableA.flag3 = 1 THEN 'R' ELSE '' END)
)
FROM TableA;
Okay, this isn't exactly the same because you get '' instead of 'nothing'. But I think the empty string does a better job of representing "no flags" than 'nothing' does.

Try this:
SELECT
CASE WHEN flag1 = 1 THEN 'C' ELSE '' END +
CASE WHEN flag2 = 1 THEN 'G' ELSE '' END +
CASE WHEN flag3 = 1 THEN 'R' ELSE '' END
FROM TABLEA

Maybe you could use function and move this CASE statement to this function

Related

Column merge using sum in case Oracle APEX

I need help How can I merge the column into a single column, here is my code, is this method is correct. I want to get the count of the selected row in the table for the columns.
SELECT
CAT_MGR,
SUM ( case when CAT_MGR = 'A' THEN 1 else 0 end ) AS DESIGN,
sum (case when CAT_MGR = 'b' THEN 1 else 0 END) AS DESIGN,
sum (case when CAT_MGR = 'c' THEN 1 else 0 END) AS DESIGN
from Table_A
GROUP BY
CAT_MGR
Can you guys help me I'm a beginner at SQL.
Thank you in advance
If you want just one row in the resultset, then remove the group by clause. Then, if you want to count the three cat mgr together, you can use in:
select
sum(case when cat_mgr = 'a' then 1 else 0 end ) as design_a,
sum(case when cat_mgr = 'b' then 1 else 0 end ) as design_b,
sum(case when cat_mgr = 'c' then 1 else 0 end ) as design_c,
sum(case when cat_mgr in ('a', 'b', 'c') then 1 else 0 end ) as design
from Table_a
You just need to make addion like below in order to get one column "Design"
SELECT
CAT_MGR,
SUM (case when CAT_MGR = 'A' THEN 1 else 0 end )
+ sum (case when CAT_MGR = 'b' THEN 1 else 0 END)
+ sum (case when CAT_MGR = 'c' THEN 1 else 0 END)
AS DESIGN
from TJD_CORE_CATPB_TB
GROUP BY
CAT_MGR

Sql query to create following tables

use the below table 1 to generate table 2
Table 1
Col1 A B C
------------------
N1 1 0 0
N2 0 1 0
N3 1 0 0
Table 2
output
new_col
-------
N1 A
N2 B
N3 A
Also how to use Table 2 to generate table 1 above
Following SQL query can be help to get requested output:
SELECT Col1,IF(A=1,'A',IF(B=1,'B','C')) AS result FROM `table_name`;
You can use conditinal with CONCAT() Function :
SELECT CONCAT(col1,' ',
CASE WHEN A = 1 THEN 'A' ELSE '' END,
CASE WHEN B = 1 THEN 'B' ELSE '' END,
CASE WHEN C = 1 THEN 'C' ELSE '' END)
FROM table1
provided you're on a DBMS with brand name such as MySQL, PostGRES, SQL Server. As an example, Oracle DB won't allow using more than two arguments for CONCAT() Function.
In order to create table2, use for most of the DBMS :
CREATE TABLE table2 AS
SELECT CONCAT(col1,' ',
CASE WHEN A = 1 THEN 'A' ELSE '' END,
CASE WHEN B = 1 THEN 'B' ELSE '' END,
CASE WHEN C = 1 THEN 'C' ELSE '' END) AS new_col
FROM table1
except for SQL Server in which prefer using :
SELECT CONCAT(col1,' ',
CASE WHEN A = 1 THEN 'A' ELSE '' END,
CASE WHEN B = 1 THEN 'B' ELSE '' END,
CASE WHEN C = 1 THEN 'C' ELSE '' END) AS new_col
INTO table2
FROM table1
In order to implement a reverse engineering(go back to original table), you need to consider the dialectics for each seperate database to handle string values. Assume you're using MySQL DB, then consider using :
CREATE TABLE table3 AS
SELECT SUBSTRING(new_col,1,instr(new_col,' ')-1) AS col1,
CASE WHEN instr(new_col,'A') > 0 THEN 1 ELSE 0 END AS A,
CASE WHEN instr(new_col,'B') > 0 THEN 1 ELSE 0 END AS B,
CASE WHEN instr(new_col,'C') > 0 THEN 1 ELSE 0 END AS C
FROM table2
where I used a different table name (table3), since table1 already exists.
Demo
Btw, if Oracle DB is the case, then use :
SELECT CONCAT(CONCAT(col1,' '),
CASE WHEN A = 1 THEN 'A' ELSE '' END||
CASE WHEN B = 1 THEN 'B' ELSE '' END||
CASE WHEN C = 1 THEN 'C' ELSE '' END) AS new_col
FROM table1
You can use one case statement. This works in Oracle.
SELECT col1
|| CASE
WHEN A = 1
THEN 'A'
WHEN B = 1
THEN 'B'
WHEN C = 1
THEN 'C'
END
NEW_COL
FROM table1
It is unclear what you want when there are multiple "1"s in a row. This is a simple solution:
select id, A from t where A = 1
union all
select id, B from t where B = 1
union all
select id, C from t where C = 1;
In databases that support lateral joins, I would recommend:
select t.id, v.which
from t cross join lateral
(values ('A', t.A), ('B', B), ('C', C)
) v(which, val)
where val = 1;

My Sql print statement is not workinng

How do I get this to work? If the count is higher for singles i would like it to output yes and then no for viceversa.
IF
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "M"
<
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "S"
Print 'Yes'
ELSE
Print 'No';
You can't use IF inside a query, instead use a CASE expression with conditional aggregation:
SELECT
CASE WHEN SUM(CASE WHEN StudMaritalStatus = 'M' THEN 1 ELSE 0 END) <
SUM(CASE WHEN StudMaritalStatus = 'S' THEN 1 ELSE 0 END)
THEN 'Yes' ELSE 'No' END AS label
FROM students

How to use sum(case) with three conditions

I usually use sum(case) to get sum of some columns:
i.e. SUM(CASE WHEN over05 = 'OK' THEN 1 ELSE 0 END) AS OK_05
and this is perfect when I have a column with two values, but when I have a column where I have three values:
i.e. over 05 = '1' or 'X' or '2'
how can I do a sum(case)?
If you want all three values to return the same thing, you should use IN():
SUM(
CASE
WHEN over05 IN ('1', 'X', '2') THEN 1
ELSE 0 END
) AS OK_05
If you want each value to return something different, you should use multiple WHEN ... THEN :
SUM(
CASE
WHEN over05 = '1' THEN 1
WHEN over05 = 'X' THEN 2
WHEN over05 = '2' THEN 3
ELSE 0 END
) AS OK_05

Case sentence using SQL

I am using double case sentence to get a value from column in a table based on 2 conditions that are available in 2 other columns in same table , and else (otherwise) the function should give null when it is null or 0 when it is 0 .
Example of code is below :
CASE CODE
WHEN 'ABC'
CASE NAME WHEN 'XYZ'
THEN 'VALUE'
ELSE NULL
END
ELSE NULL
END
The problem is if I use NULL after else then it gives all ( both null values and values with 0 ) as NULL , or if I use 0 instead of NULL after ELSE then both null and 0 values are given as 0 .
I have tried to write the sentence in many ways but I dont know its not working . Hopefully somebody can give me some good solution regarding this.
CASE WHEN CODE = 'ABC' AND NAME = 'XYZ' THEN 'VALUE' ELSE NULL END
I believe you want:
CASE WHEN CODE = 'ABC' AND NAME = 'XYZ' THEN 'VALUE' ELSE NULL END
This structure of the CASE statement is more flexible than sticking the field name before WHEN.
Are you trying to do something like this?
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN 'VALUE'
when code = '0' or name = '0'
then '0'
ELSE NULL
end)
Or perhaps it is this (based on the fact that you are trying to get something from a column):
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN value
when value is NULL or value = 0
then value
ELSE NULL
end)
Based on your comment, I think this will work:
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN value
ELSE NULL
end)
Or is it this:
(CASE when CODE = 'ABC' and NAME = 'XYZ' and value <> '0'
THEN 'value'
when CODE = 'ABC' and NAME = 'XYZ' and value = '0'
then '0'
ELSE NULL
end)
However, I think this is equivalent to your original nested two-case version.