例如 根据dqty1 数值分行,并记录第几行
表 TID
cname dqty1
A 2
B 3
C 4
在查询或者是视图中,怎么变成
cname dqty count
A 2 1
A 2 2
B 3 1
B 3 2
B 3 4
C 4 1
C 4 2
C 4 3
C 4 4
;WITH CTE AS
(
SELECT cname,dqty1,dqty1 AS row
FROM TID
UNION ALL
SELECT cname,dqty1,row-1
FROM CTE
WHERE row>1
)
SELECT * FROM CTE
ORDER BY CTE.cname,CTE.dqty1,CTE.row
OPTION (MAXRECURSION 0)
来源:oschina
链接:https://my.oschina.net/ljfxp/blog/3215530