SQL Oracle JOIN和AVG

时间:2019-05-03 00:54:09

标签: sql oracle

我有4张桌子:费用,员工,访问,账单

成本

SpecialtyCode   DiagnosisCode   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

和员工

ENum  EName       Title
101   Zhivago     Doctor
102   Welby       Doctor
103   Jekyl       Doctor
104   Caligari    Doctor
105   Nightingale Nurse
106   Ratchet     Nurse

和访问

PatientSSN,ENum,Date
302609074,101,20180215
302609074,102,20181010
302609074,105,20190505
302609074,105,20190506
997600970,103,20190115
997600970,105,20190415
997600970,104,20181212
997600970,104,20181213
874136439,102,20190808
874136439,102,20190910
874136439,102,20191001
874136439,102,20191002
874136439,102,20191003
831287780,101,20190404
831287780,102,20190505
831287780,103,20190606
831287780,104,20190707
882861510,106,20190202
882861510,105,20190303
882861510,106,20190404
882861510,105,20190505
882861510,106,20190606
796235486,104,20190808
445139565,102,20180707
445139565,102,20190929
524246868,103,20190909
524246868,103,20190910
416806352,102,20190818
946883650,103,20190717
946883650,106,20190817
946883650,103,20190917
946883650,106,20191002
129141378,103,20190909
950022926,101,20181111
950022926,105,20190323
950022926,101,20190623
799023031,106,20190930
719301054,103,20190110
719301054,103,20190210
719301054,103,20190310
316517393,102,20190626
148694321,101,20190102
148694321,101,20190203
148694321,101,20190304
332124842,104,20190405
332124842,104,20190506
332124842,104,20190607

结算

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


我的问题是如何退还该员工(enumenametitle)及其平均每次访问费用?

这是我想出的:

SELECT employee.eno, employee.ename, AVG(billing.charge)
FROM visits
JOIN billing
ON visits.ssn = outcome.ssn AND visits.date = outcome.date   
JOIN costs
ON billing.scode = costs.scode AND billing.dcode = costs.dcode
JOIN employee 
ON employee.eno = visits.eno 
GROUP BY employee.eno, employee.ename;

哪个返回:

ENUM ENAME                         AVG(billing.charge)
---------- ------------------------------ ----------
103 Jekyl                          130.666667
102 Welby                          116.588235
104 Caligari                       97.4285714
106 Ratchet                        161.166667
105 Nightingale                    135.571429
101 Zhivago                           119.625

这是不正确的,因为对101名员工Zhivago来说,他进行了7次访问,但是由于他进行了8次计费,因为访问302609074,101,20180215产生了2次计费。因此,对于8个结算,AVG为119.625,而对于7次访问,则为136.71。

0 个答案:

没有答案