SELECT `stationcode`,
(Select sum(pis.NOPKGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE R') AS PKG_R, (Select sum(pis.WTQTLS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE R') AS WT_R,
(Select sum(pis.ERNGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE R') AS ERNG_R,
(Select sum(pis.NOPKGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE P') AS PKG_P, (Select sum(pis.WTQTLS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE P') AS WT_P,
(Select sum(pis.ERNGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE P') AS ERNG_P,
(Select sum(pis.NOPKGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE S') AS PKG_S, (Select sum(pis.WTQTLS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE S') AS WT_S,
(Select sum(pis.ERNGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE S') AS ERNG_S,
(Select sum(pis.NOPKGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE L') AS PKG_L, (Select sum(pis.WTQTLS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE L') AS WT_L,
(Select sum(pis.ERNGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE L') AS ERNG_L
,SUM(PKG_R+PKG_P+PKG_S+PKG_L) AS TOTAL_PKGS
FROM `station` WHERE Parcels = 'TRUE'
上面的查询显示了这个错误:
Unknown column 'PKG_R' in 'field list'
最佳答案
您不能在单个查询中直接使用别名。您只能在外部使用这些别名,如下所示。
SELECT stationcode,SUM(PKG_R+PKG_P+PKG_S+PKG_L) AS TOTAL_PKGS FROM (SELECT stationcode,
(Select sum(pis.NOPKGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE R') AS PKG_R,
(Select sum(pis.WTQTLS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE R') AS WT_R,
(Select sum(pis.ERNGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE R') AS ERNG_R,
(Select sum(pis.NOPKGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE P') AS PKG_P,
(Select sum(pis.WTQTLS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE P') AS WT_P,
(Select sum(pis.ERNGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE P') AS ERNG_P,
(Select sum(pis.NOPKGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE S') AS PKG_S,
(Select sum(pis.WTQTLS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE S') AS WT_S,
(Select sum(pis.ERNGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE S') AS ERNG_S,
(Select sum(pis.NOPKGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE L') AS PKG_L,
(Select sum(pis.WTQTLS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE L') AS WT_L,
(Select sum(pis.ERNGS) from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE L') AS ERNG_L
FROM station WHERE Parcels = 'TRUE' ) A
GROUP BY stationcode
关于mysql - 子查询值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53513297/