Creating flag column - sql

I would like to ask you for your help. I want to create a flag column, that will mark specific contract numbers whith 1, where the "CLOSED" column is empty in some rows.
I tried
case when CLOSED is null then 1 else 0 end as flag
group by CONTRACT_NUMBER
but it's not working. Thank you for your responses
example picture

I think you need analytic max() here:
demo
select t.*,
max(case when closed is null
then 1
else 0
end) over (partition by contract_number) as flag
from t

Related

use case statement along with partition by

I have data as below
if there is a customer 1, and he has multiple rows of comments. Even if any one of the comments has the keyword "sania", I want that customer to be flagged as '0'. else mark as '1'.
Something like this?
select
customer
, comments
, min(case when comments='sania' then 0 else 1 end)
over (partition by customer) as new_flag
from Tbl

How to implement conditional COUNTIF in SQL?

I am new to SQL and facing the problem for the while.
The question is how to create such Label column in SQL?
Goal: When group count is more than 1 it should be 'Y' otherwise 'N'
Here is what I do in Excel (please see screenshot).
I think you want case with a window function:
select (case when count(*) over (partition by item) > 1 then 'Y' else 'N' end) as label

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 create a flag based on ranking values in two separate columns

I have a query where I have duplicate rows for certain ID's, but have two different columns created by window functions and need to create a flag for each row based on a certain order. I've provided an example below of what the data looks like.
I have a RANK_ONE column that returns a ranked value or null and not every ID has a value, but if it has a one, I need it to return a 1 for the row that contains 1.
The RANK_TWO column is basically the same, but I need to flag the 1 row IF there isn't already a 1 flagged from RANK_ONE for the same ID.
The PRIMARY column is my desired outcome. Any thoughts?
I don't know if I've just been in the query too long and can't see a simple solution right in front of me, but it's driving me nuts trying to figure it out.
You seem to want rank_one if it is every 1 for the id and otherwise rank_two:
select t.*,
(case when max(case when rank_one = 1 then 1 else 0 end) over (partition by id) = 1 and rank_one = 1 then 1
when max(case when rank_one = 1 then 1 else 0 end) over (partition by id) = 1 then 0
when rank_two = 1 then 1
else 0
end) as primary
from t;

SQL count on a field which is a code

I have a table
ANSWERS
qId
toggle_value
which records an HTML radio button value {Yes, N/A, No, Resolved}
Now I want to count and summarize how many yeses, nos, nas and resolved grouped by question Id.
For simplicity I started to build each individual query.
SELECT qId, count(*) as yes_qty FROM ANSWERS WHERE TOGGLE_VALUE='Yes' GROUP BY qId;
SELECT qId, count(*) as na_qty FROM ANSWERS WHERE TOGGLE_VALUE='NA' GROUP BY qId;
SELECT qId, count(*) as no_qty FROM ANSWERS WHERE TOGGLE_VALUE='No' GROUP BY qId;
SELECT qId, count(*) as resolved_qty FROM ANSWERS WHERE TOGGLE_VALUE='Resolved' GROUP BY qId;
But I really want it in one query so I can iterate over the list and display something like this ( aggregating 14 Checklists with 3 questions)
Q Yes No NA Resolved
1 4 10 0 10
2 14 0 0 0
3 7 0 7 0
I don't actually use strings for the toggle value but numbers 1=yes, 2=NA, etc... and was wondering if a better table design would have been
ANSWERS
qId
yes_value
no_value
na_value
resolved_value
I'd have to refactor a lot of other things if I changed the table deisgn so I was hoping to get a single query working.
SELECT qId,
SUM(CASE WHEN TOGGLE_VALUE='Yes' THEN 1 ELSE 0 END) AS YesQty,
SUM(CASE WHEN TOGGLE_VALUE='No' THEN 1 ELSE 0 END) AS NoQty,
SUM(CASE WHEN TOGGLE_VALUE='NA' THEN 1 ELSE 0 END) AS NAQty,
SUM(CASE WHEN TOGGLE_VALUE='Resolved' THEN 1 ELSE 0 END) AS ResolvedQty
FROM ANSWERS
GROUP BY qId
I like your table design. I would personally solve this with a SUM and GROUP BY and using CASE clauses to create new columns.