sql 递归查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 递归查询相关的知识,希望对你有一定的参考价值。
我有一张user表,里面有员工,部长,商务初审和商务终审。现在如果是商务初审登录的话,我要看到当前登录这个人的下属(部长)以及下属的下属(员工)。 表结构如下 UserSmallType(0:员工 1:部长 2:商务初审 3:商务终审)
LoginName UserName UserSmallType UpperPersonSid sid(主键)
5444 袁芳 3 null 1
4010 张三 2 1 2
4201 李四 1 2 3
4521 王五 0 3 4
4234 赵六 1 2 5
4531 田七 0 5 6
如果我登录的是 4010 就要把他的下属李四和赵六以及李四的下属王五 和赵六的下属田七。 最好不要用存储过程。求高手解答啊!!!
union all
select t1.* from tb_user t1 join tmp t2 on t2.sid=t1.UpperPersonSid )
select sid from tmp 参考技术B select * from user
where user.UserSmallType < (
select UserSmallType from user
where user.LoginName = '4010'
) 参考技术C 不用存储过程?可以用函数吗?
递归 SQL 查询 2008
【中文标题】递归 SQL 查询 2008【英文标题】:Recursive Sql Query 2008 【发布时间】:2015-07-13 11:30:12 【问题描述】:我需要计算indebtedness
列的值,以便当openingBalance !=0 then indebtedness = openingBalnce+SalesTotal-SalesReturn
时。但是,当之前的monthSales
的openingBalnce = 0 then indebtedness = indebtedness
与相同的SalesID
。如果previous value = 0
得到previous value
并继续得到previous value
直到在此列中有值:
SalesMonth SalesID openingBalance SalesTotal SalesReturn Indebtednes
1 1 352200 0 5600 NULL
1 2 50000 1100 0 NULL
1 3 9500 6000 0 NULL
2 1 0 0 1200 NULL
2 2 0 300 0 NULL
2 3 0 500 1000 NULL
3 1 0 600 0 NULL
3 2 0 200 0 NULL
3 3 0 0 10 NULL
.
.
.
12 1 0 0 0 NULL
12 2 0 0 0 NULL
12 3 0 0 0 NULL
输出如下:
when openingBalance !=0 then Indebtednes=openingBalnce+SalesTotal-SalesReturn
when openingBalnce =0 then Indebtednes=Indebtednes (of the previous
month of the same SalesID)+SalesTotal-SalesReturn.
And this is the output i want.
SalesMonth SalesID openingBalance SalesTotal SalesReturn Indebtednes
---------- ------- -------------- ---------- ----------- ------------
1 1 352200 0 5600 346600
------------------------------------------------------------------------
1 2 50000 1100 0 51100
------------------------------------------------------------------------
1 3 9500 6000 0 15500
------------------------------------------------------------------------
2 1 0 0 1200 345400
------------------------------------------------------------------------
2 2 0 300 0 51400
------------------------------------------------------------------------
2 3 0 500 1000 15000
------------------------------------------------------------------------
3 1 0 600 0 346000
------------------------------------------------------------------------
3 2 0 200 0 51600
-----------------------------------------------------------------------
3 3 0 0 10 14990
-----------------------------------------------------------------------
.
.
.
12 1 0 0 0 NULL
----------------------------------------------------------------------
12 2 0 0 0 NULL
----------------------------------------------------------------------
12 3 0 0 0 NULL
【问题讨论】:
你也可以添加预期的输出 对于 salesMonth=1 和 SalesID=1 Indebtednes =346600 我的意思是预期的样本输出,其数据基于问题中的上述样本输入。您可以编辑问题并添加详细信息 for salesMonth=1 and SalesID=1 Indebtednes =346600 salesMonth=2 and salesID =1 indebtedness = 346600 and the same case with SalesID =2,3 when openingBalnce !=0 then indebtedness=openingBalnce+SalesTotal -SalesReturn 但是,当openingBalance = 0 获取相同SalesID 的上个月的Indebtednes 时,问题是如果上一个indebtedness 为0 获取上个月的值并继续获取上个月的上一个值直到得到值,怎么办这??? 【参考方案1】:您可以执行CROSS APPLY
或共同相关的子查询。像这样的东西。 SQL Fiddle
样本数据
DECLARE @Sales TABLE (SalesMonth INT,SalesID INT,openingBalance MONEY,SalesTotal MONEY,SalesReturn MONEY,Indebtednes MONEY)
insert into @Sales
VALUES(1, 1, 352200, 0, 5600, Null),
(1, 2, 50000, 1100, 0, Null),
(1, 3, 9500, 6000, 0, Null),
(2, 1, 0, 0, 1200, Null),
(2, 2, 0, 300, 0, Null),
(2, 3, 0, 500, 1000, Null),
(3, 1, 0, 600, 0, NULL),
(3, 2, 0, 200, 0, NULL),
(3, 3, 0, 0, 10, NULL)
选择查询
SELECT C1.SalesMonth,C1.SalesID,C1.openingBalance,C1.SalesTotal,C1.SalesReturn,C2.Indebt
FROM @Sales C1
CROSS APPLY (
SELECT TOP 1 CASE WHEN openingBalance = 0 THEN NULL ELSE openingBalance + SalesTotal - SalesReturn END Indebt
FROM @Sales C2 WHERE C2.SalesID = C1.SalesID AND openingBalance <> 0 AND C2.SalesMonth <=C1.SalesMonth ORDER BY SalesMonth Desc
) C2
更新查询
UPDATE C1
SET Indebtednes =
(
SELECT TOP 1 CASE WHEN openingBalance = 0 THEN NULL ELSE openingBalance + SalesTotal - SalesReturn END Indebt
FROM @Sales C2 WHERE C2.SalesID = C1.SalesID AND openingBalance <> 0 AND C2.SalesMonth <=C1.SalesMonth ORDER BY SalesMonth Desc
)
FROM @Sales C1
SELECT * FROM @Sales
【讨论】:
请再次查看我的帖子并给我答案,我用我想要的输出编辑了帖子,谢谢你的帮助:)以上是关于sql 递归查询的主要内容,如果未能解决你的问题,请参考以下文章