SQL group rows by condition - sql

I need to merge 3 rows into one with following condition - if at least one J exists in any of the columns then all columns should be overwritten with J. If there are all N I should get one row with all N.
I am thiking something like -
SELECT BRGNR
, AFTLST
,CASE WHEN EGMK IN ('J','N') THEN 'J' ELSE EGMK END EGMK
,CASE WHEN TDMK IN ('J','N') THEN 'J' ELSE TDMK END TDMK
,CASE WHEN UDMK IN ('J','N') THEN 'J' ELSE UDMK END UDMK
, CASE WHEN FUMK IN ('J','N') THEN 'J' ELSE FUMK END FUMK
FROM IF.TIF_BRUGER_BT
GROUP BY BRGNR
But the problem is then in that case when I have all N all values will be overwritten to J anyway, and I need to have N if all values are N

I am going to interpret the question as:
If a there is a 'J' in a column, then result is 'J'.
Otherwise, if all are 'N', then result in 'N'.
I will assume that the only values are 'J' and 'N'. My guess is that 'J' --> "yes" and 'N' --> "no" and you want "yes" if any value is "yes".
If so, you can use MIN():
SELECT BRGNR, AFTLST,
MIN(EGMK), MIN(TDMK), MIN(UDMK), MIN(FUMK)
FROM IF.TIF_BRUGER_BT
GROUP BY BRGNR, AFTLST

Related

How can I rename a value from one column based on a condition and keep any other values otherwise?

Using SQL, How can I rename a value from one column based on a condition and keep any other values otherwise?
I've tried:
select a, b,
case when a = 'specific value' then 'new_value'
else a -- keep the current value for anything else
end as c
from x;
ERROR: invalid input value for enum 'new_value'
is not about update columns on database, only select statement returned
select a, b,
case when a = 'specific value' then cast('new_value' as text)
else cast(a as text) -- keep the current value for anything else
end as c
from x;

SQL case statement error handling when there are non-integer values

I have a field of 5 digit codes, and I am trying to create a new flag field if the 5 digit code is between 2 numbers. That part is easy, but there are also a lot of values that have letters and aren't strictly 5 digits. So I'm trying to put a statement at the beginning of the case statement that says if there's an error then set the flag to zero. Or a statement that says if the value is not a number then set to zero.
Here's a sample of listed values:
36569
38206
J8502
JAA8C
Here is some code I've tried (simplified to get the point across):
case
when not isnumeric([code]) then 'N'
when [code] between 50000 and 50005 then 'Y'
ELSE 'N'
end as NewFlag
Thanks!
How about using try_cast() or try_convert():
(case when try_convert(int, code) between 50000 and 50005
then 'Y' else 'N'
end) as newFlag
Actually for your particular values, you can do the comparison as strings:
(case when code between '50000' and '50005'
then 'Y' else 'N'
end) as newFlag
This is really a special case, because you have 5 digit codes and you are only concerned about the last character.

Why does this case statement not work?

Why does this case statement not work? My table is defined as below. I want the case to return true when M = "M" or false if it is not. Something does not seem to look right with this CASE statement but this is the simple case statement. But I thought that could be as many WHEN values as needed but in this cases there is only True or False and it does not appear that there could be anything different.
The input should be all of the M column in the row. I am expecting the output to be one row since I only have one row that has an M value in the column. I am not certain if the case statement should return false in this case.
CASE M = "M <-- is this is what the criteria is based on
I found that this is not the case however from the post below. When i meant that this did not look right I was referring two the fact that the WHEN portion of the code
WHEN true THEN something
just did not look like it normally does.
Code:
select CASE M = "M"
WHEN true THEN "PPP"
WHEN false THEN "False -- Look Again!"
ELSE "Not Found"
END
from Bat;
Image:
New Code:
select *,
CASE
WHEN M = 'M' THEN
Case
when O = 'O' then 'you found a double (MM)' <--Compiles but does not show result
End
WHEN M is null THEN 'False -- Look Again!'
END as 'What is this value'
from Bat;
That fixed the first problem but you can embed case statements N deep I would think so if i search again on O it should return a correct. But the first case returns only one record. So my second case would have to search in the record correct.
The normal way to write this is:
select (CASE WHEN M = 'M' THEN 'PPP'
WHEN M <> 'M' THEN 'False -- Look Again!'
ELSE 'Not Found'
END)
from Bat;
or:
select (CASE WHEN M is null then 'Not Found'
WHEN M = 'M' THEN 'PPP'
ELSE 'False -- Look Again!'
END)
from Bat;
If I had to guess, your version has unexpected behavior for NULL values, but it is hard to say without knowing what you expect or what it is doing.
Change it like this
select CASE
WHEN M = 'M' THEN 'PPP'
WHEN M is null THEN 'False -- Look Again!'
ELSE "Not Found"
END
from Bat;

Setting variables on CASE results in SQL?

I'm sure the answer is simple, but I have been searching for a while and can't seem to get this working ...
I am tring to set a variable based on a Case statement result.
So I am checking length of two columns
dbo.MY_TABLES.MY_RULESET,
dbo.MY_TABLES.MY_SPECIFICATION
My case statements check If length/75 is over 1 then we return 1. If under 1, then we return the value (under 1)
I then want to add these two results together and is where im getting problems when i need the addtion dependant on the case results.
Here is sample
Declare #SpecValue FLOAT
Declare #RuleValue FLOAT
SELECT
CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75 as Score1,
CAST(LEN (MY_RULESET) AS FLOAT)/75 AS Score2,
CASE
WHEN (CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75,2))
END as Spec1_RuleLength,
CASE
When (CAST(LEN (MY_RULESET) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN (MY_RULESET) AS FLOAT)/75,2))
END as Eval2_RuleLength,
dbo.MY_TABLES.MY_RULESET,
dbo.MY_TABLES.MY_SPECIFICATION
FROM MY_TABLES
All columns in a SELECT clause are computed as if they're evaluated in parallel. This means that one column cannot depend on the value of another column computed in the same SELECT.
You might try moving the current query into a subselect:
SELECT Score1 + Score2 /* etc */
FROM (
SELECT
CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75 as Score1,
CAST(LEN (MY_RULESET) AS FLOAT)/75 AS Score2,
CASE
WHEN (CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75,2))
END as Spec1_RuleLength,
CASE
When (CAST(LEN (MY_RULESET) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN (MY_RULESET) AS FLOAT)/75,2))
END as Eval2_RuleLength,
dbo.MY_TABLES.MY_RULESET,
dbo.MY_TABLES.MY_SPECIFICATION
FROM MY_TABLES
) t

How can I assign one field's value to another field based on a third field?

I have three fields and if column three equals a certain value, I want to get the value of column 2. Can I do this in a SELECT, and would I use an IF or CASE statement?
A CASE statement would be the easiest way to do this. The following SQL compares two columns. You can alter this to use WHEN Col2 = 'xxxx' THEN xxxx'.
SELECT Col1, Col2, CASE
WHEN Col2 = Col1 THEN 'True'
ELSE 'False'
END AS 'Columns Match'
FROM Table
Hope this leads you in the right direction
Select
Case
when columnthree = 'VALUE'
then column2
else 'do something'
end as [column name]
from
table name
Also you might want to do some conversion if the datatypes for both columns are different
You could use a CASE clause within a SELECT statement - IF won't work this way in SQLServer.
select Alpha, Beta, Gamma,
case
when Alpha > 2 * Beta then Gamma
when Beta = Gamma then Alpha + 3
when Gamma = 2 then 13
else 42
end as 'Omega'
from Alphabet
First match wins.
CASE returns a value, so it may be helpful in various circumstances:
delete ... where case when Id > Limit then 1 else 0 end = 1
update ... set ShoeSize = case when Wide = 1 then ShoeSize + 1 else ShoeSize end, ...