Home > Software design >  How can I make an incremented by 1 serial based on multiple conditions Sql server?
How can I make an incremented by 1 serial based on multiple conditions Sql server?

Time:05-02

Edit #1 : The code could contain random letters and numbers like "A123BC56xa"

Edit #2 : I tried this subquery but it gave me Syntax Error

SELECT DocDtls.Warehouse, Transactions.Code, DocDtls.zDate, Transactions.ID, Transactions.QtyIn, Transactions.QtyOut, Transactions.BalanceAfter, (SELECT Count("*") FROM Transactions AS B WHERE  B.[ID]<A.[ID] And B.[Warehouse] = A.[Warehouse] And B.[Code] = A.[Code]) 1 AS Serial
FROM DocDtls INNER JOIN Transactions ON DocDtls.DocNum = Transactions.DocNum
ORDER BY DocDtls.Warehouse, Transactions.Code, DocDtls.zDate, Transactions.ID As A;

I have this query

SELECT DocDtls.Warehouse, DocDtls.zDate, Transactions.Code, Transactions.ID, Transactions.QtyIn, Transactions.QtyOut, Transactions.BalanceAfter
FROM DocDtls INNER JOIN Transactions ON DocDtls.DocNum = Transactions.DocNum
ORDER BY DocDtls.Warehouse, Transactions.Code, DocDtls.zDate, Transactions.ID;

This is the current query result :

Warehouse | zDate   |   ID  |   Code    |   QtyIn   |QtyOut |   BalanceAfter
MainWh  |   20-03-2022  |   7   |   00001   |   5000|   0   |   0
MainWh  |   21-03-2022  |   8   |   00001   |   0   |   1000|   0
MainWh  |   20-03-2022  |   9   |   00002   |   7000|   0   |   0
MainWh  |   21-03-2022  |   10  |   00002   |   0   |   2500|   0
Wh1     |   21-03-2022  |   1   |   00001   |   1000|   0   |   0
Wh1     |   25-04-2022  |   5   |   00001   |   0   |   250 |   0
Wh1     |   25-04-2022  |   5   |   00001   |   0   |   300 |   0
Wh1     |   21-03-2022  |   2   |   00002   |   2500|   0   |   0
Wh1     |   25-04-2022  |   6   |   00002   |   0   |   500 |   0

I need to add a serial column like ID but incremented by 1 based on Code And Warehouse and the result should be Like

Warehouse | zDate   |   ID  |   Code    |   QtyIn   |QtyOut |   BalanceAfter|   Serial
MainWh  |   20-03-2022  |   7   |   00001   |   5000|   0   |   0|               1  
MainWh  |   21-03-2022  |   8   |   00001   |   0   |   1000|    0|              2  
MainWh  |   20-03-2022  |   9   |   00002   |   7000|   0   |    0|              1  
MainWh  |   21-03-2022  |   10  |   00002   |   0   |   2500|   0|               2  
Wh1     |   21-03-2022  |   1   |   00001   |   1000|   0   |   0    |           1  
Wh1     |   25-04-2022  |   5   |   00001   |   0   |   250 |   0|               2  
Wh1     |   25-04-2022  |   5   |   00001   |   0   |   300 |   0|               3  
Wh1     |   21-03-2022  |   2   |   00002   |   2500|   0   |   0    |           1
Wh1     |   25-04-2022  |   6   |   00002   |   0   |   500 |   0|               2  

I'm using sqlserver 2014 , Thanks in advance .

CodePudding user response:

SQL server 2014 is very old, and should be urgently replaced.

You can use window functions like row_number to achieve your goal

CREATE TABLE DocDtls 
    ([Warehouse] varchar(6), [zDate] varchar(10), [ID] int, [Code] int, [QtyIn] int, [QtyOut] int, [BalanceAfter] int)
;
    
INSERT INTO DocDtls 
    ([Warehouse], [zDate], [ID], [Code], [QtyIn], [QtyOut], [BalanceAfter])
VALUES
    ('MainWh', '20-03-2022', 7, 00001, 5000, 0, 0),
    ('MainWh', '21-03-2022', 8, 00001, 0, 1000, 0),
    ('MainWh', '20-03-2022', 9, 00002, 7000, 0, 0),
    ('MainWh', '21-03-2022', 10, 00002, 0, 2500, 0),
    ('Wh1', '21-03-2022', 1, 00001, 1000, 0, 0),
    ('Wh1', '25-04-2022', 5, 00001, 0, 250, 0),
    ('Wh1', '25-04-2022', 5, 00001, 0, 300, 0),
    ('Wh1', '21-03-2022', 2, 00002, 2500, 0, 0),
    ('Wh1', '25-04-2022', 6, 00002, 0, 500, 0)
;
GO

9 rows affected

SELECT *,
ROW_NUMBER() OVER( PARTITION BY [Warehouse],[Code] ORDER BY [ID]) as Serial
FROM DocDtls
GO
Warehouse | zDate      | ID | Code | QtyIn | QtyOut | BalanceAfter | Serial
:-------- | :--------- | -: | ---: | ----: | -----: | -----------: | -----:
MainWh    | 20-03-2022 |  7 |    1 |  5000 |      0 |            0 |      1
MainWh    | 21-03-2022 |  8 |    1 |     0 |   1000 |            0 |      2
MainWh    | 20-03-2022 |  9 |    2 |  7000 |      0 |            0 |      1
MainWh    | 21-03-2022 | 10 |    2 |     0 |   2500 |            0 |      2
Wh1       | 21-03-2022 |  1 |    1 |  1000 |      0 |            0 |      1
Wh1       | 25-04-2022 |  5 |    1 |     0 |    250 |            0 |      2
Wh1       | 25-04-2022 |  5 |    1 |     0 |    300 |            0 |      3
Wh1       | 21-03-2022 |  2 |    2 |  2500 |      0 |            0 |      1
Wh1       | 25-04-2022 |  6 |    2 |     0 |    500 |            0 |      2

db<>fiddle here

CodePudding user response:

If the Code column is always in this format '00001', '00002', '00101', etc., you can use a cast.

SELECT DocDtls.Warehouse, DocDtls.zDate, Transactions.Code, Transactions.ID, Transactions.QtyIn, Transactions.QtyOut, Transactions.BalanceAfter
FROM DocDtls INNER JOIN Transactions ON DocDtls.DocNum = Transactions.DocNum
ORDER BY DocDtls.Warehouse, Transactions.Code, DocDtls.zDate, Transactions.ID, CAST(Transactions.Code AS INT) AS Serial;
  • Related