Select top 10 records of a certain key sql - sql

I have 2 tables connected with each other, for simplicity lets say ID of the 1st is connected to UserCreated_FK of the 2nd table.
Table A:
ID
NAME
237
Gal
240
blah
250
blah2
in the parallel table ( aka table B ) I have the following:
UserCreated_FK
col2
237
10/10
20 more rows of 237
20 more rows of 237
240
11/10
5 more rows of 240
20 more rows of 240
250
12/10
14 more rows of 250
20 more rows of 250
Result wanted:
id.237 x10 last(might be sorted by date col I have).
no 240 (cause less than 10).
id.250 x10 last records(might be sorted by date col I have).
My task is to check which of those IDs(person'sIds) have more then 10 records on table B ( because table A is just the ids and names not the actual records of them.
So for example, ID no.237 got 19 records on table B I want to be able to pull the last 10 records of that user.
And again, the condition is only users that have more than 10 records, then to pull the last of 10 of those..
Hope I was understandable, thanks a lot, any help will be appreciated!

You can join the two tables and then use analytic functions:
SELECT *
FROM (
SELECT a.id,
a.name,
b.col2,
b.datetime,
COUNT(*) OVER (PARTITION BY a.id) AS b_count,
ROW_NUMBER() OVER (PARTITION BY a.id ORDER BY b.datetime DESC) AS rn
FROM table_a a
INNER JOIN table_b b
ON (a.id = b.user_created_fk)
)
WHERE b_count >= 10
AND rn <= 10;
If you just want the values from table_b then you do not need to use table_a:
SELECT *
FROM (
SELECT b.*,
COUNT(*) OVER (PARTITION BY b.user_created_fk) AS b_count,
ROW_NUMBER() OVER (
PARTITION BY b.user_created_fk ORDER BY b.datetime DESC
) AS rn
FROM table_b b
)
WHERE b_count >= 10
AND rn <= 10;

The WITH clause is here just to generate some sample data and it is not a part of the answer.
WITH
tbl_a AS
(
Select 237 "ID", 'Gal' "A_NAME" From Dual Union All
Select 240 "ID", 'blah' "A_NAME" From Dual Union All
Select 250 "ID", 'blah2' "A_NAME" From Dual
),
tbl_b AS
(
Select 237 "FK_ID", SYSDATE - (21 - LEVEL) "A_DATE" From Dual Connect By LEVEL <= 20 Union All
Select 240 "FK_ID", SYSDATE - (6 - LEVEL) "A_DATE" From Dual Connect By LEVEL <= 5 Union All
Select 250 "FK_ID", SYSDATE - (15 - LEVEL) "A_DATE" From Dual Connect By LEVEL <= 14
)
Select
a.ID,
a.A_NAME,
b.TOTAL_COUNT "TOTAL_COUNT",
b.RN "RN",
b.A_DATE
From
tbl_a a
Inner Join
( Select FK_ID, A_DATE, ROW_NUMBER() OVER(Partition By FK_ID Order By FK_ID, A_DATE DESC) "RN", Count(*) OVER(Partition By FK_ID) "TOTAL_COUNT" From tbl_b ) b ON(b.FK_ID = a.ID)
WHERE
b.RN <= 10 And b.TOTAL_COUNT > 10
With your sample data the main SQL joins tbl_a with a subquery by FK_ID = ID. The subquery uses analytic functions to get total rows per FK_ID (in tbl_b) and gives row numbers (partitioned by FK_ID) to the dataset. All the rest is in the Where clause.
Result:
/*
ID A_NAME TOTAL_COUNT RN A_DATE
---------- ------ ----------- ---------- ---------
237 Gal 20 1 29-OCT-22
237 Gal 20 2 28-OCT-22
237 Gal 20 3 27-OCT-22
237 Gal 20 4 26-OCT-22
237 Gal 20 5 25-OCT-22
237 Gal 20 6 24-OCT-22
237 Gal 20 7 23-OCT-22
237 Gal 20 8 22-OCT-22
237 Gal 20 9 21-OCT-22
237 Gal 20 10 20-OCT-22
250 blah2 14 1 29-OCT-22
250 blah2 14 2 28-OCT-22
250 blah2 14 3 27-OCT-22
250 blah2 14 4 26-OCT-22
250 blah2 14 5 25-OCT-22
250 blah2 14 6 24-OCT-22
250 blah2 14 7 23-OCT-22
250 blah2 14 8 22-OCT-22
250 blah2 14 9 21-OCT-22
250 blah2 14 10 20-OCT-22
*/
Regards...

Related

Procedure for adding one column from one table to another table and its data

I have two tables table1 and table2.
Table 1 has three columns
id name age
----------------
1 ram 27
2 rafi 30
Table 2-
no place
--------------
101 agra
102 delhi
103 chennai
104 hyd
In this situation I want to create a procedure to get the no column of table2 will be added to id column of table1 and the remaining data should be copied same and and if the count of table2 is more then the data should be repeated as shown below
id name age
-----------------
1 ram 27
2 rafi 30
101 ram 27
102 rafi 30
103 ram 27
104 rafi 30
Please help
Assuming table1.id1 is a monotonically increasing series starting at 1 this simple trick will work:
insert into table1
select sq2.no#
, t1.name
, t1.age
from table1 t1
inner join (
select t2.no#
, mod(rownum, sq.cnt)+1 as mod#
from table2 t2
cross join (select count(*) as cnt from table1) sq
) sq2 on sq2.mod# = t1.id
/
There is a demo on db<>fiddle.
If table1.id1 contains gaps, or does not start from 1, you will need to replace table1 in the FROM clause above with another subquery ...
(select t1.*
, row_number() over (order by t1.id) as mod#
from table1 t1 ) sq1
... and inner join that to the existing subquery on mod#.
You can achieve it using row_number analytical function with join using MOD function as following:
SQL> WITH TABLE_1(ID, NAME, AGE)
2 AS (SELECT 1, 'ram', 27 FROM DUAL UNION ALL
3 SELECT 2, 'rafi', 30 FROM DUAL),
4 TABLE_2(NO, PLACE)
5 AS (SELECT 101, 'agra' FROM DUAL UNION ALL
6 SELECT 102, 'delhi' FROM DUAL UNION ALL
7 SELECT 103, 'chennai' FROM DUAL UNION ALL
8 SELECT 104, 'hyd' FROM DUAL)
9 -- YOUR QUERY STARTS FROM HERE
10 SELECT ID, NAME, AGE FROM TABLE_1
11 UNION ALL
12 SELECT T2.NO, T1.NAME, T1.AGE
13 FROM
14 (SELECT T.*,
15 ROW_NUMBER() OVER( ORDER BY ID DESC) AS R,
16 COUNT(1) OVER() C
17 FROM TABLE_1 T ) T1
18 --
19 JOIN (SELECT T.*,
20 ROW_NUMBER() OVER( ORDER BY NO ) AS R,
21 COUNT(1) OVER() C
22 FROM TABLE_2 T ) T2
23 ON ( MOD(T2.R, T1.C) = T1.R - 1 )
24 ORDER BY ID;
ID NAME AGE
---------- ---- ----------
1 ram 27
2 rafi 30
101 ram 27
102 rafi 30
103 ram 27
104 rafi 30
6 rows selected.
SQL>
Cheers!!

Create table from loop output Oracle SQL

I need to pull a random sample from a table of ~5 million observations based on 175 demographic options. The demographic table is something like this form:
1 40 4%
2 30 3%
3 30 3%
- -
174 2 .02%
175 1 .01%
Basically I need this same demographic breakdown randomly sampled from the 5M row table. For each demographic I need a sample of the same one from the larger table but with 5x the number of observations (example: for demographic 1 I want a random sample of 200).
SELECT *
FROM (
SELECT *
FROM my_table
ORDER BY
dbms_random.value
)
WHERE rownum <= 100;
I've used this syntax before to get a random sample but is there any way I can modify this as a loop and substitute variable names from existing tables? I'll try to encapsulate the logic I need in pseudocode:
for (each demographic_COLUMN in TABLE1)
select random(5*num_obs_COLUMN in TABLE1) from ID_COLUMN in TABLE2
/*somehow join the results of each step in the loop into one giant column of IDs */
You could join your tables (assuming the 1-175 demographic value exists in both, or there is an equivalent column to join on), something like:
select id
from (
select d.demographic, d.percentage, t.id,
row_number() over (partition by d.demographic order by dbms_random.value) as rn
from demographics d
join my_table t on t.demographic = d.demographic
)
where rn <= 5 * percentage
Each row in the main table is given a random pseudo-row-number within its demographic (via the analytic row_number()). The outer query then uses the relevant percentage to select how many of those randomly-ordered rows for each demographic to return.
I'm not sure I've understood how you're actually picking exactly how many of each you want, so that probably needs to be adjusted.
Demo with a smaller sample in a CTE, and matching smaller match condition:
-- CTEs for sample data
with my_table (id, demographic) as (
select level, mod(level, 175) + 1 from dual connect by level <= 175000
),
demographics (demographic, percentage, str) as (
select 1, 40, '4%' from dual
union all select 2, 30, '3%' from dual
union all select 3, 30, '3%' from dual
-- ...
union all select 174, 2, '.02%' from dual
union all select 175, 1, '.01%' from dual
)
-- actual query
select demographic, percentage, id, rn
from (
select d.demographic, d.percentage, t.id,
row_number() over (partition by d.demographic order by dbms_random.value) as rn
from demographics d
join my_table t on t.demographic = d.demographic
)
where rn <= 5 * percentage;
DEMOGRAPHIC PERCENTAGE ID RN
----------- ---------- ---------- ----------
1 40 94150 1
1 40 36925 2
1 40 154000 3
1 40 82425 4
...
1 40 154350 199
1 40 126175 200
2 30 36051 1
2 30 1051 2
2 30 100451 3
2 30 18026 149
2 30 151726 150
3 30 125302 1
3 30 152252 2
3 30 114452 3
...
3 30 104652 149
3 30 70527 150
174 2 35698 1
174 2 67548 2
174 2 114798 3
...
174 2 70698 9
174 2 30973 10
175 1 139649 1
175 1 156974 2
175 1 145774 3
175 1 97124 4
175 1 40074 5
(you only need the ID, but I'm including the other columns for context); or more succinctly:
with my_table (id, demographic) as (
select level, mod(level, 175) + 1 from dual connect by level <= 175000
),
demographics (demographic, percentage, str) as (
select 1, 40, '4%' from dual
union all select 2, 30, '3%' from dual
union all select 3, 30, '3%' from dual
-- ...
union all select 174, 2, '.02%' from dual
union all select 175, 1, '.01%' from dual
)
select demographic, percentage, count(id) as ids, min(id) as min_id, max(id) as max_id
from (
select d.demographic, d.percentage, t.id,
row_number() over (partition by d.demographic order by dbms_random.value) as rn
from demographics d
join my_table t on t.demographic = d.demographic
)
where rn <= 5 * percentage
group by demographic, percentage
order by demographic;
DEMOGRAPHIC PERCENTAGE IDS MIN_ID MAX_ID
----------- ---------- ---------- ---------- ----------
1 40 200 175 174825
2 30 150 1 174126
3 30 150 2452 174477
174 2 10 23448 146648
175 1 5 19074 118649
db<>fiddle

How to get the full row of data based on max value?

I have an Oracle table with a usage counter. I need to get the full row of data for each sensor with the max counter value?
For ALO I need data in row 2.
For AMA I need data in row 10.
For A11 I need data in row 9658.
For MSP I need data in row 9659.
Any help would be greatly appreciated!
Thanks,
Dave
You can use window function.
selcet * from (
select a.*, row_number() over (partition by facility_id_n
order by usage_counter_n desc) rn
from your_tbl a)
where rn =1;
Or use the First window-funcrion.
ROW_NUMBER won't return correct result if there are two rows with the same USAGE_COUNTER_N value per FACILITY_ID_N. For example:
SQL> WITH test
2 AS (SELECT 9640 ID_n, 'ALO' sensor_id_c, 317 usage_counter_n FROM DUAL
3 UNION
4 SELECT 9641, 'ALO', 18 FROM DUAL
5 UNION
6 SELECT 9642, 'ALO', 0 FROM DUAL
7 UNION
8 SELECT 9659, 'MSP', 25 FROM DUAL --> MAX for MSP ...
9 UNION
10 SELECT 9660, 'MSP', 10 FROM DUAL
11 UNION
12 SELECT 1000, 'MSP', 25 FROM DUAL --> ... but this is also MAX for MSP
13 )
14 SELECT *
15 FROM (SELECT a.*,
16 ROW_NUMBER ()
17 OVER (PARTITION BY sensor_id_c
18 ORDER BY usage_counter_n DESC) rn
19 FROM test a)
20 WHERE rn = 1;
ID_N SEN USAGE_COUNTER_N RN
---------- --- --------------- ----------
9640 ALO 317 1
1000 MSP 25 1
SQL>
RANK might be better:
SQL> l16
16* ROW_NUMBER ()
SQL> c/row_number/rank
16* rank ()
SQL> /
ID_N SEN USAGE_COUNTER_N RN
---------- --- --------------- ----------
9640 ALO 317 1
1000 MSP 25 1
9659 MSP 25 1
SQL>
Or, using the oldfashioned way:
14 SELECT *
15 FROM test t
16 WHERE t.usage_counter_n = (SELECT MAX (t1.usage_counter_n)
17 FROM test t1
18 WHERE t1.sensor_id_c = t.sensor_id_c)
19 ORDER BY sensor_id_c;
ID_N SEN USAGE_COUNTER_N
---------- --- ---------------
9640 ALO 317
9659 MSP 25
1000 MSP 25
SQL>
Depending on your requirements to handle duplicates, the KEEP clause might also be a useful option
SQL> with test
2 AS (SELECT 9640 ID_n, 'ALO' sensor_id_c, 317 usage_counter_n FROM DUAL
3 UNION
4 SELECT 9641, 'ALO', 18 FROM DUAL
5 UNION
6 SELECT 9642, 'ALO', 0 FROM DUAL
7 UNION
8 SELECT 9659, 'MSP', 25 FROM DUAL --> MAX for MSP ...
9 UNION
10 SELECT 9660, 'MSP', 10 FROM DUAL
11 UNION
12 SELECT 1000, 'MSP', 25 FROM DUAL --> ... but this is also MAX for MSP
13 )
14 SELECT sensor_id_c,
15 min(id_n) keep ( dense_rank first order by usage_counter_n DESC ) id_n,
16 min(usage_counter_n) keep ( dense_rank first order by usage_counter_n DESC ) usage_counter_n
17 from test
18 group by sensor_id_c;
SEN ID_N USAGE_COUNTER_N
--- ---------- ---------------
ALO 9640 317
MSP 1000 25

Find closest or higher values in SQL

I have a table:
table1
rank value
1 10
25 120
29 130
99 980
I have to generate the following table:
table2
rank value
1 10
2 10
3 10
4 10
5 10
6 10
7 10
8 10
9 10
10 10
11 10
12 10
13 120
14 120
15 120
.
.
.
.
25 120
26 120
27 130
28 130
29 130
30 130
.
.
.
.
.
62 980
63 980
.
.
.
99 980
100 980
So, table2 should have all values from 1 to 100. There are 3 cases:
If it's an exact match, for ex. rank 25, value would be 120
Find closest, for ex. for rank 9 in table2, we do NOT have exact match, but 1 is closest to 9 (9-1 = 8 whereas 25-9 = 16), so assign value of 1
If there is equal distribution from both sides, use higher rank value, for ex. for rank 27, we have 25 as well as 29 which are equally distant, so take higher value which is 29 and assign value.
something like
-- your testdata
with table1(rank,
value) as
(select 1, 10
from dual
union all
select 25, 120
from dual
union all
select 29, 130
from dual
union all
select 99, 980
from dual),
-- range 1..100
data(rank) as
(select level from dual connect by level <= 100)
select d.rank,
min(t.value) keep(dense_rank first order by abs(t.rank - d.rank) asc, t.rank desc)
from table1 t, data d
group by d.rank;
If I understand well your need, you could use the following:
select num, value
from (
select num, value, row_number() over (partition by num order by abs(num-rank) asc, rank desc) as rn
from table1
cross join ( select level as num from dual connect by level <= 100) numbers
)
where rn = 1
This joins your table with the [1,100] interval and then keeps only the first row for each number, ordering by the difference and keeping, in case of equal difference, the greatest value.
Join hierarchical number generator with your table and use lag() with ignore nulls clause:
select h.rank, case when value is null
then lag(value ignore nulls) over (order by h.rank)
else value
end value
from (select level rank from dual connect by level <= 100) h
left join t on h.rank = t.rank
order by h.rank
Test:
with t(rank, value) as (
select 1, 10 from dual union all
select 25, 120 from dual union all
select 29, 130 from dual union all
select 99, 980 from dual )
select h.rank, case when value is null
then lag(value ignore nulls) over (order by h.rank)
else value
end value
from (select level rank from dual connect by level <= 100) h
left join t on h.rank = t.rank
order by h.rank
RANK RANK
---------- ----------
1 10
2 10
...
24 10
25 120
26 120
27 120
28 120
29 130
30 130
...
98 130
99 980
100 980
Here's an alternative that doesn't need a cross join (but does use a couple of analytic functions, so you'd need to test whether this is more performant for your set of data than the other solutions):
WITH sample_data AS (SELECT 1 rnk, 10 VALUE FROM dual UNION ALL
SELECT 25 rnk, 120 VALUE FROM dual UNION ALL
SELECT 29 rnk, 130 VALUE FROM dual UNION ALL
SELECT 99 rnk, 980 VALUE FROM dual)
SELECT rnk + LEVEL - 1 rnk,
CASE WHEN rnk + LEVEL - 1 < rnk + (next_rank - rnk)/2 THEN
VALUE
ELSE next_value
END VALUE
FROM (SELECT rnk,
VALUE,
LEAD(rnk, 1, 100 + 1) OVER (ORDER BY rnk) next_rank,
LEAD(VALUE, 1, VALUE) OVER (ORDER BY rnk) next_value
FROM sample_data)
CONNECT BY PRIOR rnk = rnk
AND PRIOR sys_guid() IS NOT NULL
AND LEVEL <= next_rank - rnk;
RNK VALUE
---------- ----------
1 10
2 10
... ...
12 10
13 120
... ...
24 120
25 120
26 120
27 130
28 130
29 130
30 130
... ...
63 130
64 980
65 980
... ...
98 980
99 980
100 980
N.B, I'm not sure why you have 62 and 63 as having a value of 980 - the mid point between 29 and 99 is 64.
Also, you'll see that I've used 100 + 1 instead of 101 - this is because if you wanted to parameterise things, you would replace 100 with the parameter - e.g. v_max_rank + 1
You can use cross join. Use the below query to get your result:
select t1.* from table1 t1
cross join
(select * from table1) t2
on (t1.rank=t2.rank);

How can I select a ranged rows with order from a table in Oracle?

In Oracle there is no limit, so I used this:
SELECT *
FROM (
SELECT m.*, ROWNUM r
FROM TABLE_NAME m
WHERE COL LIKE XYZ
ORDER BY ID ASC
)
WHERE r BETWEEN 10 AND 20;
But it is still not ordered. It is ordered in the 10 from 20, but not the entire table. How can I do that?
I want to ORDER THE ENTIRE TABLE with WHERE clause and get the ranged ones. The solution above only order within the range.
Maybe less confusing solution (in case anyone googled here):
SELECT a.* from (
SELECT b.*, ROWNUM r FROM (
SELECT * FROM table ORDER BY id ASC
) b WHERE (city LIKE '%abc%' OR city IS NULL)
) a where r between 5 and 10
So it is a better idea to put order right in the middle criteria, and then put other criteria in the second level, and put row number in the outter-most level.
You'll need one more level of nesting as far as I know, something like:
select b.* from (
select a.*, rownum rnum from (
select * from foo order by id
) a where rownum <= 20
) b where b.rnum >= 10;
Demo:
SQL> create table foo (id number);
Table created.
SQL> insert into foo
2 select round(dbms_random.value(0, 1000))
3 from dual
4 connect by level <= 15;
15 rows created.
SQL> commit;
Commit complete.
rownum gets "materialized" before the ordering, so your approach cannot work, as you've noticed:
SQL> select foo.*, rownum from foo order by id;
ID ROWNUM
---------- ----------
24 15
148 5
151 2
225 7
234 11
292 1
305 4
351 9
383 8
394 13
426 12
477 10
553 6
594 14
917 3
15 rows selected.
So nest it once to get row numbers after ordering:
SQL> select a.*, rownum from (
2 select * from foo order by id
3 ) a;
ID ROWNUM
---------- ----------
24 1
148 2
151 3
225 4
234 5
292 6
305 7
351 8
383 9
394 10
426 11
477 12
553 13
594 14
917 15
15 rows selected.
But you can't do a between with this though:
SQL> select a.*, rownum from (
2 select * from foo order by id
3 ) a where rownum between 5 and 10;
no rows selected
This is because rownum gets a value only once a row enters the result set.
And add a second layer to remove the first lines:
SQL> select id, rnum from (
2 select id, rownum rnum from (
3 select id from foo order by id
4 ) a where rownum <= 10
5 ) b where b.rnum >= 5;
ID RNUM
---------- ----------
234 5
292 6
305 7
351 8
383 9
394 10
6 rows selected.