2017-04-09 77 views
1

這是我有,它的工作原理,但我也想totalservicesSupp + totalTypeExamen的總和,我似乎無法弄清楚。任何幫助將不勝感激。如何將SQL中的兩個子查詢的結果加在一起?

SELECT 
    rendezvous.nordv, 
    rendezvous.nopatient, 
    rendezvous.noservicessupp, 
    servicessupplementaires.prix, 
    COUNT(rendezvous.noservicessupp) * servicessupplementaires.prix AS totalservicessupp, 
    rendezvous.notypeexamen, 
    typeexamen.prix, 
    COUNT(rendezvous.notypeexamen) AS notypeexamen, 
    COUNT(rendezvous.notypeexamen) * typeexamen.prix AS totaltypeexamen 
FROM 
    rendezvous 
    FULL JOIN servicessupplementaires ON rendezvous.noservicessupp = servicessupplementaires.noservicessupp 
    FULL JOIN typeexamen ON rendezvous.notypeexamen = typeexamen.notypeexamen 
WHERE 
     rendezvous.noservicessupp >= 1 
    OR 
     rendezvous.notypeexamen >= 1 
GROUP BY 
    nopatient, 
    rendezvous.nopatient, 
    nordv, 
    servicessupplementaires.prix, 
    rendezvous.noservicessupp, 
    rendezvous.nordv, 
    servicessupplementaires.prix, 
    typeexamen.prix, 
    rendezvous.notypeexamen; 

如何獲得totalServicesSupp + totalTypeExamen的總數也是??

謝謝大家。編輯/////////////////////還在尋找一些幫助,謝謝****

謝謝,這段代碼工作得很好,現在我想要計算一個百分比表示的百分比(0.1爲10%或0.2爲20%...) (patient.CouvertureAss是存儲的地方),所以它在另一個表中。我在實施一個小麻煩

,這裏是我的嘗試:

SELECT rendezvous.nordv, 
     rendezvous.nopatient, 
     rendezvous.noservicessupp, 
     servicessupplementaires.prix, 
     COUNT(rendezvous.noservicessupp) * servicessupplementaires.prix AS totalservicessupp, 
     rendezvous.notypeexamen, 
     typeexamen.prix, 
     COUNT(rendezvous.notypeexamen) * typeexamen.prix AS totaltypeexamen, 
     COUNT(rendezvous.noservicessupp) * servicessupplementaires.prix + 
      COUNT(rendezvous.notypeexamen) * typeexamen.prix AS grandTotal, 
     COUNT(rendezvous.noservicessupp) * servicessupplementaires.prix + 
      COUNT(rendezvous.notypeexamen) * typeexamen.prix * (1 - patient.CouvertureAss) AS TotalPatient 
FROM rendezvous 
FULL JOIN servicessupplementaires ON rendezvous.noservicessupp = servicessupplementaires.noservicessupp 
FULL JOIN typeexamen ON rendezvous.notypeexamen = typeexamen.notypeexamen 
FULL JOIN patient ON rendezvous.nopatient = patient.nopatient 
WHERE rendezvous.noservicessupp >= 1 
    OR rendezvous.notypeexamen >= 1 
    OR rendezvous.nopatient >= 1 
GROUP BY nopatient, 
     rendezvous.nopatient, 
     nordv, 
     servicessupplementaires.prix, 
     rendezvous.noservicessupp, 
     rendezvous.nordv, 
     servicessupplementaires.prix, 
     typeexamen.prix, 
     rendezvous.notypeexamen, 
     patient.nopatient; 
+0

在上面的代碼的GROUP BY中你引用了'nopatient','rendezvous.nopatient','nordv'和'rendezvous.nordv'是每一對之間的區別嗎?你有沒有測試運行上面的代碼? – toonice

+0

請修改您的問題,以指出患者與如此開發的陳述已被使用的表格有何關係。 – toonice

回答

0

請嘗試...

SELECT rendezvous.nordv, 
     rendezvous.nopatient, 
     rendezvous.noservicessupp, 
     servicessupplementaires.prix, 
     COUNT(rendezvous.noservicessupp) * servicessupplementaires.prix AS totalservicessupp, 
     rendezvous.notypeexamen, 
     typeexamen.prix, 
     COUNT(rendezvous.notypeexamen), 
     COUNT(rendezvous.notypeexamen) * typeexamen.prix AS totaltypeexamen, 
     COUNT(rendezvous.noservicessupp) * servicessupplementaires.prix + 
      COUNT(rendezvous.notypeexamen) * typeexamen.prix AS grandTotal 
FROM rendezvous 
FULL JOIN servicessupplementaires ON rendezvous.noservicessupp = servicessupplementaires.noservicessupp 
FULL JOIN typeexamen ON rendezvous.notypeexamen = typeexamen.notypeexamen 
WHERE rendezvous.noservicessupp >= 1 
    OR rendezvous.notypeexamen >= 1 
GROUP BY nopatient, 
     rendezvous.nopatient, 
     nordv, 
     servicessupplementaires.prix, 
     rendezvous.noservicessupp, 
     rendezvous.nordv, 
     servicessupplementaires.prix, 
     typeexamen.prix, 
     rendezvous.notypeexamen; 

請注意,在這個水平上,你不能說'totalservicessupp + totalTypeExamen AS grandTotal,但是任何查詢指你將能夠做到這一點。例如,下面會給你總清單consiting ...

SELECT totalservicessupp + totalTypeExamen AS grandTotal 
FROM 
{ 
    SELECT rendezvous.nordv, 
      rendezvous.nopatient, 
      rendezvous.noservicessupp, 
      servicessupplementaires.prix, 
      COUNT(rendezvous.noservicessupp) * servicessupplementaires.prix AS totalservicessupp, 
      rendezvous.notypeexamen, 
      typeexamen.prix, 
      COUNT(rendezvous.notypeexamen), 
      COUNT(rendezvous.notypeexamen) * typeexamen.prix AS totaltypeexamen 
    FROM rendezvous 
    FULL JOIN servicessupplementaires ON rendezvous.noservicessupp = servicessupplementaires.noservicessupp 
    FULL JOIN typeexamen ON rendezvous.notypeexamen = typeexamen.notypeexamen 
    WHERE rendezvous.noservicessupp >= 1 
     OR rendezvous.notypeexamen >= 1 
    GROUP BY nopatient, 
      rendezvous.nopatient, 
      nordv, 
      servicessupplementaires.prix, 
      rendezvous.noservicessupp, 
      rendezvous.nordv, 
      servicessupplementaires.prix, 
      typeexamen.prix, 
      rendezvous.notypeexamen 
} tempTable; 

如果您有任何問題或意見,然後請隨時據此發佈評論。

+0

按輸入或添加一個新行字符完成評論。 – toonice

+0

另外,你如何計算百分比?即您如何確定應將哪些數字彙總在一起?我們是否將這些數字中有多少數字或者其他相應字段的總和除?請用答案更新您的問題。 – toonice

+0

我用第一個問題的答案更新了問題,但我仍然需要計算總數乘以代表百分比的十進制數並存儲在單獨的表中。 –

0

把查詢你的問題變成一個派生表(用別名的子查詢),並添加了兩個數字。

select temp.* 
, totalServicesSupp + totalTypeExamen yourSum 
from (
sql from your question) temp 
相關問題