mysql - 子查询值的总和

标签 mysql sql

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/

相关文章:

PHP - Ajax 发布可编辑 HTML 表中的值

mysql - 关于获取重复行总和的 SQL 问题

mysql - 如何使用mysql将一个表中的所有列集成到另一个表的一列中

返回语法错误的 PHP SQL 插入语句,不确定它可能是什么

mysql - 如何像tinder一样获得匹配,每个用户在表中都有自己的条目

MySQL 替换

Mysql:如何在 created_at 后 2 个月检索行

sql - 如何从sql server中包含html内容的字段中提取文件名?

php - Cakephp 查询缓存

mysql - 使用带有 MySQL 链接服务器的 openquery 时,宽 varchar 字段导致 "Requested conversion is not supported"错误