10

我有一张如下表

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

谁能帮我写这个查询

4

1 回答 1

22

没有单一的SUMIFCOUNTIF

但是你确实有SUMorCOUNTIFusing 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, NULLis 3

于 2013-06-29T17:55:13.830 回答