2014-02-18 117 views
1

我還沒有發現任何SQL連接問題相當大,這是問題:我有兩個SQL語句,一個將資金分配到單獨的組中,一個獲得花在那些團體,我如何將他們加入到一起,以便將「預算」和「費用」一起顯示出來? 很多,而不是有部門,資金類別,預算和部門,資金類別,費用表,我想要一個部門,資金類別,預算,費用表。SQL加入兩個語句

SELECT st.name as "Department", sft.longname as "Funding Category", sum(sf.amount) as "Budget" 
FROM 
money_type as st, 
money_funding_type as sft, 
money_funding as sf 
WHERE 
st.ID = sf.type_ID AND 
sft.ID = sf.funding_ID 
GROUP BY 
sf.type_id, sf.funding_id 

SELECT st.name as "Department", sft.longname as "Funding Category", sum(si.amount) as "Expenses" 
FROM 
money_type as st, 
money_funding_type as sft, 
money_invoice as si 
WHERE 
st.ID = si.type_ID AND 
sft.ID = si.funding_ID 
GROUP BY 
si.type_id, si.funding_id 

回答

0

你可以嘗試以下。第一個查詢「AllTypes」是獲得可用數據的不同id /類型,而不是笛卡兒。從這一點,簡單的加入類型和資金類型來獲得描述。

然後,兩個選擇簡化了他們自己的money_funding/money_invoice元素,只是獲得他們的「id」字段和金額總和,因此對於每個預算和費用,每個類型/資金將有1條記錄。然後這些左加入到「AllTypes」,如果他們有相應的價值,抓住了結果,否則爲0。

之間的聯接爲可讀性只

SELECT 
     st.name as "Department", 
     sft.longname as "Funding Category", 
     coalesce(Budgets.Budget, 0) as Budget, 
     coalesce(Expenses.Expense, 0) as Expense, 
     coalesce(Budgets.Budget, 0) - coalesce(Expenses.Expense, 0) as AmtRemain 
    from 
     (select distinct 
       type_id, 
       funding_id 
      from 
       money_funding 
     union 
     SELECT distinct 
       type_id, 
       funding_id 
      from 
       money_invoice ) AllTypes 

     JOIN money_type as st 
      ON AllTypes.type_id = st.id 

     JOIN money_funding_type as sft 
      ON AllTypes.funding_id = sft.id 

     LEFT JOIN (SELECT 
          sf.type_id, 
          sf.funding_id, 
          sum(sf.amount) as "Budget" 
         FROM 
          money_funding as sf 
         GROUP BY 
          sf.type_id, 
          sf.funding_id) Budgets 
      ON AllTypes.type_id = Budgets.Type_id 
      AND AllTypes.funding_id = Budgets.Funding_ID 

     LEFT JOIN (SELECT 
          si.type_id, 
          si.funding_id, 
          sum(si.amount) as "Expense" 
         FROM 
          money_invoice as si 
         GROUP BY 
          si.type_id, 
          si.funding_id) Expenses 
      ON AllTypes.type_id = Expenses.Type_id 
      AND AllTypes.funding_id = Expenses.Funding_ID 

額外的換行符「 AllTypes「查詢,您可能需要刪除第二個」DISTINCT「子句,因爲第一個可能適用於整個」AllTypes「查詢,並可能會給您一個錯誤。

+0

非常感謝! – user3324909

0

使用FULL OUTER JOIN加入發票和資金,然後通過moneytype,資金和發票進行分組。

+0

你能給我SQL嗎?我做的不是SQL,我真的不知道這是如何工作的。 – user3324909

1

如何像

SELECT st.name as "Department", 
     COALESCE(sft.longname,sfti.longname) as "Funding Category", 
     sum(sf.amount) as "Budget", 
     sum(si.amount) as "Expenses" 
FROM money_type as st LEFT JOIN 

     money_funding as sf ON st.ID = sf.type_ID LEFT JOIN 
     money_funding_type as sft ON sft.ID = sf.funding_ID LEFT JOIN 

     money_invoice as si ON st.ID = si.type_ID LEFT JOIN 
     money_funding_type as sfti ON sfti.ID = si.funding_ID 
GROUP BY st.name, 
      COALESCE(sft.longname,sfti.longname) 
+0

該腳本凍結了服務器... – user3324909

+0

它終於完成了,不起作用。如果費用值應爲475.9,則爲951.8,其值應返回26452.19,它返回72388013.48009163 – user3324909

+0

向我們顯示您的實際表結構,以及一些示例數據,我們可以看看它。這是基於您提供的查詢的最佳效果。 –