SQL Countif函数

前端之家收集整理的这篇文章主要介绍了SQL Countif函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题在这里已经有一个答案:> Sql Server equivalent of a COUNTIF aggregate function6
我有一张桌子,如下所示
Type of Station | Broadcast Management
----------------+-------------------------
Full Power      | Sinclair Broadcast Group
Full Power      | Sinclair Broadcast Group
LPTV cable      | Sinclair Broadcast Group
LPTV no cable   | Sinclair Broadcast Group

现在我想执行一个查询,结果将会如下所示

Broadcast Management       | Full Power | LPTV cable | LPTV no cable
---------------------------+------------+------------+--------------
Sinclair Broadcast Group   |  2         |     1      |  1

任何人都可以帮助我如何写这个查询

解决方法

没有单个SUMIF或COUNTIF.

但您确实有SUMOR COUNT和IF使用CASE …

SELECT
  [Broadcast Management],SUM(CASE WHEN [Type of Station] = 'Full Power'    THEN 1 ELSE 0 END)   AS [Full Power],SUM(CASE WHEN [Type of Station] = 'LPTV Cable'    THEN 1 ELSE 0 END)   AS [LPTV Cable],SUM(CASE WHEN [Type of Station] = 'LPTV No Cable' THEN 1 ELSE 0 END)   AS [LPTV No Cable]
FROM
  yourTable
GROUP BY
  [Broadcast Management]

对于计数,您可以使ELSE返回NULL,因为计数为1,2,4,NULL为3.

原文链接:https://www.f2er.com/mssql/75338.html

猜你在找的MsSQL相关文章