Update 2 lines with When - sql

I have the table below which has similar names in the column 'Nam':
I need to update the column status with the condition below:
If val2 > Val1 (either in first line or in the second line)-->both status should change to "OK"
IF val2 in both lines are smaller than Val1 (in both lines) --> Status=False'
I don't know how to update this condition like (if else)
Thanks for your help.

You can do it with EXISTS:
UPDATE t1
SET t1.Status = CASE
WHEN EXISTS (SELECT 1 FROM tablename t2 WHERE t2.Nam = t1.Nam AND t2.Val2 > t2.Val1) THEN 'OK'
ELSE 'False'
END
FROM tablename t1
See the demo.

You can use window functions and an updatable CTE:
with toupdate as (
select t.*,
sum(case when val2 >= val1 then 1 else 0 end) over (partition by nam) as num_increasing,
sum(case when val2 <= val1 then 1 else 0 end) over (partition by nam) as num_decreasing
from t
)
update toupdate
set status = (case when num_increasing = 0 then 'false' else 'ok' end)
where num_increasing = 0 or num_decreasing = 0
Note that this is doing the "inverse" of your logic -- counting the exceptions.

Related

Select one value from two different select query result (Oracle) based on first select query result

Please amend the below query for my requirement. Please find code below.
CASE Select NVL(Sum(Value1+value2 +value3),0) from Table1 Where Step = 4
WHEN 0 THEN (Select Sum(Value1) from Table1 Where Step < 4)
Else NVL(Sum(Value1 + value2 + value3) END AS "SumValue"
Here your go. Use subquery.
select case when t1.val = 0 then t1.val2 else t1.val3 end as "SumValue"
from
(Select NVL(Sum(case when step = 4 then Value1+value2+value3 else 0 end),0) val
, Sum(case when step < 4 then Value1 else 0 end) val2
, Sum(Value1+value2+value3) val3 from Table1) t1

SQL check link between two records (CASE, WHEN, THEN)

I Have in table some records:
ID Services
2 A
2 C
2 C1
2 D2
I`m trying make query that will be select a link between services.
For example: If for ID 2 exists Services C then check if exist Service C1, result Yes or No.
SELECT a. ID, a.service,
CASE
WHEN (a.service ='C') = (a.service = 'C1') THEN 'Yes'
ELSE 'No'
END
FROM t1 a
Try this query:
SELECT *
FROM yourTable t1
WHERE NOT EXISTS (SELECT 1 FROM yourTable t2
WHERE (t2.Services LIKE t1.Services + '%' OR
t1.Services LIKE t2.Services + '%') AND
t1.ID = t2.ID AND t1.Services <> t2.Services);
This returns A and D2 only.
Demo
Hmm... what about this? But I now have problem with checking relationship for each ID independently...
SELECT a. ID, a.service,
CASE
WHEN a.service IN ('C','C1') THEN 'Yes'
ELSE 'No'
END
FROM t1 a
If I understand correctly, you can use aggregation:
SELECT ID,
(CASE WHEN SUM(CASE WHEN service = 'C' THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN service = 'C1' THEN 1 ELSE 0 END) > 0
THEN 'Yes' ELSE 'No'
END) as c_c1_flag
FROM t1
GROUP BY ID;
The SUM(CASE . . . ) counts the number of rows that match the conditions. The > 0 simply says that at least one row exists.

SQL MAX value of two sub queries

I have two queries and I want to get the maximum value of the two of them.
MAX((SELECT COUNT(p.[ItemID]) FROM [dbo].[Table] p WHERE HasHuman=0),
(SELECT COUNT(p.[ItemID]) FROM [dbo].[Table] p WHERE HasHuman=1))
You can calculate both result in a single query and then apply TOP:
select top 1
HasHuman,
COUNT(p.[ItemID]) as cnt
from [dbo].[Table]
group by HasHuman
order by cnt desc
You could even do this in a single query:
SELECT
CASE WHEN SUM(CASE WHEN HasHuman=0 THEN 1 ELSE 0 END) >
SUM(CASE WHEN HasHuman=1 THEN 1 ELSE 0 END)
THEN SUM(CASE WHEN HasHuman=0 THEN 1 ELSE 0 END)
ELSE SUM(CASE WHEN HasHuman=1 THEN 1 ELSE 0 END) END
FROM [dbo].[Table]
WHERE ItemID IS NOT NULL -- you were not counting NULLs
SELECT MAX(RC)
FROM (SELECT COUNT(p.ItemID) AS RC FROM dbo.[Table]
WHERE HasHuman=0
UNION
SELECT COUNT(p.ItemID) AS RC FROM dbo.[Table]
WHERE HasHuman=1
) A

Get count of a column as well as value of column with specific condition for each group

I 'am using sqlserver 2012 ,
I have table that contains column1 and column2 and other columns ,
I want to get count of column1 as well as the value of column2 that matches specific condition for each group
the condition is column2 like '%page1%'
select COUNT(column1) FROM MyTable group by column1
what should I add to this query .
Thanks in advance .....
Use CASE WHEN condition inside COUNT:
SELECT
totC1 = COUNT(CASE WHEN column1 > 0 THEN 1 ELSE NULL END) ,
totC2 = COUNT(CASE WHEN column2 > 0 THEN 1 ELSE NULL END) ,
totC3 = COUNT(CASE WHEN column3 > 0 THEN 1 ELSE NULL END) ,
totC4 = COUNT(CASE WHEN column4 > 0 OR YourCondition = 1 THEN 1 ELSE NULL)
FROM YOUR_TABLE
In this way if the condition is verified the item will be counted otherwise no.
Let me know

select query result filter using if conditions

I have folllowing select query
SELECT
Table.ID
SUM(CASE WHEN Table.Status = 1 THEN 1 ELSE null END) AS NormalCount,
SUM(CASE WHEN Table.status = 2 THEN 1 ELSE null END) AS AbnormalCount
FROM Table
GROUP BY Table.ID
I want to get above results and generate new result set with following conditions
IF(NormalCount > 0 or AbnormalCount == NULL)
SELECT
Table.ID
Table.Status AS "Normal"
FROM Table
GROUP BY Table.ID
ELSE IF ( AbnormalCount > 0)
SELECT
Table.ID
Table.Status AS "Abnormal"
SUM(CASE WHEN Header.status = 2 THEN 1 ELSE null END) AS AbnormalCount
FROM Table
GROUP BY Table.ID
I think the logic you want is to label each ID group as being abnormal if it has one or more abnormal observation. If so, then you can use another CASE statement to check the conditional abnormal sum and label the status appropriately. Normal groups would have the characteristic of having an abnormal count of zero, but this count would appear for all groups.
SELECT t.ID,
CASE WHEN SUM(CASE WHEN t.status = 2 THEN 1 ELSE 0 END) > 0
THEN "Abnormal"
ELSE "Normal" END AS Status,
SUM(CASE WHEN t.status = 2 THEN 1 ELSE 0 END) AS AbnormalCount
FROM Table t
GROUP BY t.ID