сумма значений подзапросов - PullRequest
0 голосов
/ 28 ноября 2018
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' в 'списке полей'

Ответы [ 3 ]

0 голосов
/ 28 ноября 2018

если 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'
0 голосов
/ 28 ноября 2018

Вы можете упростить этот запрос, используя условное агрегирование:

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;
0 голосов
/ 28 ноября 2018

Вы не можете использовать псевдоним непосредственно в одном запросе.вы можете использовать эти псевдонимы снаружи, как показано ниже.

   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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...