Get all the record matching the condition Oracle SQL - sql

I have the below table structure and wanted to get all the records after the row which matches the given condition
FltNbr Date Stat_Ind
======= ===== =========
1 15-MAR 0
2 16-MAR 1
3 17-MAR 0
4 18-MAR 1
5 19-MAR 0
6 19-MAR 0
I want to get all the records after the row whose Stat_ind =1, in this case the FltNbr 4 has the last occurrence of 1 and want to retrieve all the records after the FltNbr
So the result should be -
FltNbr Date Stat_Ind
======= ===== =========
5 19-MAR 0
6 19-MAR 0
Please help with the SQL

It is unclear if by "previous" you mean by date, fltnbr or both. The following assumes that you mean fltnbr.
Use lag():
select t.*
from (select t.*,
lag(stat_ind) over (order by fltnbr) as prev_stat_ind
from t
) t
where prev_stat_ind = 1;
EDIT:
If you want the rows after the last "1", then:
select t.*
from t
where t.fltnbr > all (select t2.fltnbr from t t2 where t2.stat_ind = 1);
Note that this uses > all. This handles the case where there are no rows with a status of 1. You can also use:
select t.*
from t
where t.fltnbr > (select coalesce(max(t2.fltnbr, t.fltnbr - 1)
from t t2
where t2.stat_ind = 1
);

Related

how can i alternate between 0 and 1 values in sql server?

I want to create a select which will alternate between 1 and 0
my table looks like that
id1 id2 al
11 1 1
40 1 0
12 1 0
237 1 1
but I want to make it like that
id1 id2 al
40 1 0
11 1 1
12 1 0
237 1 1
I want to keep the same values in my table but I just want to switch the rows to alternate between 0 and 1
Consider:
select *
from mytable
order by row_number() over(partition by al order by id1), al
This alternates 0 and 1 values - if the groups have a different number of rows, then, once the smallest group exhausts, all remaining rows in the other group appear at the end of the resultset.
I am unsure which column you want to use to order the rows within each group - I assumed id1, but you might want to change that to your actual requirement.

Update Table at fixed interval of Time SQL Server

I have a table with 3 Columns: ID, Date and ColA
I want to write a Sql query which will update ColA with a value from another Table only after every 3 days else ColA value should be 0.
The Query should be grouped by ID.
Table 1
ID Date ColA
1 2020/01/01 0
1 2020/01/02 0
1 2020/01/03 Run Update Query
1 2020/01/04 0
1 2020/01/05 0
1 2020/01/06 Run Update Query
2 2020/02/09 0
2 2020/02/10 0
2 2020/02/11 Run Update Query
2 2020/02/12 0
2 2020/02/13 0
2 2020/02/14 Run Update Query
Any ideas?? Thanks
You can use window functions:
with cte as (
select colA, row_number() over(partition by id order by date) rn
from mytable
)
update cte
set colA = case when rn % 3 = 0 then 'Run update query' else '0' end
The common table expression(aliasedcte) ranks records having the same id by increasing date. Then, the outer query assigns the correct value depending on the rank.

select query - eliminate rows with duplicate column value on condition

I have a select query that ends up with results like:
ID COMPLIANT
------------------
10 0
12 0
29 0
29 1
43 1
44 1
44 0
How can I get results without these duplicate ID rows, on the condition that if an ID has already been marked as COMPLIANT once (a 1 instead of a 0), the duplicate rows with COMPLIANT=0 do not appear? I'd want:
ID COMPLIANT
------------------
10 0
12 0
29 1
43 1
44 1
How about aggregation?
select id, max(complaint) as complaint
from t
group by id;
This returns one row per id. If you can have multiple complaints -- and you want all of those -- than an alternative is:
select id, complaint
from t
where complaint = 1
union all
select id, complaint
from t
where not exists (select 1 from t t2 where t2.id = t.id and t2.complaint = 1);
this will work:
select id, max(complaint)
from tablename
group by id;

GROUP result set BY column provided an element in group has different column with specific value

If I had a table as shown below and I want to find the max date for each ID, provided at least one of the elements of the ID group has an Info value of 1. So ID 2 would not be included since both of the elements with ID of 2 have an Info value of 2. ID 1 and 3 are included because those ID fields do have at least 1 element with Info value of 1.
ID Date Info
---------------------------
1 01-01-2013 1
1 02-02-2013 2
1 03-03-2013 2
2 01-01-2013 2
2 04-04-2013 2
3 01-01-2013 3
3 05-05-2013 1
3 06-06-2013 1
So output would look like this.
ID MaxDate
-----------------
1 03-03-2013
3 06-06-2013
Thank you very much for your help.
You can use the EXISTS predicate to check that a record where info = 1 exists for the given ID
SELECT T.ID, MaxDate = MAX(T.Date)
FROM T
WHERE EXISTS
( SELECT 1
FROM T T2
WHERE T2.ID = T.ID
AND T2.Info = 1
)
GROUP BY T.ID;
You can achieve the same thing with a conditional aggregate in the HAVING clause:
SELECT T.ID, MaxDate = MAX(T.Date)
FROM T
GROUP BY T.ID
HAVING COUNT(CASE WHEN T.Info = 1 THEN 1 END) > 0;
does this work for you?
Select id, max(date)
From my_table
where id in (select id from my_table where info = 1)
group by id

Return results where first entry is 1 and all subsequent rows are 0

I m working on weird SQL query
Patient_ID Count order_no
1 1 1
2 1 2
2 0 3
2 0 4
3 1 5
3 0 6
where I need to count the patient as above, for every new patient , the count column is 1.
If repeated , the below entry it should be 0
I m confused how should make that work in SQL
In order to make the first entry 1 and all subsuqent entries 0, I believe you need a ranking with partition by the order number. Please checkout the sqlfiddle below to test results.
http://www.sqlfiddle.com/#!3/4e2e2/17/0
SELECT
patient_id
,CASE WHEN r.rank = 1
THEN 1
ELSE 0
END
, order_number
FROM
(
SELECT
order_number
,patient_id
,ROW_NUMBER() OVER (PARTITION BY patient_id ORDER BY order_number)[rank]
FROM
PatientTable
)r