-2
A
回答
1
那麼解決方案取決於數據 - 你只有6個「倉庫」值?在SQLFiddle
CREATE TABLE my_table (
godown VARCHAR2(20),
product_name VARCHAR2(20),
quantity NUMBER
);
INSERT INTO my_table VALUES ('Godown1', 'Rice', 5);
INSERT INTO my_table VALUES ('Godown1', 'Tea', 6);
INSERT INTO my_table VALUES ('Godown1', 'Milk', 4);
INSERT INTO my_table VALUES ('Godown2', 'Rice', 6);
INSERT INTO my_table VALUES ('Godown2', 'Tea', 7);
INSERT INTO my_table VALUES ('Godown2', 'Milk', 5);
INSERT INTO my_table VALUES ('Godown3', 'Rice', 8);
INSERT INTO my_table VALUES ('Godown3', 'Tea', 3);
INSERT INTO my_table VALUES ('Godown3', 'Milk', 5);
INSERT INTO my_table VALUES ('Godown4', 'Rice', 3);
INSERT INTO my_table VALUES ('Godown4', 'Tea', 2);
INSERT INTO my_table VALUES ('Godown4', 'Milk', 1);
INSERT INTO my_table VALUES ('Godown5', 'Rice', 4);
INSERT INTO my_table VALUES ('Godown5', 'Tea', 6);
INSERT INTO my_table VALUES ('Godown5', 'Milk', 7);
INSERT INTO my_table VALUES ('Godown6', 'Rice', 9);
INSERT INTO my_table VALUES ('Godown6', 'Tea', 24);
INSERT INTO my_table VALUES ('Godown6', 'Milk', 12);
COMMIT;
SELECT
product_name,
godown1_sum_quantity AS Godown1,
godown2_sum_quantity AS Godown2,
godown3_sum_quantity AS Godown3,
godown4_sum_quantity AS Godown4,
godown5_sum_quantity AS Godown5,
godown6_sum_quantity AS Godown6,
(SELECT SUM(quantity) FROM my_table WHERE product_name = data.product_name) AS Total
FROM (
SELECT
product_name,
godown,
quantity
FROM my_table
)
PIVOT (SUM(quantity) AS sum_quantity
FOR (godown) IN (
'Godown1' AS Godown1,
'Godown2' AS Godown2,
'Godown3' AS Godown3,
'Godown4' AS Godown4,
'Godown5' AS Godown5,
'Godown6' AS Godown6
)
) data
;
檢查:如果是這樣,你可以試試下面的解決方案http://sqlfiddle.com/#!4/5f8e8/1
0
可以使用CASE語句和聚合函數。
SELECT product_name,
MIN (CASE WHEN godown = 'godown1' THEN quantity END) godown1,
MIN (CASE WHEN godown = 'godown2' THEN quantity END) godown2,
MIN (CASE WHEN godown = 'godown3' THEN quantity END) godown3,
MIN (CASE WHEN godown = 'godown4' THEN quantity END) godown4,
MIN (CASE WHEN godown = 'godown5' THEN quantity END) godown5,
MIN (CASE WHEN godown = 'godown6' THEN quantity END) godown6,
SUM (quantity)
FROM products
GROUP BY product_name;
你也可以在11g中使用PIVOT運算符,但它不會給你最後的總列。你必須分開計算它。
SELECT *
FROM (SELECT godown, product_name, quantity FROM products)
PIVOT (MIN (
quantity)
FOR (godown)
IN ('godown1' AS godown1,
'godown2' AS godown2,
'godown3' AS godown3,
'godown4' AS godown4,
'godown5' AS godown5,
'godown6' AS godown6));
+0
倉庫不解決也許它的2或100可能依賴於用戶 – user1153193
相關問題
- 1. 甲骨文複雜的SQL SELECT查詢
- 2. 甲骨文 - 複雜更新
- 3. 甲骨文 - 幫助作出查詢
- 4. 甲骨文查詢
- 5. 甲骨文複雜的更新語句
- 6. 甲骨文子查詢
- 7. 甲骨文LISTAGG子查詢
- 8. 更新查詢甲骨文
- 9. 查詢和甲骨文12C
- 10. 甲骨文連接查詢
- 11. 甲骨文查詢優化
- 12. 甲骨文選擇查詢
- 13. 甲骨文查詢組
- 14. 甲骨文內部查詢
- 15. 查詢甲骨文PLSQL
- 16. 甲骨文插入查詢
- 17. SQL DML甲骨文查詢
- 18. 甲骨文分析查詢
- 19. 甲骨文連接查詢
- 20. 甲骨文雙查詢
- 21. 層次查詢 - 甲骨文
- 22. 甲骨文選擇查詢的幫助,請
- 23. 甲骨文選擇幫助
- 24. 甲骨文的SQL查詢與GROUP BY
- 25. 格式化甲骨文plsql的查詢
- 26. 甲骨文聚合的查詢
- 27. SQLite複雜查詢幫助
- 28. 複雜SQL查詢幫助
- 29. 查找甲骨文
- 30. 甲骨文 - 在選擇查詢
沒有貨倉不能解決倉庫是用戶定義也許只有2貨倉或也許100貨倉它取決於用戶 – user1153193
@ user1153193那我想你必須做,我這裏提出的方式:在數據透視] (http://stackoverflow.com/questions/19298424/oracle-pivot-with-subquery/19302540#19302540) –
但我使用oracle 10 g xe – user1153193