2017-03-30 198 views
0

我有這個疑問:顯示查詢結果不同

SELECT pr_products.product AS PRODUCT, pr_varieties.variety AS VARIETY, pr_grades.GRADE, SUM(pf_harvest.quantity) AS QUANTITY 
FROM pf_harvest 
INNER JOIN pf_performance ON pf_performance.id = pf_harvest.id_performance 
INNER JOIN pr_products ON pr_products.id = pf_harvest.id_product 
INNER JOIN pr_varieties ON pr_varieties.id = pf_harvest.id_variety 
INNER JOIN pr_grades ON pr_grades.id = pf_harvest.id_grade 
WHERE pf_performance.status = 100 
AND pf_harvest.id_tenant = 1 
AND pf_harvest.date = '2017-03-22' 
GROUP BY pf_harvest.id_product, pf_harvest.id_variety, pf_harvest.id_grade 
ORDER BY pf_harvest.id_product, pr_varieties.variety, pf_harvest.id_grade; 

這我顯示了以下結果:

------------------------------------------------------------------- 
    PRODUCT  |  VARIETY  |  GRADE  |  QUANTITY | 
------------------------------------------------------------------- 
    ROSE  |  ROSEV1  |  GRADE1  |  1000  | 
------------------------------------------------------------------- 
    ROSE  |  ROSEV1  |  GRADE2  |  5000  | 
------------------------------------------------------------------- 
    ROSE  |  ROSEV2  |  GRADE1  |  2000  | 
------------------------------------------------------------------- 
    ROSE1  |  ROSE1V1  |  GRADE1  |  3500  | 
------------------------------------------------------------------- 

是否可以按下列步驟顯示查詢結果?

------------------------------------------------------------------- 
    PRODUCT  | VARIETY | GRADE1 | GRADE2 | TOTAL   | 
------------------------------------------------------------------- 
    ROSE  | ROSEV1 | 1000 | 5000 | 6000   | 
------------------------------------------------------------------- 
    ROSE  | ROSEV2 | 2000 | 0 | 2000   | 
------------------------------------------------------------------- 
    ROSE1  | ROSE1V1 | 3500 | 0 | 3500   | 
------------------------------------------------------------------- 

我試圖改變查詢,但我不能,我想知道是否有可能,我希望有人能幫助我。

UPDATED 注意:查詢結果(GRADE1,GRADE2,GRADE3 ...)中可能會有更多的GRADES。

謝謝!

+0

在SQL中旋轉是所有RDBMS中最常見的問題之一,毫無疑問在數百個SO!查找*條件聚合*。 – Parfait

回答

1

通過不分組對GRADE列修改當前的查詢,而是轉動該列。然後,使用條件聚合來計算GRADE1GRADE2列。

SELECT t3.product AS PRODUCT, 
     t4.variety AS VARIETY, 
     SUM(CASE WHEN t5.GRADE = 'GRADE1' THEN t1.quantity ELSE 0 END) AS GRADE1, 
     SUM(CASE WHEN t5.GRADE = 'GRADE2' THEN t1.quantity ELSE 0 END) AS GRADE2, 
     SUM(CASE WHEN t5.GRADE = 'GRADE3' THEN t1.quantity ELSE 0 END) AS GRADE3, 
     -- hopefully it is clear how to add more grades 
     SUM(t1.quantity) AS TOTAL 
FROM pf_harvest t1 
INNER JOIN pf_performance t2 
    ON t2.id = t1.id_performance 
INNER JOIN pr_products t3 
    ON t3.id = pf_harvest.id_product 
INNER JOIN pr_varieties t4 
    ON t4.id = t1.id_variety 
INNER JOIN pr_grades t5 
    ON t5.id = t1.id_grade 
WHERE t2.status = 100 AND 
     t1.id_tenant = 1 AND 
     t1.date = '2017-03-22' 
GROUP BY t1.id_product, 
     t1.id_variety 
ORDER BY t1.id_product, 
     t4.variety, 
     t1.id_grade; 
+0

感謝您的回答,但查詢結果中可能會有更多的GRADE(GRADE1,GRADE2,GRADE3 ...) –

+0

然後,您將需要動態SQL。沒有快速和骯髒的解決方案。 –

+0

更有趣!讓我們舉出這個不完整的問題嗎? – maSTAShuFu

0

試試這個

添加該代碼

sum(case when pr_grades.Grade='Grade1' then pf_harvest.quantity else 0 end)) [Grade1], 
     sum(case when pr_grades.Grade='Grade2' then pf_harvest.quantity else 0 end)) [Grade2] 

SELECT 
    pr_products.product AS PRODUCT, 
    pr_varieties.variety AS VARIETY, 
    sum(case when pr_grades.Grade='Grade1' then pf_harvest.quantity else 0 end)) [Grade1], 
    sum(case when pr_grades.Grade='Grade2' then pf_harvest.quantity else 0 end)) [Grade2], 
    SUM(pf_harvest.quantity) AS TOTAL 
FROM pf_harvest 
INNER JOIN pf_performance 
    ON pf_performance.id = pf_harvest.id_performance 
INNER JOIN pr_products 
    ON pr_products.id = pf_harvest.id_product 
INNER JOIN pr_varieties 
    ON pr_varieties.id = pf_harvest.id_variety 
INNER JOIN pr_grades 
    ON pr_grades.id = pf_harvest.id_grade 
WHERE pf_performance.status = 100 
AND pf_harvest.id_tenant = 1 
AND pf_harvest.date = '2017-03-22' 
GROUP BY pf_harvest.id_product, 
     pf_harvest.id_variety 
ORDER BY pf_harvest.id_product, pr_varieties.variety, pf_harvest.id_grade; 
+0

你是什麼意思? – maSTAShuFu

+0

我的答案的好副本。 –

+0

沒有複製你的答案..我剛剛糾正了一些文書問題,當我審查了問題。僅供參考11分鐘前回答..你剛剛回答6分鐘前。 – maSTAShuFu