Running total of rows by ID - sql

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

Related

How to display total count in descending order but group by id

i have the following codes which displays the following result
select distinct
srcaccountid,
srccharid,
srccharname,
action,
itemname,
sum(itemcount) over (partition by srccharname, itemname)
as total_count,
sum(price) over(partition by srccharname, itemname)
as total_price
from
itemlog
where
action = 6
and logtime >='2023-02-13'
order by
total_count desc,
srcaccountid
the result is as follows
srcaccountid srccharid srccharname action itemname total_count total_price
1 21 abc 6 dog 2222 231
2 22 sdd 6 cat 1234 122
1 21 abc 6 cat 324 77
1 21 abc 6 mouse 122 32
2 22 sdd 6 mouse 12 3
i will like the result to show as follow
srcaccountid srccharid srccharname action itemname total_count
total_price
1 21 abc 6 dog 2222 231
1 21 abc 6 cat 324 77
1 21 abc 6 mouse 122 32
2 22 sdd 6 cat 1234 122
2 22 sdd 6 mouse 12 3
i cant seem to be able to show the highest sales amount while grouping the ids together and not splitting them up
Change the order by. Put the srcaccountid first.
order by
srcaccountid,
total_count desc

SQL Server : how to count a column that if exist duplicate data, it reuse duplicate data first exist count number

Table A
shop
amount
count
sameShopCount
shop5
100
1
1
shop2
99
2
1
shop3
98
3
1
shop4
97
4
1
shop1
96
5
1
shop2
95
6
2
shop4
94
7
2
shop5
93
8
2
shop5
92
9
3
shop1
91
10
2
shop5
90
11
4
shop3
89
12
2
Expected Result (order by amount desc):
shop
amount
expected result
shop5
100
1
shop2
99
2
shop3
98
3
shop4
97
4
shop1
96
5
shop2
95
2
shop4
94
4
shop5
93
1
shop5
92
1
shop1
91
5
shop5
90
1
shop3
89
3
I want to count shop column similar to count column in Table A. But also if shop exist more than 1 time it will reuse the first exist count number.
How can I achieved this with/without a temp table in SQL Server respectively? (SQL Server 2014 - build v12.0.6108.1)
I had tried something like:
ROW_NUMBER() OVER (ORDER BY amount DESC)
DENSE_RANK() OVER (PARTITION BY shop ORDER BY amount DESC)
Try using max and dense_rank window functions as the following:
with max_shop_amount as
(
select *,
max(amount) over (partition by shop) as mx
from table_name
)
select shop, amount,
dense_rank() over (order by mx desc) expected
from max_shop_amount
order by amount desc
See demo

SQL Temp table Array to perfrom rolling caluclations

I wish to use some sort of SQL array to subtract values from a certain row (QTYOnHand) that decreases that row value every time and throws it into a rolling calculation for the other rows. I've been thinking of some sort of Self Join/Temp Table solution, but not sure how to formulate. Also, All the results will be partitioned by the ItemID below. Help would be appreciated.
Here's some data, If I do a simple row by row subtraction I will get this: 17-3 = 14, 17-5 = 12 and so on.
(Item_ID) (ItemQty) (QTYOnHand) (QtyOnHand - ItemQty)
123 3 17 14
123 5 17 12
123 4 17 13
456 7 12 5
456 8 12 4
456 2 12 10
456 3 12 9
789 2 6 4
789 2 6 4
789 2 6 4
These are the results that I want, where I subtract every next value from the new QTYOnHand-ItemQty column value. Looks like 17-3 then 14 -5 then 9 -4 for Item_ID (123):
(Item_ID) (ItemQty) (QTYOnHand) (QtyOnHand - ItemQty)
123 3 17 14
123 5 17 9
123 4 17 5
456 7 12 5
456 8 12 -3
456 2 12 -5
456 3 12 -8
789 2 6 4
789 2 6 2
789 2 6 0
try the following:
;with cte as
(
select *, ROW_NUMBER() over (partition by Item_ID order by Item_ID) rn
from YourTable
)
, cte2 as
(
select Item_ID, ItemQty, QTYOnHand, Case when rn = 1 then QTYOnHand else 0 end - ItemQty as calc, rn
from cte
)
select Item_ID, ItemQty, QTYOnHand, sum(calc) over (partition by Item_ID order by rn) as [QtyOnHand - ItemQty]
from cte2 t1
Please find the db<>fiddle here.

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

count of a column in the result set on which distinct is already applied

Consider the table Property.
KeyIdNum|Property|IdNum
1 12 1234
1 12 1234
1 44 1234
1 12 1234
1 56 1234
2 12 4567
3 12 6789
3 56 6789
3 12 6789
4 44 3434
5 12 4444
6 44 9999
6 44 9999
It contains property num associated with each id num.But it contains duplicates.
I applied distinct to avoid duplicates.
select distinct KeyIdNum,Property,IdNum from Property.
So i got the result as :
KeyIdNum |Property |IdNum
1 12 1234
1 44 1234
1 56 1234
2 12 4567
3 12 6789
3 56 6789
4 44 3434
5 12 4444
6 44 9999
But now I want to `select( after applying distinct) ,the KeyIdNum (or IdNum) which are coming more than one time in the distinct result set shown above.
Please help me on this.I am not able to find a way to get the count of a column in the distinct result set using a single query.
Below query will result of KeyidNum , its number of row count.
select KeyIdNum,count(KeyIdNum)
From (
select distinct KeyIdNum,Property,IdNum from Property )
group by KeyIdNum
select KeyIdNum,count(KeyIdNum) as count
From (
select distinct KeyIdNum,Property,IdNum from Table19 )A
group by KeyIdNum
output
KeyIdNum count
1 3
2 1
3 2
4 1
5 1
6 1
This answer uses t-sql:
SELECT x
FROM ( SELECT * ,
rn = rownumber() OVER ( PARTITION BY keyidnum, idnum
ORDER BY keyidnum, idnum )
FROM tblProperty
) x
WHERE rn > 1