compare two consequetive rows of teradata table - sql

If i have a table like
In my table i want to compare every two consequetive row. Suppose if the employee_status is 'yes' and after it is 'pro'. Then I want to add another column and write 'problem' in both rows and 'no problem' in other rows.
Like,
1 yes problem
2 pro problem
3 pro no problem

You can use lead()/lag() -- or the Teradata equivalent -- and a case expression:
select t.*
(case when employee_status = 'yes' and
max(employee_status) over (order by employee_no rows between 1 following and 1 following) = 'pro'
then 'problem'
when employee_status = 'pro' and
max(employee_status) over (order by employee_no rows between 1 preceding and 1 preceding) = 'yes'
then 'problem'
else 'no problem'
end) as problem_col
from t;

Related

Remove the duplicate rows based on Presence of Number of NULL values in a row

I was able to remove the duplicate rows, but I would like to remove the duplicate rows based on one more constraint. I want to keep only a row with a smaller number of NULL values.
Original Table
Ran the SQL Server Query
WITH CTE AS(
SELECT *,
RN = ROW_NUMBER()OVER(PARTITION BY Premise_ID ORDER BY Premise_ID)
FROM sde.Premise_Test
)
DELETE FROM CTE WHERE RN > 1
Result:
But I want to get this result
I have modified the SQL script as per the comment from Aaron. but the result is still the same. DB fiddle is showing NULL from IS NULL getting highlighted.
Update the ROW_NUMBER() function like this (no, there is no shorter way):
RN = ROW_NUMBER() OVER (
PARTITION BY Premise_ID
ORDER BY Premise_ID,
CASE WHEN Division IS NULL THEN 1 ELSE 0 END
+ CASE WHEN InstallationType IS NULL THEN 1 ELSE 0 END
+ CASE WHEN OtherColumn IS NULL THEN 1 ELSE 0 END
...
)

Combining multiple rows with the same ID, but different 'Yes'/'No' values for several columns, into one row showing all 'Yes'/'No' values

For the above table, I need to reduce the rows down to one per Filter ID and have all the possible yes/no values showing for that particular Filter Id
for example:
Filter ID
Outpatient Prescriptions
Opioid Outpatient Prescriptions
...
IP Pharmacy Medication Orders - Component Level
1
Yes
Yes
...
No
How is this achieved?
If I understand your question, for each partition of FilterID value, you want any field that has a yes to be aggregated up as 'Yes', otherwise 'No'. If you group by FilterID then you can handle the rollup using a CASE SUM CASE.
SELECT
FilterID,
Field1Response = CASE WHEN SUM(CASE WHEN Field1='Yes' THEN 1 ELSE 0 END) > 1 THEN 'Yes' ELSE 'No' END,
Field2Response = CASE WHEN SUM(CASE WHEN Field2='Yes' THEN 1 ELSE 0 END) > 1 THEN 'Yes' ELSE 'No' END ,
Field3Response = CASE WHEN SUM(CASE WHEN Field3='Yes' THEN 1 ELSE 0 END) > 1 THEN 'Yes' ELSE 'No' END
...
FROM
Data
GROUP BY
FilterID
By the nature of the data, you can also simply use a MAX. This is not a good habit of getting into because the values may change over time, however, if the values are always Y or N then you could simply use MAX:
SELECT
FilterID,
Field1Response = MAX(Field1),
Field2Response = MAX(Field1),
Field3Response = MAX(Field1)
...
FROM
Data
GROUP BY
FilterID

SQL - Subselect in select clause - how to create column which decides uniqity logic

I am trying to write subselect which will run through returned data, then checks status of all and then decides uniquity logic.
Is there any way to find out following ?
case any of data has 'Active' status first one will be marked as 1 everything else as 0
case there is no 'Active' status then first 'Expired' status will by marked as 1 and everything else as 0
case there is no 'Active' and 'Expired' status then first 'In Progress' will be marked as 1 and everything else as 0
I was trying to write it like this but i need to have it in one case statement
SELECT a.id, a.status,
,(SELECT
CASE WHEN b.STATUS = 'Active' THEN 1 ELSE 0 END
CASE WHEN b.STATUS = 'Expired' THEN 1 ELSE 0 END
FROM b.TABLE
WHERE a.id=b.id )AS unique
FROM my.TABLE
Result should look like https://i.stack.imgur.com/qCA74.png picture for expired case
Thank you in advance for any tips.
Use a window function:
select t.*,
(case when row_number() over (partition by id
order by case status when 'Active' then 1 when 'Expired' then 2 else 3 end
) = 1
then 1 else 0
end) as unique_flag
from my.table t;
If the lookup table is the same as source table, then you can use LAG function with constant and use its default value to mark the first row with 1 and others with 0. But you need to order your rows by some fields to deal with duplicates on status.
select a.id, a.status,
lag(0, 1, 1) over(
partition by a.id
order by
case a.status
when 'Active' then 0
when 'Expired' then 1
else 3
end asc,
a.some_more_columns asc /*To find that first row when there are duplicates by status*/
) as unique_flag
from MY_TABLE a
And what about object naming: never use keywords as identifiers. Calling column with date as date, table with users as users and some unknown table as table makes you design error prone.

Mark values with diffreent tag in sql

I have one endpoint that is 7. I would like to few numbers 40,35,30,26,22,18,12 mark as completed.(This is an example. The value may be different) and few numbers 13,17,21,27,32,38,43 mark as pending. (This is an example. The value may be different) Can we achieve by SQL statement? for number details, please find the image.
If your DBMS supports Windowed Aggregates:
with cte as
( select ID, point,
-- find all rows after the latest 7 row
sum(case when point = 7 then 1 end)
over (order by ID DESC) as cumsum
from tab
)
select ID, point,
case when point = 7 then 'endpoint'
when cumsum is null then 'pending' -- no 7 after those IDs
else 'completed'
end
from cte
If you want everything before the first "7" as "completed" and the rest as "pending", then you can use window functions and cumulative logic. One method is:
select t.*,
(case when point = 7 then null
when id < min(case when point = 7 then id end) over ()
then 'complete'
else 'pending'
end) as mark
from t ;

Trying to combine multiples of a key ID into single row, but with different values in columns

TSQL - SQL Sever
I'm building a report to very specific requirements. I'm trying to combine multiples of a key ID into single rows, but there's different values in some of the columns, so GROUP BY won't work.
SELECT count(tt.Person_ID) as CandCount, tt.Person_ID,
CASE e.EthnicSuperCategoryID WHEN CandCount > 1 THEN 10 ELSE e.EthnicSuperCategoryID END as EthnicSuperCategoryID,
CASE e.Ethnicity_Id WHEN 1 THEN 1 ELSE 0 END as Black ,
CASE e.Ethnicity_Id WHEN 2 THEN 1 ELSE 0 END as White ,
CASE e.Ethnicity_Id WHEN 3 THEN 1 ELSE 0 END as Asian,
etc
FROM T_1 TT
JOINS
WHERE
GROUP
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '>'.
Here's the results (without the first CASE). Note person 3 stated multiple ethnicities.
SELECT count(tt.Person_ID) as CandCount, tt.Person_ID,
CASE e.Ethnicity_Id WHEN 1 THEN 1 ELSE 0 END as Black ,
CASE e.Ethnicity_Id WHEN 2 THEN 1 ELSE 0 END as White ,
CASE e.Ethnicity_Id WHEN 3 THEN 1 ELSE 0 END as Asian,
etc
FROM T_1 TT
JOINS
WHERE
GROUP
That’s expected, but the goal would be to assign multiple ethnicities to Ethnicity_Id of 10 (multiple). I also want them grouped on a single line.
So the end result would look like this:
So my issue is two fold. If the candidate has more than 2 ethnicities, assign the records to Ethnicity_Id of 10. I also need duplicated person IDs grouped into a single row, while displaying all of the results of the columns.
This should bring your desired result:
SELECT Person_ID
, ISNULL(ID_Dummy,Ethnicity_ID) Ethnicity_ID
, MAX(Black) Black
, MAX(White) White
, MAX(Asian) Asian
FROM #T T
OUTER APPLY(SELECT MAX(10) FROM #T T2
WHERE T2.Person_ID = T.Person_ID
AND T2.Ethnicity_ID <> T.Ethnicity_ID
)EthnicityOverride(ID_Dummy)
GROUP BY Person_ID, ISNULL(ID_Dummy,Ethnicity_ID)
You want conditional aggregation. Your query is incomplete, but the idea is:
select
person_id,
sum(case ethnicity_id = 1 then 1 else 0 end) as black,
sum(case ethnicity_id = 2 then 1 else 0 end) as white,
sum(case ethnicity_id = 3 then 1 else 0 end) as asian
from ...
where ...
group by person_id
You might want max() instead of sum(). Also I did not get the logic for column the second column in the desired results - maybe that's just count(*).
This would be my approach
SELECT
person_id,
CASE WHEN flag = 1 THEN Ethnicity_Id ELSE 10 END AS Ethnicity_Id,
[1] as black,
[2] as white,
[3] as asian
FROM
(
SELECT
person_id,
Ethnicity_Id as columns,
1 as n,
MAX(Ethnicity_Id) over(PARTITION BY person_id) as Ethnicity_Id,
COUNT(Ethnicity_Id) over(PARTITION BY person_id) as flag
FROM
#example
) AS SourceTable
PIVOT
(
MAX(n) FOR columns IN ([1], [2], [3])
) AS PivotTable;
Pivot the Ethnicity_Id column into multiples columns, Using constant
1 to make it complain with your expected result.
Using Max(Ethnicity_Id) with Partition By to get the original
Ethnicity_Id
Using Count(Ethnicity_Id) to flag if a need to raplace Ethnicity_Id
with 10 bc there is more that 1 row for that person_id
If you need to add more Ethnicitys add the ids in ... IN ([1], [2], [3]) ... and in the select