关于sql查询数据很棘手的问题,劳烦各位大侠帮忙
现在有一个user表,字段有ID,ParentId
如图
还有一个UserMoneyLog表,字段有UserID,moneychange,IsOk
如图
User的parentID和ID是递归,现在想通过两个表结合,ID为940没有父,也就是最高。ID 944为940的子,ID945为944的子。以此类推944为最高时,他的下级与下下级的。
在图2中moneyChange总共的值,取绝对值并且Isok=0。子节点的总值大于50,那么为N_UserMoneyLog插入一条数据,子节点的子节点的总值大于100,那么为N_UserMoneyLog插入一条数据。
不知道我这样描述大家是否能看懂。在线等回复。 美女 你到底要什么结果?你把你想要的结果贴出来吧!还有测试数据能不能不要贴图片啊
[解决办法]
CREATE TABLE t_tt(id INT,pId INT)
INSERT t_tt SELECT 940,0
UNION ALL SELECT 944,940
UNION ALL SELECT 945,0
UNION ALL SELECT 946,945
UNION ALL SELECT 947,946
UNION ALL SELECT 948,947
GO
CREATE FUNCTION F_GetNode(@Id INT,@PId INT)
RETURNS @t_L TABLE(Id INT,Pid INt,L INT)
AS
BEGIN
DECLARE @L int
SET @L=0
INSERT @t_L SELECT @Id,@PId,@L
WHILE @@ROWCOUNT>0
BEGIN
SET @L=@L+1
INSERT @t_L SELECT a.Id,a.Pid,@L
FROM t_tt a,@t_L b
WHERE a.Pid=b.Id AND b.L=@L-1
END
RETURN
END
GO
SELECT * from dbo.F_GetNode(945,0) WHERE L<=2
--RESULT
/*Id Pid L
----------- ----------- -----------
945 0 0
946 945 1
947 946 2
(所影响的行数为 3 行)
*/
select 940 as id,0 ParentId
into #User
union all select 944 as id,940 ParentId
union all select 945 as id,0 ParentId
union all select 946 as id,945 ParentId
union all select 947 as id,946 ParentId
union all select 948 as id,947 ParentId
select 60700 as id,945 as UserId,500.0 as MoneyChange ,1 isOk
into #UserMoneyLog
union all select 60701 as id,946 as UserId,100.0 as MoneyChange ,1 isOk
union all select 60702 as id,945 as UserId,0.3 as MoneyChange ,0 isOk
union all select 60703 as id,946 as UserId,-2.5 as MoneyChange ,0 isOk
union all select 60704 as id,945 as UserId,0.005 as MoneyChange ,0 isOk
union all select 60705 as id,946 as UserId,-0.4 as MoneyChange ,0 isOk
union all select 60706 as id,945 as UserId,0.0008 as MoneyChange ,0 isOk
union all select 60707 as id,946 as UserId,-2.5 as MoneyChange ,0 isOk
union all select 60708 as id,945 as UserId,0.005 as MoneyChange ,0 isOk
union all select 60709 as id,947 as UserId,100 as MoneyChange ,1 isOk
union all select 60710 as id,946 as UserId,0.3 as MoneyChange ,0 isOk
union all select 60711 as id,945 as UserId,0.2 as MoneyChange ,0 isOk
union all select 60712 as id,948 as UserId,100.0 as MoneyChange ,1 isOk
union all select 60713 as id,947 as UserId,0.3 as MoneyChange ,0 isOk
union all select 60714 as id,946 as UserId,0.2 as MoneyChange ,0 isOk
union all select 60715 as id,948 as UserId,0.85 as MoneyChange ,0 isOk
union all select 60716 as id,947 as UserId,0.005 as MoneyChange ,0 isOk
union all select 60717 as id,946 as UserId,0.005 as MoneyChange ,0 isOk
union all select 60718 as id,945 as UserId,0.01 as MoneyChange ,0 isOk
union all select 60719 as id,947 as UserId,8.5 as MoneyChange ,0 isOk
union all select 60720 as id,946 as UserId,0.05 as MoneyChange ,0 isOk
union all select 60721 as id,945 as UserId,0.1 as MoneyChange ,0 isOk
;with tb as
(
select parentid,id
from #User
union all
select a.parentid,b.id
from tb a join #User b
on a.id=b.parentid
)
select parentid as userid,abs(sum(MoneyChange)) sumMC
from tb t1 join #UserMoneyLog t2
on t1.id=t2.userid and t2.isok=0
group by parentid