如何计算提交SQL Server的人数

时间:2016-08-09 06:42:20

标签: sql sql-server database

我不知道如何计算提交的人数并输出他们的列表。

Employees.ReportsTo表示此人提交的身份证。

Select
   Employees.LastName,
   Employees.FirstName,
   Region.RegionDescription,
   count(case 
         when Employees.ReportsTo=Employees.EmployeeID then 1 
         end) as Count_of_employees,
   Subordinates = STUFF((SELECT ', ' + Employees.LastName 
                         FROM Employees
                         WHERE Employees.ReportsTo = Employees.EmployeeID
                         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')  
from
   Employees  
left join
   EmployeeTerritories 
      on  Employees.EmployeeID=EmployeeTerritories.EmployeeID  
left join
   Territories 
      on  EmployeeTerritories.TerritoryID=Territories.TerritoryID  
left join
   Region 
      on Territories.RegionID=Region.RegionID  
group by
   Region.RegionDescription,
   Employees.LastName,
   Employees.FirstName

那是输出:

Last name  First Name     RegionDescription  Count_of_employees Subordinates 
Davolio    Nancy          Eastern              0                NULL
Fuller     Andrew         Eastern              0                NULL
Peacock    Margaret       Eastern              0                NULL
Buchanan   Steven         Eastern              0                NULL

这必须是:

Last name  First Name     RegionDescription  Count_of_employees Subordinates 
Davolio    Nancy          Eastern              0                NULL
Fuller     Andrew         Eastern              3        Davolio,Peacock, Buchanan      
Peacock    Margaret       Eastern              0                NULL
Buchanan   Steven         Eastern              0                NULL

来自员工的示例数据

FirstName   LastName    EmployeeID  ReportsTo
Nancy       Davolio     1           2
Andrew      Fuller      2           NULL
Janet       Leverling   3           2
Margaret    Peacock     4           2
Steven      Buchanan    5           2
Michael     Suyama      6           5
Robert      King        7           5
Laura       Callahan    8           2
Anne        Dodsworth   9           5

1 个答案:

答案 0 :(得分:0)

主要问题是您只在同一行上比较字段。由于Employees没有别名,因此SQL服务器不知道将内部选择中的Emploees.ReportsTo与主选择中的Employees.EmployeeID进行比较 - 它们看起来都是一样的。尝试将下属更改为:

Subordinates = STUFF((SELECT ', ' + Employees.LastName 
                     FROM Employees ESubordinate -- so SQL knows it's a "different" table
                     WHERE ESubordinate.ReportsTo = Employees.EmployeeID
                     FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')

此外,出于类似原因,请勿CASE使用COUNT -- the () are important here, and ECount differentiates the two Employees tables too (SELECT COUNT(1) FROM Employees ECount WHERE ECount.ReportsTo = Employees.ReportsTo) as Count_of_employees, 。使用另一个内部选择:

CROSS APPLY

那应该可以解决你的问题。请记住,它并不是最快的。如果您想要速度,请查看Z = tf.pow(Z, 2.0) summary_z = tf.scalar_summary('z', Z) #etc.. summary_merge = tf.merge_all_summaries() #on each desired step save: summary_str = sess.run(summary_merge) summary_writer.add_summary(summary_str, i)