用同一张表中的另一列筛选列。

huangapple go评论92阅读模式
英文:

Filter column with other column in the same table

问题

我正在学习SQL,我想使用ReportsTo的值来获取EmployeeId,然后使用它来获取FullName,而不使用JOIN。最终的结果应该显示谁有多少下属。

EmployeeID FullName ReportsTo
1 Nancy Davolio 2
2 Andrew Fuller NULL
3 Janet Leverling 2
4 Margaret Peacock 2
5 Steven Buchanan 2
6 Michael Suyama 5
7 Robert King 5
8 Laura Callahan 2
9 Anne Dodsworth 5
  1. Select
  2. (Select FirstName + ' ' + LastName
  3. From Employees
  4. Where EmployeeID = ReportsTo) as EmployeeName,
  5. Count(EmployeeID) as SubordinateAmount,
  6. ReportsTo
  7. From
  8. Employees
  9. Where
  10. ReportsTo is not Null
  11. Group By
  12. ReportsTo

结果应该如下:

FullName Subordinates
Andrew Fuller 5
Steven Buchanan 3
英文:

I am learning SQL and I want to use ReportsTo value to get the EmployeeId and use it to get FullName without using JOIN. The final results should show who has how many subordinates.

EmployeeID FullName ReportsTo
1 Nancy Davolio 2
2 Andrew Fuller NULL
3 Janet Leverling 2
4 Margaret Peacock 2
5 Steven Buchanan 2
6 Michael Suyama 5
7 Robert King 5
8 Laura Callahan 2
9 Anne Dodsworth 5
  1. Select
  2. (Select FirstName + ' ' + LastName
  3. From Employees
  4. Where EmployeeID = ReportsTo) as EmployeeName,
  5. Count(EmployeeID) as SubordinateAmount,
  6. ReportsTo
  7. From
  8. Employees
  9. Where
  10. ReportsTo is not Null
  11. Group By
  12. ReportsTo

The result should be like:

FullName Subordinates
Andrew Fuller 5
Steven Buchanan 3

I have tried using a subquery by EmployeeID = ReportsTo, but it returns Null, and I can't figure out what to use.

答案1

得分: 0

你可以像这样做:

  1. select *
  2. into #data
  3. from (
  4. VALUES (1, N'Nancy Davolio', N'2')
  5. , (2, N'Andrew Fuller', NULL)
  6. , (3, N'Janet Leverling', N'2')
  7. , (4, N'Margaret Peacock', N'2')
  8. , (5, N'Steven Buchanan', N'2')
  9. , (6, N'Michael Suyama', N'5')
  10. , (7, N'Robert King', N'5')
  11. , (8, N'Laura Callahan', N'2')
  12. , (9, N'Anne Dodsworth', N'5')
  13. ) t (EmployeeID,FullName,ReportsTo)
  14. select COUNT(*)
  15. , (select fullname from #data d2 where d2.EmployeeID = d.reportsto)
  16. from #data d
  17. where ReportsTo is not null
  18. group by reportsto

它通过相关子查询获取必要的数据。但我不太确定为什么不愿意使用连接,通常使用连接更好而且更清晰。

英文:

You can do something like this i guess:

  1. select *
  2. into #data
  3. from (
  4. VALUES (1, N'Nancy Davolio', N'2')
  5. , (2, N'Andrew Fuller', NULL)
  6. , (3, N'Janet Leverling', N'2')
  7. , (4, N'Margaret Peacock', N'2')
  8. , (5, N'Steven Buchanan', N'2')
  9. , (6, N'Michael Suyama', N'5')
  10. , (7, N'Robert King', N'5')
  11. , (8, N'Laura Callahan', N'2')
  12. , (9, N'Anne Dodsworth', N'5')
  13. ) t (EmployeeID,FullName,ReportsTo)
  14. select COUNT(*)
  15. , (select fullname from #data d2 where d2.EmployeeID = d.reportsto)
  16. from #data d
  17. where ReportsTo is not null
  18. group by reportsto

It fetches the necessary data by correlated subquery. But i'm not sure why the reluctance to use a join, it's "usually" better and more clearer to use

huangapple
  • 本文由 发表于 2023年7月14日 06:04:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定