普遍的 PSQL GROUP BY 子句
Pervasive PSQL GROUP BY Clause
我在 Pervasive V11 数据库上进行了以下 SQL 调用。 我正在努力使每个项目 ID 只得到一行。
SELECT
inventory_history.item_id AS 'ItemId',
RTrim(inventory_items.item_name) AS 'Description',
If(RTrim(trx_type) = 'P',SUM(change_quantity),'') AS 'QtyChangeP',
If(RTrim(trx_type) = 'P',SUM(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),'') AS 'CostChangeP',
If(RTrim(trx_type) = 'S',SUM(change_quantity),'') AS 'QtyChangeS',
If(RTrim(trx_type) = 'S',SUM(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),'') AS 'CostChangeS',
If(RTrim(trx_type) = 'A',SUM(change_quantity),'') AS 'QtyChangeA',
If(RTrim(trx_type) = 'A',SUM(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),'') AS 'CostChangeA',
If(RTrim(trx_type) = 'T',SUM(change_quantity),'') AS 'QtyChangeT',
If(RTrim(trx_type) = 'T',SUM(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),'') AS 'CostChangeT',
If(RTrim(inventory_categories.stocked_by_factor) = 'FT',inventory_items.unit_weight,'') AS 'WtPerFt'
FROM
inventory_history
INNER JOIN
inventory_items ON inventory_history.item_id = inventory_items.item_id
INNER JOIN
inventory_categories ON inventory_items.category_id = inventory_categories.category_id
WHERE
trx_date>'20170731'
GROUP BY
inventory_history.item_id, inventory_items.item_name, trx_type, stocked_by_factor, unit_weight;
就像现在一样,如果我没有在 Group By 子句中列出 trx_type、stocked_by_factor 或 unit_weight,我会从数据库收到错误消息。
删除这些字段会导致此错误:
Column invalid. Must be a group by column: trx_type in SELECT LIST.
以下是完整 SQL 调用的示例结果:
ItemId Description QtyChangeP CostChangeP QtyChangeS CostChangeS QtyChangeA CostChangeA QtyChangeT CostChangeT WtPerFt
========================= ================================================== ============================================================================ ============================================================================ ============================================================================ ============================================================================ ============================================================================ ============================================================================ ============================================================================ ============================================================================ ==============
1100SM19050T2C1 1100-H19 LAMI SHIM .050" TYPE 2, CLASS 1 58.2400 890.00 0.0000 0.00 0.0000 0.00 0.0000 0.00 0.000000
1100SM19050T2C1 1100-H19 LAMI SHIM .050" TYPE 2, CLASS 1 0.0000 0.00 58.2400 890.00 0.0000 0.00 0.0000 0.00 0.000000
1100SM19085T2C1 1100-H19 LAMI SHIM .085" TYPE 2, CLASS 1 0.0000 0.00 0.0000 0.00 0.0000 0.00 0.0000 0.00 0.000000
1100SM19085T2C1 1100-H19 LAMI SHIM .085" TYPE 2, CLASS 1 19.8080 208.00 0.0000 0.00 0.0000 0.00 0.0000 0.00 0.000000
1100SM19085T2C1 1100-H19 LAMI SHIM .085" TYPE 2, CLASS 1 0.0000 0.00 19.8080 208.00 0.0000 0.00 0.0000 0.00 0.000000
非常感谢任何帮助。谢谢!!
条件应该是 sum()
:
的参数
sum(If(RTrim(trx_type) = 'P', change_quantity), 0) AS QtyChangeP,
通常,这将使用 case
:
来编写
sum(case when RTrim(trx_type) = 'P' then change_quantity else 0 end) AS QtyChangeP,
没关系...经过几个小时的绞尽脑汁,答案就在我面前。我只需要将 If() 函数转换为 SUM() 函数。
这是工作电话:
SELECT
inventory_history.item_id AS 'ItemId',
RTrim(inventory_items.item_name) AS 'Description',
SUM(If(RTrim(trx_type) = 'P',inventory_history.change_quantity,0)) AS 'QtyChangeP',
SUM(If(RTrim(trx_type) = 'P',(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),0)) AS 'CostChangeP',
SUM(If(RTrim(trx_type) = 'S',inventory_history.change_quantity,0)) AS 'QtyChangeS',
SUM(If(RTrim(trx_type) = 'S',(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),0)) AS 'CostChangeS',
SUM(If(RTrim(trx_type) = 'A',inventory_history.change_quantity,0)) AS 'QtyChangeA',
SUM(If(RTrim(trx_type) = 'A',(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),0)) AS 'CostChangeA',
SUM(If(RTrim(trx_type) = 'T',inventory_history.change_quantity,0)) AS 'QtyChangeT',
SUM(If(RTrim(trx_type) = 'T',(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),0)) AS 'CostChangeT',
If(RTrim(inventory_categories.stocked_by_factor) = 'FT',inventory_items.unit_weight,'') AS 'WtPerFt'
FROM
inventory_history
INNER JOIN
inventory_items ON inventory_history.item_id = inventory_items.item_id
INNER JOIN
inventory_categories ON inventory_items.category_id = inventory_categories.category_id
WHERE
trx_date>'20170731'
GROUP BY
inventory_history.item_id, inventory_items.item_name, stocked_by_factor, unit_weight;
我在 Pervasive V11 数据库上进行了以下 SQL 调用。 我正在努力使每个项目 ID 只得到一行。
SELECT
inventory_history.item_id AS 'ItemId',
RTrim(inventory_items.item_name) AS 'Description',
If(RTrim(trx_type) = 'P',SUM(change_quantity),'') AS 'QtyChangeP',
If(RTrim(trx_type) = 'P',SUM(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),'') AS 'CostChangeP',
If(RTrim(trx_type) = 'S',SUM(change_quantity),'') AS 'QtyChangeS',
If(RTrim(trx_type) = 'S',SUM(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),'') AS 'CostChangeS',
If(RTrim(trx_type) = 'A',SUM(change_quantity),'') AS 'QtyChangeA',
If(RTrim(trx_type) = 'A',SUM(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),'') AS 'CostChangeA',
If(RTrim(trx_type) = 'T',SUM(change_quantity),'') AS 'QtyChangeT',
If(RTrim(trx_type) = 'T',SUM(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),'') AS 'CostChangeT',
If(RTrim(inventory_categories.stocked_by_factor) = 'FT',inventory_items.unit_weight,'') AS 'WtPerFt'
FROM
inventory_history
INNER JOIN
inventory_items ON inventory_history.item_id = inventory_items.item_id
INNER JOIN
inventory_categories ON inventory_items.category_id = inventory_categories.category_id
WHERE
trx_date>'20170731'
GROUP BY
inventory_history.item_id, inventory_items.item_name, trx_type, stocked_by_factor, unit_weight;
就像现在一样,如果我没有在 Group By 子句中列出 trx_type、stocked_by_factor 或 unit_weight,我会从数据库收到错误消息。
删除这些字段会导致此错误:
Column invalid. Must be a group by column: trx_type in SELECT LIST.
以下是完整 SQL 调用的示例结果:
ItemId Description QtyChangeP CostChangeP QtyChangeS CostChangeS QtyChangeA CostChangeA QtyChangeT CostChangeT WtPerFt
========================= ================================================== ============================================================================ ============================================================================ ============================================================================ ============================================================================ ============================================================================ ============================================================================ ============================================================================ ============================================================================ ==============
1100SM19050T2C1 1100-H19 LAMI SHIM .050" TYPE 2, CLASS 1 58.2400 890.00 0.0000 0.00 0.0000 0.00 0.0000 0.00 0.000000
1100SM19050T2C1 1100-H19 LAMI SHIM .050" TYPE 2, CLASS 1 0.0000 0.00 58.2400 890.00 0.0000 0.00 0.0000 0.00 0.000000
1100SM19085T2C1 1100-H19 LAMI SHIM .085" TYPE 2, CLASS 1 0.0000 0.00 0.0000 0.00 0.0000 0.00 0.0000 0.00 0.000000
1100SM19085T2C1 1100-H19 LAMI SHIM .085" TYPE 2, CLASS 1 19.8080 208.00 0.0000 0.00 0.0000 0.00 0.0000 0.00 0.000000
1100SM19085T2C1 1100-H19 LAMI SHIM .085" TYPE 2, CLASS 1 0.0000 0.00 19.8080 208.00 0.0000 0.00 0.0000 0.00 0.000000
非常感谢任何帮助。谢谢!!
条件应该是 sum()
:
sum(If(RTrim(trx_type) = 'P', change_quantity), 0) AS QtyChangeP,
通常,这将使用 case
:
sum(case when RTrim(trx_type) = 'P' then change_quantity else 0 end) AS QtyChangeP,
没关系...经过几个小时的绞尽脑汁,答案就在我面前。我只需要将 If() 函数转换为 SUM() 函数。
这是工作电话:
SELECT
inventory_history.item_id AS 'ItemId',
RTrim(inventory_items.item_name) AS 'Description',
SUM(If(RTrim(trx_type) = 'P',inventory_history.change_quantity,0)) AS 'QtyChangeP',
SUM(If(RTrim(trx_type) = 'P',(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),0)) AS 'CostChangeP',
SUM(If(RTrim(trx_type) = 'S',inventory_history.change_quantity,0)) AS 'QtyChangeS',
SUM(If(RTrim(trx_type) = 'S',(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),0)) AS 'CostChangeS',
SUM(If(RTrim(trx_type) = 'A',inventory_history.change_quantity,0)) AS 'QtyChangeA',
SUM(If(RTrim(trx_type) = 'A',(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),0)) AS 'CostChangeA',
SUM(If(RTrim(trx_type) = 'T',inventory_history.change_quantity,0)) AS 'QtyChangeT',
SUM(If(RTrim(trx_type) = 'T',(history_cost_01+history_cost_02+history_cost_03+history_cost_04+history_cost_05+history_cost_06+history_cost_07+history_cost_08+history_cost_09+history_cost_10+history_cost_11+history_cost_12),0)) AS 'CostChangeT',
If(RTrim(inventory_categories.stocked_by_factor) = 'FT',inventory_items.unit_weight,'') AS 'WtPerFt'
FROM
inventory_history
INNER JOIN
inventory_items ON inventory_history.item_id = inventory_items.item_id
INNER JOIN
inventory_categories ON inventory_items.category_id = inventory_categories.category_id
WHERE
trx_date>'20170731'
GROUP BY
inventory_history.item_id, inventory_items.item_name, stocked_by_factor, unit_weight;