Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要SQL Server中3个表的层次结构数据_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

需要SQL Server中3个表的层次结构数据

需要SQL Server中3个表的层次结构数据,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我有以下表格: UserMaster: UserId Int,UserName Varchar(200),由Int添加 UserId EmpName AddedBy 1 admin 0 2 SubAdmin1 1 3 SubAdmin2 1 4 Vikas 2 5 Mohit

我有以下表格:

UserMaster

UserId Int,UserName Varchar(200),由Int添加

UserId     EmpName           AddedBy
1          admin             0
2          SubAdmin1         1
3          SubAdmin2         1    
4          Vikas             2      
5          Mohit             4
6          Atul              5 
7          Vishal            6
8          Mani              3
9          Sunny             8
销售主管

SalesId Int,UserId Int(FK\u UserMaster\u UserId),Price Int

SalesId  UserId  Price

1        1       100         
2        2       200      
3        3       300      
4        4       500     
5        5       100
6        6       200
7        7       111
8        8       222
9        9       333
案例1:现在我需要一个特定用户下的所有用户的总价格以及它自己的价格。 意味着如果我考虑用户ID=1,那么价格将被计算为所有用户,其中ADDIEBY=1的列值。 以及他们的下级员工。 表示将为具有用户ID的用户计算用户的总价为:1,2,3,4,5,6,7,8,9

案例2:类似地,如果我想在UserId=3(subadmin=2)下计算总价,那么将为具有UserId的用户计算salesMaster的总价:3,8,9

第一个案例的结果应为:

UserId     Price
1          2066
UserId     Price
3          300+222+333
第二种情况的结果应为:

UserId     Price
1          2066
UserId     Price
3          300+222+333
请帮忙

谢谢和问候 尼汀


欢迎来到SO。到目前为止你试过什么?