2016-09-07 146 views
-1

我有一個查詢按代理機構「Vente」和「Recouv」返回兩種類型。我希望我的請求顯示一行與「委員會」欄的總和。SQL數據透視表到列

這裏是一個畫面,顯示我有什麼什麼,我想:

select codeAgence,CASE WHEN (TypeOperation =0) THEN 'Vente' ELSE 'Recouvrement' END as 'Type', 
count(*) 'Nb',SUM(PxVenteHT) 'C.A',SUM(acompte) 'Montant',SUM(MontantCommission) 'Commission' from Commission_Archive 
    group by CodeAgence,TypeOperation 
    order by CodeAgence 

Enter image description here

回答

1

我想這可能會做你想要什麼,但你的問題缺乏適當的示例源數據I避風港沒有對它進行測試。

SELECT 
    codeAgence, 
    COUNT(CASE WHEN (TypeOperation = 0) THEN 'Vente' ELSE NULL END) 'Nb Vente', 
    SUM(CASE WHEN (TypeOperation = 0) THEN PxVenteHT ELSE 0 END) 'C.A', 
    COUNT(CASE WHEN (TypeOperation <> 0) THEN 'Recouve' ELSE NULL END) 'Nb Recouve', 
    SUM(CASE WHEN (TypeOperation <> 0) THEN acompte ELSE 0 END) 'Montant Recouve', 
    SUM(MontantCommission) 'Commission' 
FROM Commission_Archive 
GROUP BY CodeAgence 
ORDER BY CodeAgence 

關鍵是要有條件的聚合技術應用到所有應該對Nb/Recouve拆分和改變分組彙總列。

+1

謝謝你「jpw」,這是工作! –