Update Table at fixed interval of Time SQL Server - sql

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.

Related

Create a group id in SQL Server

I have data in this format
ColA
Date
RSN
ID
DesiredColumn
70
0904
2
0904-2
1
71
0904
3
0904-3
1
100
0904
4
0904-4
1
70
0904
5
0904-5
2
I want to add the DesiredColumn that changes its values every time it sees RTI = 070 in ColA
Can someone please help?
You can use a cumulative sum, something like:
select t.*,
sum(case when colA = 70 then 1 else 0 end) over (partition by date order by rsn)
from t;
I am guessing that you want this per value of date and the ordering is based on rsn. If that is not true, remove the partition by clause.
Sql Fiddle: http://sqlfiddle.com/#!18/59e49/10

Get all the record matching the condition Oracle 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
);

Compare 2 tables and update final table with the row which has max value Bigquery

I have two tables TableA and Final_Table
TableA
ID Step1 Step2 Step3 Step4
1 1 1 0 0 --Update Final_table for Id1 with these values as more steps have 1
2 1 1 1 0
3 1 1 1 1 --Since 3 doesn't exist in Final table insert row in final table
Final_Table
ID Step1 Step2 Step3 Step4
1 1 0 0 0
2 1 1 1 1 --Keep this as more steps have 1 in it
4 0 0 0 0 --since Id4 doesn't exist in Table
After Running the Query I want the final_table to look like below
Final_Table
ID Step1 Step2 Step3 Step4
1 1 1 0 0
2 1 1 1 1
3 1 1 1 1
4 0 0 0 0
Whenever a Id exists in both table I want to Update the Final_Table with data which has max steps as 1. I would appreciate any help!Thanks in advance
I used Merge and Update to solve my question - It worked liked a charm. I created a sumT column of all steps in my Final_Table. Might help someone
First query:
MERGE final_Table T
USING (SELECT id, SUM(of all columns) AS sumA
FROM TableA
GROUP BY Id FROM TableA) S ON T.id = S.id
WHEN MATCHED THEN
UPDATE SET sum = greatest(sumA, sumT)
WHEN NOT MATCHED THEN
INSERT (id, sumT) VALUES (id,sumA)
Second query:
UPDATE Final_Table AS 1 OR 0 depending on SumT
Hope it helps!

SQL: Selecting rows from non unique column values once partitioned by another column

Using SQL here. Trying to select all rows where the column value is unique within that specific partition.
Have tried:
select *
from dataTable
where value in ( select value
from dataTable
group by tv_id, value
having count(*) > 1)
but it returns the full table-- i think the issue is that the values for many of tv_ids are identical and overlap.
What I have:
tv_id value
1 1
1 2
1 2
1 3
2 1
2 1
2 2
2 3
2 4
3 1
3 1
3 2
What I want:
tv_id value
1 2
1 2
2 1
2 1
3 1
3 1
I have a bunch of tv_ids and essentially, I only want the rows where the value is not unique within each tv_id.
Ex: I don't want tv_id, value: 3, 2 because it is the only combination in the data.
Thanks in advance!
Maybe something like this does the trick
Oracle Option
I include this oracle version because it enables you to understand better what are you querying.
select tv_id, value
from dataTable
where (tv_id, value) in (
select tv_id, value
from dataTable
group by tv_id, value
having count(1) > 1
)
SQL
But this is a standard sql version that will work with almost any database engine
select tv_id, value
from dataTable d1
join (
select tv_id, value
from dataTable
group by tv_id, value
having count(1) > 1
) d2
on d1.tv_id=d2.tv_id
and d1.value=d2.value
You need to query the same table twice because the group by makes a distinct in your data, so you won't retrieve duplicated rows as you show in your expected output.

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