How to fetch records up to particular data - sql

Table tb data as below
id remarks status key
------------------------------------
1 dfe 1 340
2 ert 4 340
3 aaa 6 340
4 gh 7 340
5 bnh 2 341
6 xdc 6 341
7 qqq 1 341
8 rty 3 343
9 mnh 1 343
10 ppo 3 343
11 oit 6 343
I want to get list of id upto status 6 by grouping key.
select id
from tb
where status in (6) AND key in (select key_list from temporary_table)
group by key;
I didnt get required records.
For example: I need output as below for
key 340
id
---
1
2
3
For key 341
id
---
5
6
For key 343
id
---
8
9
10
11
Note: Orginal key column data not in order, it is in suffle. For easy understanding in given list this.

You can use a correlated subquery:
select tb.* -- or whatever columns you want
from tb
where tb.id <= (select min(tb2.id)
from tb tb2
where tb2.key = tb.key and tb2.status = 6
);
Alternatively, you can use a window function:
select tb.id
from (select tb.*,
min(case when status = 6 then id end) over (partition by key) as id_6
from tb
) tb
where id <= id_6;

Assuming that
You need a query to return the respective id's for all keys in a
single query;
If for a given key the status is never 6, then you must return all
the rows for that key
you can do this with match_recognize:
select key, id
from tb
match_recognize(
partition by key
order by id
all rows per match
pattern ( ^ a* b? )
define a as status != 6
);
If, instead, you must give the query a single input key as a variable, and return the result just for that key, you can modify the query like this:
select id
from (select id, status from tb where key = :input_key)
match_recognize(
order by id
all rows per match
pattern ( ^ a* b? )
define a as status != 6
);

Related

update _ids values by sorted timestamp with sql

i have a few tables look like below (sqlite)
table a:
a_id (unique)
timestamp
21
123
2
178
33
101
7
221
4
115
table b:
b_id (unique)
a_id
data (no need to sort)
14
4
bb
3
2
dd
22
21
ee
6
7
xx
17
2
tt
11
33
qq
now i want to update these tables' _id according to the timestamp in a as follows:
table a:
a_id
timestamp
1
101
2
115
3
123
4
178
5
221
table b:
b_id
a_id
data
1
1
qq
2
2
bb
3
3
ee
4
4
dd
5
2
tt
6
5
xx
how can i do this easily with sql, without re inserting the data etc?
thanks
i used to create a dummy column with new and sorted ids by timestamp, then update other tables accordingly but it was inefficient and i have forgotten how i did it
update:
to simplify, i want to create a new column sort_id in table a, which holds the value according to the sorted timestamp, so
before:
table a
a_id (unique)
timestamp
21
123
2
178
33
101
7
221
4
115
after:
table a
a_id (unique)
timestamp
sort_id
21
123
3
2
178
4
33
101
1
7
221
5
4
115
2
so how can i fill the sort_id column according to the timestamp?
you want to query your table b data and sort it based on time stamp .
order of rows in a database is not guaranteed at all and it changes anytime after each insert/update, so you need to join two tables and sort based on timestamp column :
select b.*
from table_b b
left join table_a a
on a.a_id = b.a_id
order by a.timestamp
depending on your sqlite version you also can use update from :
update tableb
set sort_id = rn
from (select a_id , row_number() over (order by timestamp) as rn from tableA) tableA
where tableA.a_id = tableB.a_.id
Aftrer you've altered the table to add the new column...
WITH
sorted AS
(
SELECT
a_id,
ROW_NUMBER() OVER (ORDER BY timestamp) AS new_sort_id
FROM
table_a
)
UPDATE
table_a
SET
sort_id = sorted.new_sort_id
FROM
sorted
WHERE
table_a.a_id = sorted.a_id

Custom Sort Based On Referenced Records

Please consider these data:
Id F1 F2 Ref_ID
-------------------------------------------
1 Nima 35 Null
2 Eli 33 Null
3 Arian 5 1
4 Ava 1 1
5 Arsha 3 2
6 Rozhan 30 1
7 Zhina 20 2
I want to sort this table like this result:
Id F1 F2 Ref_ID
-------------------------------------------
1 Nima 35 Null
3 Arian 5 1
4 Ava 1 1
6 Rozhan 30 1
2 Eli 33 Null
5 Arsha 3 2
7 Zhina 20 2
the refrenced records should place under the reference record based on Id ascending.
How I can do this using LINQ or SQL. Thanks
In SQL you could sort it by using a COALESCE or ISNULL for the Ref_id and the Id.
And an IIF or a CASE WHEN to make sure the parent id comes first in the same group of Ref_id.
SELECT Id, F1, F2, Ref_ID
FROM YourTable
ORDER BY COALESCE(Ref_ID, Id), IIF(Ref_ID IS NULL, 0, 1), Id;
A test on db<>fiddle here
Here is a more simple solution:
SELECT
Id,
F1,
F2,
Ref_ID
FROM
#Table
ORDER BY
ISNULL(Ref_ID,ID), ID
Result:
Using LINQ, you can do it like this:
from i in data
orderby i.Ref_ID ?? i.Id
select i;
Another solution is to add an extra column in the query, and sort on that column
select t.*
from ( select Id,
F1,
F2,
Ref_ID,
coalesce(Ref_ID, Id) as RefID_or_Id,
iif(Ref_ID is null, 0, 1) as Ref_ID_0_or_1
from YourTable
) t
order by t.RefID_or_Id,
t.Ref_ID_0_or_1,
t.Id
In case your table is large, you should test wich of the solutions here performs the best for you

Delete rows, which are duplicated and follow each other consequently

It's hard to formulate, so i'll just show an example and you are welcome to edit my question and title.
Suppose, i have a table
flag id value datetime
0 b 1 343 13
1 a 1 23 12
2 b 1 21 11
3 b 1 32 10
4 c 2 43 11
5 d 2 43 10
6 d 2 32 9
7 c 2 1 8
For each id i want to squeze the table by flag columns such that all duplicate flag values that follow each other collapse to one row with sum aggregation. Desired result:
flag id value
0 b 1 343
1 a 1 23
2 b 1 53
3 c 2 75
4 d 2 32
5 c 2 1
P.S: I found functions like CONDITIONAL_CHANGE_EVENT, which seem to be able to do that, but the examples of them in docs dont work for me
Use the differnece of row number approach to assign groups based on consecutive row flags being the same. Thereafter use a running sum.
select distinct id,flag,sum(value) over(partition by id,grp) as finalvalue
from (
select t.*,row_number() over(partition by id order by datetime)-row_number() over(partition by id,flag order by datetime) as grp
from tbl t
) t
Here's an approach which uses CONDITIONAL_CHANGE_EVENT:
select
flag,
id,
sum(value) value
from (
select
conditional_change_event(flag) over (order by datetime desc) part,
flag,
id,
value
from so
) t
group by part, flag, id
order by part;
The result is different from your desired result stated in the question because of order by datetime. Adding a separate column for the row number and sorting on that gives the correct result.

SQL copy from one table to another with changing ID value

I have two tables:
A
ID VALUE
----------
1 7
2 5
3 44
4 982
5 1
6 0
7 671
B
ID VALUE
---------------
1 6
2 6
3 77
4 22
How do I copy data from #B to #A to get a different ID (one bigger than the MAX in #A)? For example I need to get
ID VALUE
1 7
2 5
3 44
4 982
5 1
6 0
7 671
8 6
9 6
10 77
11 22
Either make it an IDENTITY column which auto-increments, or this:
INSERT INTO A
SELECT b.ID + (SELECT MAX(ID) FROM A) AS ID, b.Value
FROM B
DEMO
The select is slightly different if the ID in table B has gaps. Then those gaps are transferred.
If the ID column in TableA is not already set to auto-increment, do the following command:
ALTER TABLE TableA MODIFY COLUMN ID INT auto_increment
Now you can just insert all the records from TableB into TableA:
INSERT INTO TableA (VALUE)
SELECT VALUE
FROM TableB
It is not a great idea to rely on the business logic in your query to maintain the order of the ID column. Instead, let SQL take care of it for you; it was designed for this purpose.

Highest per each group

It's hard to show my actual table and data here so I'll describe my problem with a sample table and data:
create table foo(id int,x_part int,y_part int,out_id int,out_idx text);
insert into foo values (1,2,3,55,'BAK'),(2,3,4,77,'ZAK'),(3,4,8,55,'RGT'),(9,10,15,77,'UIT'),
(3,4,8,11,'UTL'),(3,4,8,65,'MAQ'),(3,4,8,77,'YTU');
Following is the table foo:
id x_part y_part out_id out_idx
-- ------ ------ ------ -------
3 4 8 11 UTL
3 4 8 55 RGT
1 2 3 55 BAK
3 4 8 65 MAQ
9 10 15 77 UIT
2 3 4 77 ZAK
3 4 8 77 YTU
I need to select all fields by sorting the highest id of each out_id.
Expected output:
id x_part y_part out_id out_idx
-- ------ ------ ------ -------
3 4 8 11 UTL
3 4 8 55 RGT
3 4 8 65 MAQ
9 10 15 77 UIT
Using PostgreSQL.
Postgres specific (and fastest) solution:
select distinct on (out_id) *
from foo
order by out_id, id desc;
Standard SQL solution using a window function (second fastest)
select id, x_part, y_part, out_id, out_idx
from (
select id, x_part, y_part, out_id, out_idx,
row_number() over (partition by out_id order by id desc) as rn
from foo
) t
where rn = 1
order by id;
Note that both solutions will only return each id once, even if there are multiple out_id values that are the same. If you want them all returned, use dense_rank() instead of row_number()
select *
from foo
where (id,out_id) in (
select max(id),out_id from foo group by out_id
) order by out_id
Finding max(val) := finding the record for which no larger val exists:
SELECT *
FROM foo f
WHERE NOT EXISTS (
SELECT 317
FROM foo nx
WHERE nx.out_id = f.out_id
AND nx.id > f.id
);