SQL Case When Statement for Id with multiple rows - sql

I have a table like so
Id Code
1 03J0
1 0304
1 03HI
2 033I
2 03J5
3 03J4
4 030H
I want to do a case when statement, when there is any occurrence where the Id has a Code that is like '%03J' then Happy otherwise Sad. How do I do this when an Id has multiple rows of different codes?
Intended output
Id Emotion
1 Happy
2 Happy
3 Happy
4 Sad

Is this what you want?
select id,
(case when sum(case when code like '03J%' then 1 else 0 end) > 0 then 'Happy' else 'Sad' end) as emotion
from t
group by id;
Using the ordering of strings, you can simplify this to:
select id,
min(case when code like '03J%' then 'Happy' else 'Sad' end) as emotion
from t
group by id;
Here is a db<>fiddle.

Using self-join. Judging from your sampple data, I think you want '03J%' instead of '%03J'
select distinct a.id, case when b.code is not null then 'Happy' else 'Sad' end as emotion
from mytable a
left join mytable b on a.id=b.id and b.code like '03J%';

Related

How can I use pivot to find the records with the most columns populated?

I have a problem where I have 5 columns.
What I want to do is add a count on the end with the number of columns where there is no null value.
I am trying to use pivot as this seems to be the most logical SQL clause. Any ideas on this? I haven't used Pivot in many instances so this is new for me.
An inline pivot/conditional aggregate and a COUNT seems to be what you want here. As all your columns have different data types, you need to also use some CASE expressions. Something like this:
SELECT ID,
a,
...
(SELECT COUNT(V.C)
FROM (VALUES(CASE WHEN a IS NOT NULL THEN 1 END),
(CASE WHEN b IS NOT NULL THEN 1 END),
(CASE WHEN c IS NOT NULL THEN 1 END),
(CASE WHEN d IS NOT NULL THEN 1 END),
(CASE WHEN e IS NOT NULL THEN 1 END),
(CASE WHEN f IS NOT NULL THEN 1 END))V(C)) AS NonNullColumns
FROM dbo.YourTable;

SQL using CASE in SELECT with GROUP BY. Need CASE-value but get row-value

so basicially there is 1 question and 1 problem:
1. question - when I have like 100 columns in a table(and no key or uindex is set) and I want to join or subselect that table with itself, do I really have to write out every column name?
2. problem - the example below shows the 1. question and my actual SQL-statement problem
Example:
A.FIELD1,
(SELECT CASE WHEN B.FIELD2 = 1 THEN B.FIELD3 ELSE null FROM TABLE B WHERE A.* = B.*) AS CASEFIELD1
(SELECT CASE WHEN B.FIELD2 = 2 THEN B.FIELD4 ELSE null FROM TABLE B WHERE A.* = B.*) AS CASEFIELD2
FROM TABLE A
GROUP BY A.FIELD1
The story is: if I don't put the CASE into its own select statement then I have to put the actual rowname into the GROUP BY and the GROUP BY doesn't group the NULL-value from the CASE but the actual value from the row. And because of that I would have to either join or subselect with all columns, since there is no key and no uindex, or somehow find another solution.
DBServer is DB2.
So now to describing it just with words and no SQL:
I have "order items" which can be divided into "ZD" and "EK" (1 = ZD, 2 = EK) and can be grouped by "distributor". Even though "order items" can have one of two different "departements"(ZD, EK), the fields/rows for "ZD" and "EK" are always both filled. I need the grouping to consider the "departement" and only if the designated "departement" (ZD or EK) is changing, then I want a new group to be created.
SELECT
(CASE WHEN TABLE.DEPARTEMENT = 1 THEN TABLE.ZD ELSE null END) AS ZD,
(CASE WHEN TABLE.DEPARTEMENT = 2 THEN TABLE.EK ELSE null END) AS EK,
TABLE.DISTRIBUTOR,
sum(TABLE.SOMETHING) AS SOMETHING,
FROM TABLE
GROUP BY
ZD
EK
TABLE.DISTRIBUTOR
TABLE.DEPARTEMENT
This here worked in the SELECT and ZD, EK in the GROUP BY. Only problem was, even if EK was not the designated DEPARTEMENT, it still opened a new group if it changed, because he was using the real EK value and not the NULL from the CASE, as I was already explaining up top.
And here ladies and gentleman is the solution to the problem:
SELECT
(CASE WHEN TABLE.DEPARTEMENT = 1 THEN TABLE.ZD ELSE null END) AS ZD,
(CASE WHEN TABLE.DEPARTEMENT = 2 THEN TABLE.EK ELSE null END) AS EK,
TABLE.DISTRIBUTOR,
sum(TABLE.SOMETHING) AS SOMETHING,
FROM TABLE
GROUP BY
(CASE WHEN TABLE.DEPARTEMENT = 1 THEN TABLE.ZD ELSE null END),
(CASE WHEN TABLE.DEPARTEMENT = 2 THEN TABLE.EK ELSE null END),
TABLE.DISTRIBUTOR,
TABLE.DEPARTEMENT
#t-clausen.dk: Thank you!
#others: ...
Actually there is a wildcard equality test.
I am not sure why you would group by field1, that would seem impossible in your example. I tried to fit it into your question:
SELECT FIELD1,
CASE WHEN FIELD2 = 1 THEN FIELD3 END AS CASEFIELD1,
CASE WHEN FIELD2 = 2 THEN FIELD4 END AS CASEFIELD2
FROM
(
SELECT * FROM A
INTERSECT
SELECT * FROM B
) C
UNION -- results in a distinct
SELECT
A.FIELD1,
null,
null
FROM
(
SELECT * FROM A
EXCEPT
SELECT * FROM B
) C
This will fail for datatypes that are not comparable
No, there's no wildcard equality test. You'd have to list every field you want tested individually. If you don't want to test each individual field, you could use a hack such as concatenating all the fields, e.g.
WHERE (a.foo + a.bar + a.baz) = (b.foo + b.bar + b.az)
but either way, you're listing all of the fields.
I might tend to solve it something like this
WITH q as
(SELECT
Department
, (CASE WHEN DEPARTEMENT = 1 THEN ZD
WHEN DEPARTEMENT = 2 THEN EK
ELSE null
END) AS GRP
, DISTRIBUTOR
, SOMETHING
FROM mytable
)
SELECT
Department
, Grp
, Distributor
, sum(SOMETHING) AS SumTHING
FROM q
GROUP BY
DEPARTEMENT
, GRP
, DISTRIBUTOR
If you need to find all rows in TableA that match in TableB, how about INTERSECT or INTERSECT DISTINCT?
select * from A
INTERSECT DISTINCT
select * from B
However, if you only want rows from A where the entire row matches the values in a row from B, then why does your sample code take some values from A and others from B? If the row matches on all columns, then that would seem pointless. (Perhaps your question could be explained a bit more fully?)

best query for a complex situation

I have a table with following columns,(id,fkid, flag1,flag2,flag3,flag4). The possible value for each flag field is -1 to 3 and null is allowed. What I need is a query to check if count of any flag field with value 2 is greater than 3 for a given foreign key fkid. The way I am doing is write a query for each field. It works but very not smart to me. Anyone has better idea? Thanks.
You can do this with one query:
select fkid
from t
group by fkid
having sum(case when flag1 = 2 then 1 else 0 end) > 3 or
sum(case when flag2 = 2 then 1 else 0 end) > 3 or
sum(case when flag3 = 2 then 1 else 0 end) > 3 or
sum(case when flag4 = 2 then 1 else 0 end) > 3
I do agree strongly with the comments, though, that sample data, sample results, and a clear table structure would greatly improve the question.
Query below will also answer your question, sql fiddle example: http://sqlfiddle.com/#!3/adda9/2
SELECT
DISTINCT fkid
FROM
tblTest pvt
UNPIVOT
(
FlagValue FOR Flag IN
(flag1,flag2,flag3,flag4)
) as Unpvt
WHERE
FlagValue = 2
GROUP BY
FKID, FLAG
HAVING COUNT(*)>3

SQL statement using case, like, and having

I am using an Oracle based system.
How do you use like, having, and a case statement together?
I am basically trying to list all of the unique individuals that are found in a transactional table that have more than 4 "Class A" transactions, or more than 1 "Class B" transactions. The reason why I want to use like is because the only way to diferentiate between transaction classes is by using a like statement in the transaction type column.
For example, there are many transaction types, but only "Class A" have '%ABC%' as part of their transaction type, and "Class B" are all the other types that do not have '%ABC%' in their transaction type column.
So again, I want my query to return only the indiv ids that have more than 4 "Class A" Transactions, or 1 "Class B" transaction.
here is what I have so far:
select tt.indiv_id, count(*) from transactiontable tt
group by tt.indiv_id
case when tt.tran_type like '%ABC'
having count(*) > 4
else
having count(*)>1.
I have searched a good bit on the site and I have not found an example using all of these functions together.
select tt.indiv_id,
count(case when tt.tran_type like '%ABC' then 1 end) as ClassACount,
count(case when tt.tran_type not like '%ABC' then 1 end) as ClassBCount
from transactiontable tt
group by tt.indiv_id
having count(case when tt.tran_type like '%ABC' then 1 end) > 4
or count(case when tt.tran_type not like '%ABC' then 1 end) > 1
Try this
select tt.indiv_id, count(*)
from transactiontable tt
group by tt.indiv_id, tt.tran_type
having count(*) > case when tt.tran_type like '%ABC' then 4 else 1 end
Your query is close. You want to keep track of each transaction type separately in the having clause:
select tt.indiv_id, count(*)
from transactiontable tt
group by tt.indiv_id
having sum(case when tt.tran_type like '%ABC%' then 1 else 0 end) > 4 or
sum(case when tt.tran_type not like '%ABC%' then 1 else 0 end) > 1

Get the distinct count of values from a table with multiple where clauses

My table structure is this
id last_mod_dt nr is_u is_rog is_ror is_unv
1 x uuid1 1 1 1 0
2 y uuid1 1 0 1 1
3 z uuid2 1 1 1 1
I want the count of rows with:
is_ror=1 or is_rog =1
is_u=1
is_unv=1
All in a single query. Is it possible?
The problem I am facing is that there can be same values for nr as is the case in the table above.
Case statments provide mondo flexibility...
SELECT
sum(case
when is_ror = 1 or is_rog = 1 then 1
else 0
end) FirstCount
,sum(case
when is_u = 1 then 1
else 0
end) SecondCount
,sum(case
when is_unv = 1 then 1
else 0
end) ThirdCount
from MyTable
you can use union to get multiple results e.g.
select count(*) from table with is_ror=1 or is_rog =1
union
select count(*) from table with is_u=1
union
select count(*) from table with is_unv=1
Then the result set will contain three rows each with one of the counts.
Sounds pretty simple if "all in a single query" does not disqualify subselects;
SELECT
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_ror=1 OR is_rog=1) cnt_ror_reg,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_u=1) cnt_u,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_unv=1) cnt_unv;
how about something like
SELECT
SUM(IF(is_u > 0 AND is_rog > 0, 1, 0)) AS count_something,
...
from table
group by nr
I think it will do the trick
I am of course not sure what you want exactly, but I believe you can use the logic to produce your desired result.