SQL - Based on YES or NULL value, assign new value AND concatenate new values - sql-server-2012

I have six unique columns (let's just call them A, B, C, D, E, F) that all have either YES or NULL as the value. In a new column (let's call it NEW) I need to input the column name (A, B, C, D, E, F) if the value is YES. I also need these values to be concatenated, separated by a comma.
I've been playing around with SELECT/CASE/IF/THEN/ELSE statements, but haven't had any success.
Any guidance that can be offered is appreciated.

You can use CONCAT:
SELECT CONCAT( CASE WHEN A = 'YES' THEN 'A' END,
',' + CASE WHEN B = 'YES' THEN 'B' END,
',' + CASE WHEN C = 'YES' THEN 'C' END,
',' + CASE WHEN D = 'YES' THEN 'D' END,
',' + CASE WHEN E = 'YES' THEN 'E' END) NEW
FROM dbo.YourTable
;

Related

Order by -- different sequence for different criteria

I want to do something like this:
select a
from table
order by
case when a='A' then b,c,d
else d,c,b
a, b, c, d are all columns of the the table.
Your question is not that clear about the result that you really expect, bu I suspect that is:
order by
case when a = 'A' then b else d end,
c,
case when a = 'A' then d else b end
Or if you want records where a = 'A' first (with the specified order), and then the rest of the records (with the other sequence), then:
order by
case when a = 'A' then 0 else 1 end,
case when a = 'A' then b else d end,
c,
case when a = 'A' then d else b end

Suggested sequence of responses in query

I have such values in the letter column:
A, B, C, D, E, **X**.
I would like the select to return to me such an order of
A, B, **X**, C, D, E.
I tried with ORDER BY, but I don't know if it's a good way, or it should be SELECT Top 2 and next...
If it's only one character:
order by case when MyColumn < 'C' then 1
when MyColumn = 'X' then 2
else 3
end,
MyColumn
In this case you should assign a numeric value to each of the possible values in order to get them in the desired way. It could be something like
order by case when column = 'A' then 1
when column = 'B' then 2
when column = 'C' then 3
when column = 'X' then 4
...
else 99999999
end
This can be done using a case expression to re-position the 'X' between 'B' and 'C' as follows.
order by case when MyColumn = 'X' then 'BB' else MyColumn end

Conditional value in sqlite column

I have two integer columns in my sqlite table: a and b. I need to create a third column, c, which should contain either Y if a+b mod 2 == 1 or N if the previous condition is not satisfied. I am not sure how to define such a column with a conditional value in my query.
You can do this readily in a query:
select a, b, (case when (a + b) % 2 = 1 then 'Y' else 'N' end) as col3
from table t;
You can do this in an update statement as well:
update t
set col3 = (case when (a + b) % 2 = 1 then 'Y' else 'N' end) ;
You need to be sure that col3 exists. You can do that with alter table:
alter table t add column col3 int;

Sum of null columns in SQL

I have a table where A, B and C allow null values.
SELECT
A, B, C,
A + B + C AS 'SUM'
FROM Table
If my A, B and C values are 10, NULL and NULL, my SUM column shows nothing in it.
Is there any way to fix this, and display SUM as 10? OTHER THAN converting NULL s to Zeros?
You could use SQL COALESCE which uses the value in the column, or an alternative value if the column is null
So
SUM ( COALESCE(A,0) + COALESCE(B,0) + COALESCE(C,0))
In this case you need to use IsNull(Column, 0) to ensure it is always 0 at minimum.
SELECT
A, B, C,
IsNull(A,0) + IsNull(B,0) + IsNull(C,0) AS 'SUM'
FROM Table
ISNULL() determines what to do when you have a null value. if column returns a null value so you specified a 0 to be returned instead.

Identify columns with varying values for each set of columns

Given a table, t:
a b c d e
1 2 3 4 7
1 2 3 5 7
3 2 4 6 7
3 2 4 6 8
What SQL query can identify the columns that has one or more instances of varying values associated with each tuple from columns a and b, ?
In table t above, columns d and e would satisfy this criterion but not column c.
For tuples <1,2> and <3,2> that come from columns a and b, column c doesn't have varying values for each tuple.
Column d has one instance of varying values for tuple <1,2> -- values 4 and 5.
Column e also has one instance of varying values for tuple <3,2> -- values 7 and 8.
Something like this should work for you using CASE, COUNT and GROUP BY:
select
a, b,
case when count(distinct c) > 1 then 'yes' else 'no' end colc,
case when count(distinct d) > 1 then 'yes' else 'no' end cold,
case when count(distinct e) > 1 then 'yes' else 'no' end cole
from t
group by a, b
SQL Fiddle Demo
Slightly indirectly:
SELECT a, b,
COUNT(DISTINCT c) AS num_c,
COUNT(DISTINCT d) AS num_d,
COUNT(DISTINCT e) AS num_e
FROM t
GROUP BY a, b;
This yields:
1 2 1 2 1
3 2 1 1 2
If the num_c or num_d or num_e column has a value greater than 1, then there are varying values. You can vary the query to list whether the column is varying for a given value of (a, b) by using a CASE statement like this:
-- v for varying, n for non-varying
SELECT a, b,
CASE WHEN COUNT(DISTINCT C) > 1 THEN 'v' ELSE 'n' END AS num_c,
CASE WHEN COUNT(DISTINCT d) > 1 THEN 'v' ELSE 'n' END AS num_d,
CASE WHEN COUNT(DISTINCT e) > 1 THEN 'v' ELSE 'n' END AS num_e
FROM t
GROUP BY a, b;
This yields:
1 2 n v n
3 2 n n v
If you really want just to know whether any set of values in the given column varies for any values of (a, b) — and not which values of (a, b) it varies for — you can use the query above as a sub-query in the FROM clause and organize things as you want.
SELECT MAX(num_c) AS num_c,
MAX(num_d) AS num_d,
MAX(num_e) AS num_e
FROM (SELECT a, b,
CASE WHEN COUNT(DISTINCT C) > 1 THEN 'v' ELSE 'n' END AS num_c,
CASE WHEN COUNT(DISTINCT d) > 1 THEN 'v' ELSE 'n' END AS num_d,
CASE WHEN COUNT(DISTINCT e) > 1 THEN 'v' ELSE 'n' END AS num_e
FROM t
GROUP BY a, b
);
This relies on v being larger than n; it is easy enough (and convenient enough) for this binary decision, but not necessarily convenient or easy if there are, say, 4 states to map.
This yields:
n v v