简体   繁体   中英

Sum of a column in 1 table group by 2 other columns in another table

I have 2 tables, the 1st one is

outcomes

ssn,date,scode,dcode,ppay,ipay
302609074,20180215,4002,1001,25,35
302609074,20180215,4006,1003,40,100
302609074,20181010,4002,1001,25,35
302609074,20181010,4006,1003,40,100
302609074,20190505,4002,1001,25,35
302609074,20190506,4006,1003,40,100
997600970,20190115,4001,1002,25,50
997600970,20190115,4001,1003,40,30
997600970,20190115,4004,1004,40,50
997600970,20190415,4002,1004,40,95
997600970,20190415,4003,1005,40,100
997600970,20181212,4004,1001,25,47
997600970,20181213,4004,1001,25,47
874136439,20190808,4002,1002,25,50
874136439,20190808,4002,1003,40,60
874136439,20190910,4002,1004,40,95
874136439,20190910,4002,1005,60,90
874136439,20191001,4003,1002,25,50
874136439,20191002,4003,1002,25,50
874136439,20191003,4003,1002,25,55
831287780,20190404,4001,1001,25,0
831287780,20190505,4003,1002,25,40
831287780,20190606,4004,1003,25,100
831287780,20190707,4006,1004,60,100
882861510,20190202,4005,1002,60,60
882861510,20190303,4002,1004,25,100
882861510,20190404,4005,1002,40,100
882861510,20190505,4002,1004,50,100
882861510,20190606,4005,1003,50,0
796235486,20190808,4001,1001,25,25
445139565,20180707,4002,1001,40,35
445139565,20190929,4002,1003,40,70
445139565,20190929,4002,1004,40,95
445139565,20190929,4002,1005,40,120
524246868,20190909,4006,1005,100,100
524246868,20190910,4006,1005,100,100
416806352,20190818,4003,1001,25,30
416806352,20190818,4003,1005,50,100
946883650,20190717,4001,1004,25,100
946883650,20190817,4001,1004,25,100
946883650,20190917,4006,1004,50,132
946883650,20191002,4006,1004,50,132
129141378,20190909,4001,1002,25,50
129141378,20190909,4001,1003,0,100
129141378,20190909,4001,1004,0,125
129141378,20190909,4001,1005,0,150
950022926,20181111,4006,1004,182,0
950022926,20190323,4006,1004,100,82
950022926,20190623,4006,1004,0,182
799023031,20190930,4005,1003,50,100
719301054,20190110,4001,1003,25,75
719301054,20190210,4001,1003,25,75
719301054,20190310,4001,1003,25,75
316517393,20190626,4005,1005,0,0
148694321,20190102,4006,1002,40,72
148694321,20190203,4006,1002,40,72
148694321,20190304,4006,1002,40,72
332124842,20190405,4004,1001,25,47
332124842,20190506,4004,1002,25,80
332124842,20190607,4004,1003,25,107

and diagnosis


scode,dcode,charge
4001,1001,50
4001,1002,75
4001,1003,100
4001,1004,125
4001,1005,150
4002,1001,60
4002,1002,85
4002,1003,110
4002,1004,135
4002,1005,160
4003,1001,55
4003,1002,80
4003,1003,105
4003,1004,130
4003,1005,155
4004,1001,72
4004,1002,102
4004,1003,132
4004,1004,162
4004,1005,192
4005,1001,90
4005,1002,140
4005,1003,190
4005,1004,240
4005,1005,290
4006,1001,77
4006,1002,112
4006,1003,147
4006,1004,182
4006,1005,217

I need to get the SUM(charge) for each SSN on the same date. For example from

302609074,20180215,4002,1001,25,35
302609074,20180215,4006,1003,40,100

I want to get the sum(charge) = 60 + 147 = 207 from table diagnosis, on date 20180215

Nothing I've tried has worked so far. For example, I got:

ELECT Z.date, Z.ssn, TotalCharges
FROM (  
        (SELECT ssn,date,scode,dcode
         FROM outcome
        ) Z
        JOIN 
        (SELECT scode, dcode, charge, SUM(charge) TotalCharges
         FROM diagnosis
        ) U
        ON (U.scode = Z.scode AND U.dcode = Z.dcode)
     )
     GROUP BY Z.date, Z.ssn;

and got an error ORA-00937: not a single-group group function

Why would you use so many subqueries?

SELECT o.date, o.ssn, SUM(d.charge)
FROM outcome o JOIN
     diagnosis d
     ON d.scode = o.scode AND d.dcode = o.dcode
GROUP BY o.date, o.ssn;

Your query might also work if the GROUP BY were in the subquery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM