Select Group By 无法正常工作
Select with Group By not working properly
我正在开发一个需要嵌入式数据库的应用程序,所以我使用 MYSQL 设计了它,并创建了一个可以在其中运行的 SQL 语句。但是,当移动到嵌入式数据库(即 Hyper-SQL (HSQLDB))时,我的查询不再有效。这似乎与我在 select 上必须与分组依据匹配的参数数量有关,但如果我去添加它们,它将使数据保持未分组状态,这对我不起作用。
这是查询:
我有两个具有相同列的表(GFS 和 SUPP),不同之处在于行为,其中一个的填充列比另一个多。
SELECT gfs.GFSAccount, gfs.GLAccount,
CASE
WHEN gfs.ICPartner = "" THEN '[ICP_NONE]'
WHEN gfs.ICPartner <> "" THEN gfs.ICPartner
END AS ICPartner,
CASE
WHEN gfs.PPESupplementalData <> '' THEN gfs.PPESupplementalData
WHEN gfs.PPESupplementalData = '' THEN '[NONE]'
END AS SupplementalData,
CASE
WHEN gfs.AccountType = 'SALES AND COS' THEN gfs.PCode
WHEN gfs.AccountType = 'COO' THEN 'COO'
WHEN gfs.AccountType = 'OTHERS' THEN gfs.Business
END AS Business,
Round(Sum(gfs.Amount), 2) AS Amount
FROM gfs, supp
WHERE gfs.GLAccount <> supp.GLAccount
GROUP BY gfs.GFSAccount,
CASE WHEN gfs.ICPartnerSplit = 1 THEN gfs.ICPartner END
UNION
SELECT GFSAccount, GLAccount,
CASE
WHEN ICPartner = "" THEN '[ICP_NONE]'
WHEN ICPartner <> "" THEN ICPartner
END AS ICPartner,
CASE
WHEN SuppData <> '' THEN SuppData
WHEN SuppData = '' THEN '[NONE]'
END AS SupplementalData,
Business, Round(Sum(Amount), 2) AS Amount
FROM supp
ORDER BY GFSAccount;
有很多针对空白和固定字段的转换,但主要问题是 3 组依据(每个 select 中有 2 个,最后有一个),它不允许我来做。
(我知道查询本身并不是最好的创建或优化的,但我已经离开数据库有一段时间了,我有点生疏了)
你们有什么建议可以让我在 HSQLDB(或另一个不是 MySQL 的数据库)中工作吗??
似乎 您想要 select 来自 gfs
table 的数据,但前提是 GLAccount
不是出现在 supp
table 中。你需要 LEFT JOIN
才能做到这一点。否则,您需要 GROUP BY
所有未在 sum()
函数中使用的列。您还可以简化 CASE
语句。对于 PostgreSQL,查询将如下所示:
SELECT gfs.GFSAccount, gfs.GLAccount,
CASE gfs.ICPartner
WHEN '' THEN '[ICP_NONE]' ELSE gfs.ICPartner
END AS ICPartner,
CASE gfs.PPESupplementalData
WHEN '' THEN '[NONE]' ELSE gfs.PPESupplementalData
END AS SupplementalData,
CASE gfs.AccountType
WHEN 'SALES AND COS' THEN gfs.PCode
WHEN 'COO' THEN 'COO'
WHEN 'OTHERS' THEN gfs.Business
END AS Business,
round(sum(gfs.Amount), 2) AS Amount
FROM gfs
LEFT JOIN supp USING (GLAccount)
WHERE supp.GLAccount IS NULL
GROUP BY 1, 2, 3, 4, 5, 6
UNION
SELECT GFSAccount, GLAccount,
CASE ICPartner
WHEN '' THEN '[ICP_NONE]' ELSE ICPartner
END AS ICPartner,
CASE SuppData
WHEN '' THEN '[NONE]' ELSE SuppData
END AS SupplementalData,
Business, round(sum(Amount), 2) AS Amount
FROM supp
GROUP BY 1, 2, 3, 4, 5, 6
ORDER BY GFSAccount;
如果各个列是 NULL
而不是空字符串 ''
您应该将 CASE
语句更改为 coalesce(gfs.ICPartner, '[ICP_NONE]') AS ICPartner
等
我正在开发一个需要嵌入式数据库的应用程序,所以我使用 MYSQL 设计了它,并创建了一个可以在其中运行的 SQL 语句。但是,当移动到嵌入式数据库(即 Hyper-SQL (HSQLDB))时,我的查询不再有效。这似乎与我在 select 上必须与分组依据匹配的参数数量有关,但如果我去添加它们,它将使数据保持未分组状态,这对我不起作用。
这是查询:
我有两个具有相同列的表(GFS 和 SUPP),不同之处在于行为,其中一个的填充列比另一个多。
SELECT gfs.GFSAccount, gfs.GLAccount,
CASE
WHEN gfs.ICPartner = "" THEN '[ICP_NONE]'
WHEN gfs.ICPartner <> "" THEN gfs.ICPartner
END AS ICPartner,
CASE
WHEN gfs.PPESupplementalData <> '' THEN gfs.PPESupplementalData
WHEN gfs.PPESupplementalData = '' THEN '[NONE]'
END AS SupplementalData,
CASE
WHEN gfs.AccountType = 'SALES AND COS' THEN gfs.PCode
WHEN gfs.AccountType = 'COO' THEN 'COO'
WHEN gfs.AccountType = 'OTHERS' THEN gfs.Business
END AS Business,
Round(Sum(gfs.Amount), 2) AS Amount
FROM gfs, supp
WHERE gfs.GLAccount <> supp.GLAccount
GROUP BY gfs.GFSAccount,
CASE WHEN gfs.ICPartnerSplit = 1 THEN gfs.ICPartner END
UNION
SELECT GFSAccount, GLAccount,
CASE
WHEN ICPartner = "" THEN '[ICP_NONE]'
WHEN ICPartner <> "" THEN ICPartner
END AS ICPartner,
CASE
WHEN SuppData <> '' THEN SuppData
WHEN SuppData = '' THEN '[NONE]'
END AS SupplementalData,
Business, Round(Sum(Amount), 2) AS Amount
FROM supp
ORDER BY GFSAccount;
有很多针对空白和固定字段的转换,但主要问题是 3 组依据(每个 select 中有 2 个,最后有一个),它不允许我来做。
(我知道查询本身并不是最好的创建或优化的,但我已经离开数据库有一段时间了,我有点生疏了)
你们有什么建议可以让我在 HSQLDB(或另一个不是 MySQL 的数据库)中工作吗??
似乎 您想要 select 来自 gfs
table 的数据,但前提是 GLAccount
不是出现在 supp
table 中。你需要 LEFT JOIN
才能做到这一点。否则,您需要 GROUP BY
所有未在 sum()
函数中使用的列。您还可以简化 CASE
语句。对于 PostgreSQL,查询将如下所示:
SELECT gfs.GFSAccount, gfs.GLAccount,
CASE gfs.ICPartner
WHEN '' THEN '[ICP_NONE]' ELSE gfs.ICPartner
END AS ICPartner,
CASE gfs.PPESupplementalData
WHEN '' THEN '[NONE]' ELSE gfs.PPESupplementalData
END AS SupplementalData,
CASE gfs.AccountType
WHEN 'SALES AND COS' THEN gfs.PCode
WHEN 'COO' THEN 'COO'
WHEN 'OTHERS' THEN gfs.Business
END AS Business,
round(sum(gfs.Amount), 2) AS Amount
FROM gfs
LEFT JOIN supp USING (GLAccount)
WHERE supp.GLAccount IS NULL
GROUP BY 1, 2, 3, 4, 5, 6
UNION
SELECT GFSAccount, GLAccount,
CASE ICPartner
WHEN '' THEN '[ICP_NONE]' ELSE ICPartner
END AS ICPartner,
CASE SuppData
WHEN '' THEN '[NONE]' ELSE SuppData
END AS SupplementalData,
Business, round(sum(Amount), 2) AS Amount
FROM supp
GROUP BY 1, 2, 3, 4, 5, 6
ORDER BY GFSAccount;
如果各个列是 NULL
而不是空字符串 ''
您应该将 CASE
语句更改为 coalesce(gfs.ICPartner, '[ICP_NONE]') AS ICPartner
等