Sqlite 如何在组_concat中使用sum和join
我有两张桌子,顾客分类和物品。这两个表都有一个名为VatPercentage和VatPrice的列 场景: 如果两个表中存在相同的vatpercentage,我想对两个表中的vatprice求和,并将其添加到字符串中。 如果vatpercentage只存在于CustomerCategories表中,我想从这里求vatprice之和,并将其添加到字符串中。 如果vatpercentage只存在于Articles表中,我想从这里计算vatprice的总和,并将其添加到字符串中。 示例表: 文章: 客户类别: 在本例中,我希望从查询返回的结果是:Sqlite 如何在组_concat中使用sum和join,sqlite,Sqlite,我有两张桌子,顾客分类和物品。这两个表都有一个名为VatPercentage和VatPrice的列 场景: 如果两个表中存在相同的vatpercentage,我想对两个表中的vatprice求和,并将其添加到字符串中。 如果vatpercentage只存在于CustomerCategories表中,我想从这里求vatprice之和,并将其添加到字符串中。 如果vatpercentage只存在于Articles表中,我想从这里计算vatprice的总和,并将其添加到字符串中。 示例表: 文章: 客
{esc}{40h}25 %{esc}{41h}82.5 NOK{lf}{esc}{40h}8 %{esc}{41h}22.6 NOK{lf}{esc}{40h}13 %{esc}{41h}13 NOK{lf}
在没有任何积极结果的情况下,我尝试的代码是:
SELECT GROUP_CONCAT(Result, '|') Results
FROM (
select case when cc.VatPercentage = a.VatPercentage
then
SELECT '{esc}{40}' || CAST(cc.VatPercentage AS INTEGER) || '% ' ||
(SUM(cc.VatPrice) + SUM(a.VatPrice)) || ' NOK' || '{lf}' Result end
else
(
case when cc.VatPercentage <> a.VatPercentage
then
SELECT '{esc}{40}' || CAST(cc.VatPercentage AS INTEGER) || '% ' ||
(SUM(cc.VatPrice) + SUM(a.VatPrice)) || ' NOK' || '{lf}' ||
SELECT '{esc}{40}' || CAST(a.VatPercentage AS INTEGER) || '% ' ||
(SUM(a.VatPrice)) || ' NOK' || '{lf}' Result
end
)
FROM CustomerCategories cc
LEFT JOIN Articles a
on cc.VatPercentage = a.VatPercentage
WHERE
cc.VatPercentage != '0'
AND a.VatPercentage != '0'
AND cc.TotalPrice != '0'
AND a.TotalPrice != '0'
GROUP BY
cc.VatPercentage OR a.VatPercentage) x
我们将不胜感激
首先,两个表:
SELECT VatPercentage, VatPrice FROM CustomerCategories
UNION ALL
SELECT VatPercentage, VatPrice FROM Articles
然后转义字符是否会将结果弄乱:
SELECT GROUP_CONCAT('{esc}{40h}' || VatPercentage || ' %' ||
'{esc}{41h}' || VatPrice || ' NOK{lf}',
'')
FROM (SELECT VatPercentage,
SUM(VatPrice) AS PriceSum
FROM (SELECT VatPercentage, VatPrice FROM CustomerCategories
UNION ALL
SELECT VatPercentage, VatPrice FROM Articles)
WHERE VatPercentage != '0'
GROUP BY VatPercentage)
非常感谢您:您是否可以通过以下方式按升序排列?我尝试在Group by之后添加order by,但它没有排序。字符串按字典顺序比较,不像数字。
SELECT VatPercentage, VatPrice FROM CustomerCategories
UNION ALL
SELECT VatPercentage, VatPrice FROM Articles
VatPercentage VatPrice
8.0000000000 13
13.0000000000 13
0 0
25.0000000000 37.5
25.0000000000 25
25.0000000000 20
8.0000000000 4
8.0000000000 5.6
0 0
0 0
SELECT VatPercentage,
SUM(VatPrice) AS PriceSum
FROM (SELECT VatPercentage, VatPrice FROM CustomerCategories
UNION ALL
SELECT VatPercentage, VatPrice FROM Articles)
WHERE VatPercentage != '0'
GROUP BY VatPercentage
SELECT GROUP_CONCAT('{esc}{40h}' || VatPercentage || ' %' ||
'{esc}{41h}' || VatPrice || ' NOK{lf}',
'')
FROM (SELECT VatPercentage,
SUM(VatPrice) AS PriceSum
FROM (SELECT VatPercentage, VatPrice FROM CustomerCategories
UNION ALL
SELECT VatPercentage, VatPrice FROM Articles)
WHERE VatPercentage != '0'
GROUP BY VatPercentage)