我有几个表要选择。我还想在其中一张表上使用sum()。这是我到目前为止所得到的。
DROP TABLE ... IF EXISTS
CREATE TABLE ... IF EXISTS AS
SELECT
fbp.*
bcs.yyyy_mm_dd,
cspd.*,
FROM xxx.fbp fbp
INNER JOIN table2 st ON (fbp.id = st.xml_id)
INNER JOIN (
SELECT
CASE
WHEN (field1) IS NULL THEN 0
ELSE SUM(field1)
END AS field1
FROM table2 bcs
WHERE yyyy_mm_dd >= '2019-04-21'
GROUP BY bcs.column_id
) AS bcs ON bcs.column_id = fbp.id
INNER JOIN table3 cspd ON (fbp.id = cspd.id)
WHERE bcs.yyyy_mm_dd >= '2019-04-21'
AND cspd.yyyy_mm_dd >= '2019-04-21'
AND (st.id IN (9)) AND (st.date IS NULL);
运行此查询时,出现以下错误。
表达式不在GROUP BY键“ field1”中。
将field1
添加到GROUP BY时,出现另一个错误。
我正在寻找我的数据集是由hotel_id然后是我选择的其他指标分组的sum(field1)。那应该给我每天的总和以及其他一些元数据。如何做到这一点?
答案 0 :(得分:0)
您可以尝试以下吗?
DROP TABLE ... IF EXISTS
CREATE TABLE ... IF EXISTS AS
SELECT
fbp.*
bcs.yyyy_mm_dd,
cspd.*,
FROM xxx.fbp fbp
INNER JOIN table2 st ON (fbp.id = st.xml_id)
INNER JOIN (
SELECT
column_id
sum(CASE
WHEN field1 IS NULL THEN 0
ELSE field1
END) AS field1
FROM table2 bcs
WHERE yyyy_mm_dd >= '2019-04-21'
GROUP BY bcs.column_id
) AS bcs ON bcs.column_id = fbp.id
INNER JOIN table3 cspd ON (fbp.id = cspd.id)
WHERE bcs.yyyy_mm_dd >= '2019-04-21'
AND cspd.yyyy_mm_dd >= '2019-04-21'
AND (st.id IN (9)) AND (st.date IS NULL);