sql – 从结果查询中选择count(*)

前端之家收集整理的这篇文章主要介绍了sql – 从结果查询中选择count(*)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要你的帮助,这是我的SQL查询
  1. select count(SID)
  2. from Test
  3. where Date = '2012-12-10'
  4. group by SID

这是我的结果:

  1. |2|
  2. |3|
  3. |4|
  4. |3|

现在我必须先从第一个查询中算出结果!

  1. Expected result: 4

解决方法

您可以将查询包装在另一个SELECT中:
  1. select count(*)
  2. from
  3. (
  4. select count(SID) tot -- add alias
  5. from Test
  6. where Date = '2012-12-10'
  7. group by SID
  8. ) src; -- add alias

SQL Fiddle with Demo

为了使其工作,计数(SID)需要列别名,您必须为子查询本身提供一个别名.

猜你在找的MsSQL相关文章