0

我有这个枢轴查询:

SELECT `fonte`, 

MAX(IF(month= '1', sell, NULL)) AS Gen,  
MAX(IF(month= '2', sell, NULL)) AS Feb, 
MAX(IF(month= '3', sell, NULL)) AS Mar, 
MAX(IF(month= '4', sell, NULL)) AS Apr, 
MAX(IF(month= '5', sell, NULL)) AS Mag, 
MAX(IF(month= '6', sell, NULL)) AS Giu, 
MAX(IF(month= '7', sell, NULL)) AS Lug, 
MAX(IF(month= '8', sell, NULL)) AS Ago, 
MAX(IF(month= '9', sell, NULL)) AS Sett, 
MAX(IF(month= '10', sell, NULL)) AS Ott, 
MAX(IF(month= '11', sell, NULL)) AS Nov, 
MAX(IF(month= '12', sell, NULL)) AS Dic,

FROM `pdl_dati` group by fonte

如何插入“总计”为月='1' 和月='2' 和月='3' 和月='4' 和月='5' 和月='6' 和月='7'和月='8' 和月='9' 和月='10' 和月='11' 和月='12'?

谢谢

4

1 回答 1

0

如果您想要最后一行有每个月的总数,您可以使用GROUP BY WITH ROLLUP

SELECT `fonte`, 
  MAX(IF(month= '1', sell, NULL)) AS Gen,   -- do you want this to be sum?
  MAX(IF(month= '2', sell, NULL)) AS Feb, 
  MAX(IF(month= '3', sell, NULL)) AS Mar, 
  MAX(IF(month= '4', sell, NULL)) AS Apr, 
  MAX(IF(month= '5', sell, NULL)) AS Mag, 
  MAX(IF(month= '6', sell, NULL)) AS Giu, 
  MAX(IF(month= '7', sell, NULL)) AS Lug, 
  MAX(IF(month= '8', sell, NULL)) AS Ago, 
  MAX(IF(month= '9', sell, NULL)) AS Sett, 
  MAX(IF(month= '10', sell, NULL)) AS Ott, 
  MAX(IF(month= '11', sell, NULL)) AS Nov, 
  MAX(IF(month= '12', sell, NULL)) AS Dic
FROM `pdl_dati` 
group by fonte with ROLLUP;

作为旁注,如果您尝试按月获取fonte每个月的总数,我认为您会想要使用sum而不是max. 看演示

于 2013-04-26T11:22:54.150 回答