如何在SQL Oracle中添加两个子查询的结果?

时间:2017-04-09 01:57:04

标签: sql oracle

这就是我所拥有的,它有效,但我也想要totalServicesSupp + totalTypeExamen的总和,我似乎无法弄明白。任何帮助将不胜感激。

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;

2 个答案:

答案 0 :(得分:0)

将问题中的查询放入派生表(带有别名的子查询)中并添加两个数字。

select temp.*
, totalServicesSupp + totalTypeExamen yourSum
from (
sql from your question) temp

答案 1 :(得分: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,但任何引用您的查询都可以这样做。例如,以下内容将为您提供一个包含总计...

的列表
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;

如果您有任何问题或意见,请随时发表评论。