有人可以告诉我如何更改我的查询,以便它不会使用GROUP_CONCAT
。
该查询适用于MYSQL
和PHP,但我需要在Filemaker Pro
中运行相同的查询,但Filemaker不支持GROUP CONCAT。
SELECT sqlvalue
CAST(GROUP_CONCAT(IF(stat_number = 693, sqlvalue, NULL)) AS UNSIGNED) AS S693,
CAST(GROUP_CONCAT(IF(stat_number = 698, sqlvalue, NULL)) AS UNSIGNED) AS S698,
CAST(GROUP_CONCAT(IF(stat_number = 2010, sqlvalue, NULL)) AS UNSIGNED) AS S2010,
CAST(GROUP_CONCAT(IF(stat_number = 2020, sqlvalue, NULL)) AS UNSIGNED) AS S2020,
IFNULL(CAST(GROUP_CONCAT(IF(stat_number = 693, sqlvalue, NULL)) AS UNSIGNED),0) +
IFNULL(CAST(GROUP_CONCAT(IF(stat_number = 698, sqlvalue, NULL)) AS UNSIGNED),0) +
IFNULL(CAST(GROUP_CONCAT(IF(stat_number = 2010, sqlvalue, NULL)) AS UNSIGNED),0) +
IFNULL(CAST(GROUP_CONCAT(IF(stat_number = 2020, sqlvalue, NULL)) AS UNSIGNED),0) + AS STOTAL
FROM statsvalue
WHERE sqldate > ’01-07-2013′
ORDER BY sqldate ASC
GROUP By sqldate
字段stat_number
是为输入的记录提供的代码,sqlvalue
是数字值,sqldate
是日期。
上述查询会获取stat_number
的值并将它们相加,并将总数按sqldate field
分组。
答案 0 :(得分:2)
也许我错过了一些东西,但在这种特殊情况下你不需要GROUP_CONCAT()
(做条件聚合)。
因此恕我直言,您可以继续使用GROUP_CONCAT()
更改MAX()
,这可能更有意义,至少在阅读查询时更有意义。
我不知道Filemaker,但您也可以考虑更改
IF()
至CASE END
IFNULL()
至COALESCE()
除此之外,您的查询还有其他一些问题:
sqlvalue
子句中使用SELECT
列,而没有将聚合函数应用于它作为group by的一部分。它只是没有意义,其他主要的RDBMS(SQL Server,Oracle,Postgres)不允许它。 您最有可能打算使用sqldate
。 sqlvalue
放在SELECT
中,也应该在列名后面使用逗号+
AS STOTAL
ORDER BY
应该追踪GROUP BY
据说你的查询可能看起来像
SELECT sqldate,
CAST(MAX(CASE WHEN stat_number = 693 THEN sqlvalue END) AS UNSIGNED) AS S693,
CAST(MAX(CASE WHEN stat_number = 698 THEN sqlvalue END) AS UNSIGNED) AS S698,
CAST(MAX(CASE WHEN stat_number = 2010 THEN sqlvalue END) AS UNSIGNED) AS S2010,
CAST(MAX(CASE WHEN stat_number = 2020 THEN sqlvalue END) AS UNSIGNED) AS S2020,
COALESCE(MAX(CASE WHEN stat_number = 693 THEN sqlvalue END) AS UNSIGNED), 0) +
COALESCE(MAX(CASE WHEN stat_number = 698 THEN sqlvalue END) AS UNSIGNED), 0) +
COALESCE(MAX(CASE WHEN stat_number = 2010 THEN sqlvalue END) AS UNSIGNED), 0) +
COALESCE(MAX(CASE WHEN stat_number = 2020 THEN sqlvalue END) AS UNSIGNED), 0) AS STOTAL
FROM statsvalue
WHERE sqldate > '2013-01-07'
GROUP BY sqldate
ORDER BY sqldate
或使用像这样的内联视图
SELECT sqldate, S693, S698, S2010, S2020,
COALESCE(S693, 0) +
COALESCE(S698, 0) +
COALESCE(S2010, 0) +
COALESCE(S2020, 0) AS STOTAL
FROM
(
SELECT sqldate
CAST(MAX(CASE WHEN stat_number = 693 THEN sqlvalue END) AS UNSIGNED) AS S693,
CAST(MAX(CASE WHEN stat_number = 698 THEN sqlvalue END) AS UNSIGNED) AS S698,
CAST(MAX(CASE WHEN stat_number = 2010 THEN sqlvalue END) AS UNSIGNED) AS S2010,
CAST(MAX(CASE WHEN stat_number = 2020 THEN sqlvalue END) AS UNSIGNED) AS S2020
FROM statsvalue
WHERE sqldate > '2013-01-07'
GROUP BY sqldate
) q
ORDER BY sqldate