Update sequence in table - Oracle - sql

I have a table:
table1
u_a_id element_id my_seq line_num
1 HI01-01 1 30
1 HI01-02 1 30
1 HI01-01 1 31
1 HI01-02 1 31
1 HI02-03 1 31
1 HI02-04 1 31
2 HI01-01 1 40
2 HI01-02 1 40
2 HI02-01 1 40
2 HI02-02 1 40
2 HI02-03 1 40
2 HI02-04 1 40
2 HI03-02 1 41
2 HI03-03 1 41
2 HI05-04 1 41
2 HI05-05 1 41
I need to update my_seq if a new HI01 appears in the same u_a_id or the counter for HI changes, for ex. HI01 -> HI02 for each u_a_id order by line_num.
I have this query, however this gives seq as 1 even for second instance of HI01-01 in u_a_id = 1:
select t.*,
dense_rank() over (partition by u_a_id order by substr(element_id, 1, 4)) as new_my_seq
from table1 t
The output would look like:
u_a_id element_id my_seq line_num
1 HI01-01 1 30
1 HI01-02 1 30
1 HI01-01 2 31
1 HI01-02 2 31
1 HI02-03 3 31
1 HI02-04 3 31
2 HI01-01 1 40
2 HI01-02 1 40
2 HI02-01 2 40
2 HI02-02 2 40
2 HI02-03 2 40
2 HI02-04 2 40
2 HI03-02 3 41
2 HI03-03 3 41
2 HI05-04 4 41
2 HI05-05 4 41
Is there a way in Oracle SQL to achieve this?

I think you just want:
select t.*,
dense_rank() over (partition by u_a_id
order by line_num,
substr(element_id, 1, 4)
) as my_seq
from t;

You may want to order by line_num instead of substr(element_id, 1, 4) and place substr(element_id, 1, 4) in partition by.
select t.*,
dense_rank() over (partition by u_a_id, substr(element_id, 1, 4) order by line_num) as new_my_seq
from table1 t

Related

Cumulative sum in SQL using window function

QTY
STOCK
RNK
ID KEY
CUM SUM
40
35
1
1
35
20
35
2
1
0
15
35
3
1
0
58
35
4
1
0
18
35
5
1
0
40
35
1
2
35
20
35
2
2
0
15
35
3
2
0
CUM SUM should be MIN(QTY, STOCK-SUM(all rows in cumsum before the current row)) for every other row and for 1st row it should be MIN(QTY, STOCK-SUM(0))=> MIN(QTY,STOCK)
QTY
STOCK
RNK
ID KEY
CUM SUM
40
35
1
1
5
20
35
2
1
-10
15
35
3
1
-30
58
35
4
1
-7
18
35
5
1
-24
40
35
1
2
5
20
35
2
2
-10
15
35
3
2
-30
After, I tried I am getting the above output
SELECT sum(qty-stock) over (
partition by ID KEY
ORDER BY rnk
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) as CUM SUM
FROM TABLE
Need to get correct cumsum value using a window function in the existing table
You may use a rolling SUM() here, using SUM() as an analytic function:
SELECT *, SUM(QTY - STOCK) OVER (PARTITION BY ID_KEY ORDER BY RNK) AS CUM_SUM
FROM yourTable
ORDER BY ID_KEY, RNK;

Restart Row Number Based on Date and Increments of N

I have a table with the following data that I generated with a date table
date day_num (DAY_NUM % 7)
2019-07-09 0 0
2019-07-10 1 1
2019-07-11 2 2
2019-07-12 3 3
2019-07-13 4 4
2019-07-14 5 5
2019-07-15 6 6
2019-07-16 7 0
I basically want to get a week number that restarts at 0 and I need help figuring out the last part
The final output would look like this
date day_num (DAY_NUM % 7) week num
2019-07-09 0 0 1
2019-07-10 1 1 1
2019-07-11 2 2 1
2019-07-12 3 3 1
2019-07-13 4 4 1
2019-07-14 5 5 1
2019-07-15 6 6 1
2019-07-16 7 0 2
This is the sql I have so far
select
SUB.*,
DAY_NUM%7
FROM(
SELECT
DISTINCT
id_date,
row_number() over(order by id_date) -1 as day_num
FROM schema.date_tbl
WHERE Id_date BETWEEN "2019-07-09" AND date_add("2019-07-09",146)
Building on your query:
select SUB.*, DAY_NUM%7,
DENSE_RANK() OVER (ORDER BY FLOOR(DAY_NUM / 7)) as weeknum
FROM (SELECT DISTINCT id_date,
row_number() over(order by id_date) -1 as day_num
FROM schema.date_tbl
WHERE Id_date BETWEEN "2019-07-09" AND date_add("2019-07-09", 146)
) x

How to reorder a DENSE_RANK column considering multiple columns in Oracle?

I have a query following which have a DENSE_RANK and ROW_NUMBER columns:
WITH CTE AS(
SELECT
A.SL_NO,
A.ACC_NO,
A.ACC_NAME
DENSE_RANK() OVER(ORDER BY A.ACC_NO, A.ACC_NAME) DRN,
ROW_NUMBER() OVER(PARTITION BY A.ACC_NO, A.ACC_NAME ORDER BY A.SL_NO) RN
FROM TEST_TBL A
)
SELECT *
FROM CTE A
ORDER BY A.SL_NO;
The query results in:
SL_NO ACC_NO ACC_NAME DRN RN
1 234 UNIP 3 1
2 234 UNIP 3 2
3 234 UNIP 3 3
4 256 PURP 4 1
5 256 PURP 4 2
6 289 KFAR 5 1
7 210 FHAS 2 1
8 210 FHAS 2 2
9 210 FHAS 2 3
10 110 PURP 1 1
11 110 PURP 1 2
12 110 PURP 1 3
13 110 PURP 1 4
But do wanna order the DRN column like this (The rank must be according to the acc_no and acc_name columns):
SL_NO ACC_NO ACC_NAME DRN RN
1 234 UNIP 1 1
2 234 UNIP 1 2
3 234 UNIP 1 3
4 256 PURP 2 1
5 256 PURP 2 2
6 289 KFAR 3 1
7 210 FHAS 4 1
8 210 FHAS 4 2
9 210 FHAS 4 3
10 110 PURP 5 1
11 110 PURP 5 2
12 110 PURP 5 3
13 110 PURP 5 4
Need suggestions to achieve it with or without using DENSE_RANK. Thanks in advance.
It looks like you just want the DRN column to be a dense rank as ordered by the SL_NO column. We can subquery once, and arbitrary take the minimum SL_NO value per each account, and then use dense rank afterwards:
WITH cte AS (
SELECT
SL_NO,
ACC_NO,
ACC_NAME,
ROW_NUMBER() OVER(PARTITION BY ACC_NO, ACC_NAME ORDER BY SL_NO) RN,
MIN(SL_NO) OVER (PARTITION BY ACC_NO, ACC_NAME) AS SL_NO_MIN
FROM TEST_TBL A
)
SELECT
SL_NO,
ACC_NO,
ACC_NAME,
DENSE_RANK() OVER (ORDER BY SL_NO_MIN) AS DRN,
RN
FROM cte
ORDER BY
SL_NO;
Demo

Find duplicates on multiple columns in a SQL

I want to find the batchID who are having subjectID(0,1,2) only, pls help me, thanks in advance
I need answer BatchID=12 and BatchID=51, what can I do in sql
MyTable
uid BatchID SubjectID
6 2 0
7 2 1
8 2 2
9 2 4
10 3 0
11 3 1
12 4 5
13 4 0
14 5 5
15 6 5
17 7 0
18 7 1
19 7 2
26 12 0
27 12 1
28 12 2
29 1 0
30 1 1
31 1 4
62 45 5
63 46 0
64 46 1
65 46 4
107 49 6
108 49 2
109 49 4
110 50 1
111 50 3
116 0 1
117 0 4
118 51 0
119 51 1
120 51 2
You can use conditional aggregation for this:
select batchId
from your_table
group by batchId
having count(distinct case when subjectID in (0,1,2) then subjectID end) = 3
and count(case when subjectID not in (0,1,2) then 1 end) = 0
Explanation:
Group by batchId - Aggregate on batchId
count(distinct case when subjectID in (0,1,2) then subjectID end) - Produces 3 only if all three of them are present for this batchId
count(case when subjectID not in (0,1,2) then 1 end) - Produces 0 if there is no other subjectID except 0,1,2 assuming nulls are not allowed in the subjectId column.
You can use Row_Number()
;with cte as (
select *, RowN = row_number() over (partition by batchid order by uid) from #yourbatch where subjectid in (0,1,2)
) select distinct BatchId from cte where RowN > 1

Running total of rows by ID

I have a list of IDs, transactions, and the date of those transactions. I want to create a count of each transaction within each ID.
The starting table I have is looks something like this:
id trxn_dt trxn_amt
1 10/31/2014 58
1 11/9/2014 34
1 12/10/2014 12
2 7/8/2014 78
2 11/20/2014 99
3 1/5/2014 120
4 2/17/2014 588
4 2/18/2014 8
4 3/9/2014 65
4 4/25/2014 74
and I want the end result to look something like this:
id trxn_dt trxn_amt trxn_count
1 10/31/2014 58 1
1 11/9/2014 34 2
1 12/10/2014 12 3
2 7/8/2014 78 1
2 11/20/2014 99 2
3 1/5/2014 120 1
4 2/17/2014 588 1
4 2/18/2014 8 2
4 3/9/2014 65 3
4 4/25/2014 74 4
Count(distinct(id)) would only give me the overall number of distinct IDs and not a running total by each ID that restarts at each new ID.
Thank you!
In SQL-Server you can use ROW_NUMBER in following:
SELECT id,
trxn_dt,
trxn_amt,
ROW_NUMBER() OVER(PARTITION BY Id ORDER BY Id, trxn_dt) AS trxn_count
FROM StarningTable
In MySQL you can do in following:
SELECT
t.id,
t.trxn_dt,
t.trxn_amt,
#cur:= IF(id=#id, #cur+1, 1) AS RowNumber,
#id := id
FROM
StarningTable t
CROSS JOIN
(SELECT #id:=(SELECT MIN(id) FROM StarningTable t), #cur:=0) AS init
ORDER BY
t.id
using Row_number we can achieve this
Select *,
ROW_NUMBER()OVER(PARTITION BY id ORDER BY (SELECT NULL))trxn_count
from Transactions