[英]how to give different row id to sub group in in a group in sql query?
i have bunch of discount scheme for my item table , and for each item i have different discount scheme. 我为我的商品表有很多折扣方案,而对于每件商品,我都有不同的折扣方案。 now i want to give row id to that item but it should be start from zer0(0) for each item group, and when it got different DiscountId then it should be change, my table is in below image..
现在我想为该项目提供行ID,但对于每个项目组,它应该从zer0(0)开始,并且当它获得不同的DiscountId时,应该进行更改,我的表格在下面的图像中。
now for an example, for ItemCode 429 there are 7 same discount with DiscountId 427 so for this all i want row Id 0(zero) but when change DiscountId, it means for Same ItemCode and 428 DiscountId, then i want another RowId with increment. 现在以一个示例为例,对于ItemCode 429,DiscountId 427有7个相同的折扣,因此,我想要的所有行ID为0(零),但是当更改DiscountId时,这意味着对于Same ItemCode和428 DiscountId,则我想要另一个具有增量的RowId。 and when ItemCode change then rowId should be start from Zero(0).
当ItemCode更改时,rowId应该从零(0)开始。
can anyone help me please?? 谁能帮我吗?
my current query is simpaly "select * from ItemDiscount_md". 我当前的查询是simpaly“ select * from ItemDiscount_md”。
Maybe something like this: 也许是这样的:
Test data: 测试数据:
DECLARE @tbl TABLE(ITEMCode INT,DiscountId INT)
INSERT INTO @tbl
VALUES
(73,419),(73,419),(73,420),(73,420),(73,420),
(429,427),(429,427),(429,427),(429,427),(429,427),
(429,427),(429,427),(429,427),(429,428),(429,428)
Query: 查询:
;WITH CTE
AS
(
SELECT
DENSE_RANK() OVER(PARTITION BY tbl.ITEMCode
ORDER BY DiscountId) AS Rownbr,
tbl.*
FROM
@tbl AS tbl
)
SELECT
CTE.Rownbr-1 AS RowNbr,
CTE.DiscountId,
CTE.ITEMCode
FROM
CTE
Of course you can simplify the query by writing this: 当然,您可以通过编写以下代码来简化查询:
SELECT
(DENSE_RANK() OVER(PARTITION BY tbl.ITEMCode
ORDER BY DiscountId))-1 AS Rownbr,
tbl.*
FROM
@tbl AS tbl
I just thought it was nicer and more readable with a CTE function 我只是觉得CTE功能更好,更易读
References: 参考文献:
EDIT 编辑
To answer the comment. 回答评论。 No ROW_NUMBER will not return the same counter.
没有ROW_NUMBER将不会返回相同的计数器。 This is the output with
DENSE_RANK
: 这是
DENSE_RANK
的输出:
0 419 73
0 419 73
1 420 73
1 420 73
1 420 73
0 427 429
0 427 429
0 427 429
0 427 429
0 427 429
0 427 429
0 427 429
0 427 429
1 428 429
1 428 429
And this is with ROW_NUMBER
: 这是
ROW_NUMBER
:
0 419 73
1 419 73
2 420 73
3 420 73
4 420 73
0 427 429
1 427 429
2 427 429
3 427 429
4 427 429
5 427 429
6 427 429
7 427 429
8 428 429
9 428 429
As you see ROW_NUMBER
() recounts the group when the DENSE_RANK
ranks the group 如您所见,当
DENSE_RANK
对组进行排名时, ROW_NUMBER
()重新计算了组
Just more simplified Arion's Answer 只是简化了Arion的答案
DECLARE @tbl TABLE(ITEMCode INT,DiscountId INT)
INSERT INTO @tbl
VALUES
(73,419),
(73,419),
(73,420),
(73,420),
(73,420),
(429,427),
(429,427),
(429,427),
(429,427),
(429,427),
(429,427),
(429,427),
(429,427),
(429,428),
(429,428)
;
SELECT
(DENSE_RANK() OVER(PARTITION BY ITEMCode ORDER BY DiscountId) -1) AS Rownbr,
DiscountId,
ITEMCode
FROM
@tbl
if you got data like this: (from the image) @temp table 如果您得到这样的数据:(来自图像)@temp表
itemcode DiscountId DayId
----------- ----------- -----------
102 416 2
102 416 3
102 416 4
79 419 3
79 419 1
79 420 2
79 420 1
use row_number() to get below result 使用row_number()获得以下结果
itemcode DiscountId DayId rowid
----------- ----------- ----------- --------------------
102 416 2 1
102 416 3 2
102 416 4 3
79 419 3 1
79 419 1 2
79 420 2 1
79 420 1 2
SQL example: SQL范例:
select itemcode, DiscountId, DayId
, ROW_NUMBER() over (partition by Discountid order by discountid) as 'rowid'
from @temp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.