0

我正在尝试进行查询,将前 10 个系统和“其他”显示为第 11 行。我做了联合查询。我发现我构建的查询为不在 Top10 中的每个系统名称返回了“其他”记录,因此我在 select 中选择了所有“其他”的总和。这导致我出现错误代码 1349。有人可以帮我重新排列查询吗?

create view QueryView as (Select 
System, Cast(Sum(Duration) as unsigned) AS 'Sum'
From
tbl_Events
    INNER JOIN
(trans_GU, tbl_G, trans_GM) ON (trans_GU.Unit_ID = tbl_Events.Unit_ID
    AND trans_GU.GIB_SN = tbl_G.SN
    AND tbl_G.EQUIPCODE = trans_GM.EQUIPCODE
    AND tbl_G.GEN_MODEL = trans_GM.GEN_MODEL)
WHERE
trans_GM.ProductLine = 'fleet'
GROUP BY System
Order By Sum desc
Limit 10) 
Union 
(Select 'Other' As System, Cast(Sum(a.`Sum`) As Unsigned) As 'Sum' FROM (Select 
'Other' As System,
Sum(Duration) AS 'Sum'
FROM    tbl_Events
    INNER JOIN
(trans_GU, tbl_G, trans_GM) ON (trans_GU.Unit_ID = tbl_Events.Unit_ID
    AND trans_GU.GIB_SN = tbl_G.SN
    AND tbl_G.EQUIPCODE = trans_GM.EQUIPCODE
    AND tbl_G.GEN_MODEL = trans_GM.GEN_MODEL)
WHERE
trans_GM.ProductLine = 'fleet'
GROUP BY System
Order By Sum desc
Limit 10,99999999) As a);
4

1 回答 1

0

正确的。MySQL 不支持from子句中的子查询。您可以在没有子查询的情况下重写查询的第二部分:

(Select 'Other', cast(Sum(Duration) as unisgned) AS "Sum"
 FROM  tbl_Events INNER JOIN
      (trans_GU, tbl_G, trans_GM)
      ON (trans_GU.Unit_ID = tbl_Events.Unit_ID
          AND trans_GU.GIB_SN = tbl_G.SN
          AND tbl_G.EQUIPCODE = trans_GM.EQUIPCODE
          AND tbl_G.GEN_MODEL = trans_GM.GEN_MODEL)
 WHERE trans_GM.ProductLine = 'fleet'
 From tbl_RAM
 GROUP BY System
 Order By Sum desc
 Limit 10,99999999
) 
于 2013-08-26T13:32:48.460 回答