将数据行分组为列

时间:2019-07-25 22:12:54

标签: sql-server group-by

我正在尝试将看起来像这样的表中的数据分组:

Name                   Cupon
USULUTAN             AVAMYS27.5          
USULUTAN             PAXILCR25           
USULUTAN             AVAMYS27.5          
USULUTAN             AVAMYS27.5                       
UNICENTRO LOURDES    AVAMYS27.5          
UNICENTRO ALTAVISTA  AVAMYS27.5  

我有这个查询:

SELECT DISTINCT(SL.Name),
    CASE WHEN Cupon = 'AVAMYS27.5' THEN SUM(1) ELSE 0 END [AVAMYS],
    CASE WHEN Cupon = 'PAXILCR25' THEN SUM(1) ELSE 0 END PAXILCR25,
FROM [INTRANET].[coupon].[CuponGlaxo] CG 
JOIN INTRANET.intranet.[Store List] SL ON CG.IdSala = SL.[Store No_]
WHERE Transaccion IS NOT NULL
GROUP BY SL.Name, Cupon
ORDER BY SL.Name DESC

它给了我这些结果:

Name                 AVAMYS PAXILCR25
USULUTAN               0    0
USULUTAN               0    1
USULUTAN               3    0
UNICENTRO LOURDES      1    0
UNICENTRO ALTAVISTA    0    0
UNICENTRO ALTAVISTA    2    0

但是我需要这种方式:

Name                 AVAMYS PAXILCR25
USULUTAN               3    1
UNICENTRO LOURDES      1    0
UNICENTRO ALTAVISTA    2    0

0 个答案:

没有答案