0

下面是一组数据。

ID|NAME |AMOUNT|COUNTRY|No. ITEM|
1 |ARIES|23.00 |US     |2
2 |ARIES|13.00 |US     |3
3 |VINCE|22.00 |US     |4
4 |VINCE|32.00 |UK     |5

所需的输出。这在 SQL 命令中可能吗?计算美国和非美国的记录数量,并将美国和非美国的项目数量相加。

NAME |AMOUNT|US Count|NON US COUNT|US NO. ITEM|NON - US NO. ITEM
ARIES|36.00 |       2|           0|          5|               0
VINCE|22.00 |       1|           0|          4|               0
VINCE|32.00 |       0|           1|          4|               5 
4

1 回答 1

1
SELECT NAME,
       SUM(AMOUNT) AS AMOUNT,
       SUM(CASE WHEN COUNTRY = 'US' THEN 1 ELSE 0 END) AS US_COUNT,
       SUM(CASE WHEN COUNTRY = 'US' THEN 0 ELSE 1 END) AS NON_US_COUNT,
       SUM(CASE WHEN COUNTRY = 'US' THEN [No. ITEM] ELSE 0 END) AS US_NO_ITEM,
       SUM(CASE WHEN COUNTRY = 'US' THEN 0 ELSE [No. ITEM] END) AS NON_US_NO_ITEM
FROM   Table
GROUP BY NAME,
       COUNTRY
于 2013-08-22T07:04:48.917 回答