繁体   English   中英

子查询值之和

[英]sum of subqueries values

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' 

上面的查询显示此错误:

“字段列表”中的未知列“ PKG_R”

您不能在单个查询中直接使用别名。 您只能在外部使用这些别名,如下所示。

   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

如果stationcode仅返回一行,那么您可以尝试以下操作:

SELECT `stationcode`, PKG_R, WT_R, ERNG_R, PKG_P, WT_P, ERNG_P, PKG_S, WT_S, ERNG_S, PKG_L, WT_L, ERNG_L , SUM(PKG_R+PKG_P+PKG_S+PKG_L) AS TOTAL_PKGS
FROM
(
    (Select sum(pis.NOPKGS) AS PKG_R from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE R') a,
    (Select sum(pis.WTQTLS)  AS WT_R from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE R') b,
    (Select sum(pis.ERNGS)  AS ERNG_R from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE R') c,
    (Select sum(pis.NOPKGS)  AS PKG_P from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE P') d, 
    (Select sum(pis.WTQTLS) AS WT_P from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE P') e,
    (Select sum(pis.ERNGS) AS ERNG_P from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE P') f,
    (Select sum(pis.NOPKGS) AS PKG_S from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE S') g, 
    (Select sum(pis.WTQTLS) AS WT_S from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE S') h,
    (Select sum(pis.ERNGS) AS ERNG_S from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE S') i,
    (Select sum(pis.NOPKGS) AS PKG_L from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE L') j, 
    (Select sum(pis.WTQTLS) AS WT_L from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE L') k,
    (Select sum(pis.ERNGS) AS ERNG_L from pis where pis.prcldate = '2018-11-28' and pis.parcelstn = station.stationcode and pis.BKDSCALE = 'SCALE L') l,
    `station`
)
WHERE Parcels = 'TRUE'

您可以使用条件聚合来简化此查询:

SELECT stationcode, 
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE R' THEN pis.NOPKGS ELSE 0 END) AS PKG_R,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE R' THEN pis.WTQTLS ELSE 0 END) AS WT_R,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE R' THEN pis.ERNGS ELSE 0 END) AS ERNG_R,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE P' THEN pis.NOPKGS ELSE 0 END) AS PKG_P,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE P' THEN pis.WTQTLS ELSE 0 END) AS WT_P,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE P' THEN pis.ERNGS ELSE 0 END) AS ERNG_P,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE S' THEN pis.NOPKGS ELSE 0 END) AS PKG_S,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE S' THEN pis.WTQTLS ELSE 0 END) AS WT_S,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE S' THEN pis.ERNGS ELSE 0 END) AS ERNG_S,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE L' THEN pis.NOPKGS ELSE 0 END) AS PKG_L,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE L' THEN pis.WTQTLS ELSE 0 END) AS WT_L,
       SUM(CASE WHEN pis.BKDSCALE = 'SCALE L' THEN pis.ERNGS ELSE 0 END) AS ERNG_L,
       SUM(pis.NOPKGS) AS total_packages
FROM sstation s LEFT JOIN
     pis
     ON pis.parcelstn = s.stationcode AND
        pis.prcldate = '2018-11-28' AND
        pis.DKDSCALE IN ('SCALE R', 'SCALE S', 'SCALE P', 'SCALE L')
WHERE Parcels = 'TRUE' 
GROUP BY stationcode;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM