在Oracle中查询(使用连接选择,求和和计数)

时间:2017-03-20 05:24:28

标签: java sql oracle10g

表1: - 注册表

Date_phy       |Name      |Address  |Diseases   |Mob    | Advance   |Code
12 March, 2017 |Navneet   |Delhi    |Fever      |12345  | 1000      |2
12 March, 2017 |Ashutosh  |Gurgaon  |fever      |4321   | 2000      |3
12 March, 2017 |Bitu      |Mumbai   |fever      |2345   | 1500      |4
15 March, 2017 |Rashmi    |pune     |fever      |34567  | 1600      |5
16 March, 2017 |komal     |jaipur   |fever      |43215  | 7000      |6

表2: - Attendence Table

Code    |Name        |Date_phy
2       |Navneet     |13 March,2017
2       |Navneet     |14 March,2017
2       |Navneet     |15 March,2017
5       |Rashmi      |16 March,2017
6       |komal       |17 March,2017

表3: - 存款表

Code    |Amount   |Date_phy
2       |1000     |13 March,2017
2       |500      |14 March,2017
2       |500      |15 March,2017
5       |3000     |16 March,2017
6       |5000     |17 March,2017

我想在oracle中输出以下内容。

Name     |Date of Joining    |Code  |Total Paid(advance+deposit) |Total Present
Navneet  |12 March,2017      |2     |3000                        |3
Ashutosh |12 March,2017      |3     |2000                        |0
Bitu     |12 March,2017      |4     |1500                        |0
Rashmi   |15 March,2017      |5     |4600                        |1
Komal    |16 March,2017      |6     |12000                       |1

我无法编写查询。我必须将三个表与sum和count函数合并。 尝试过每件事但失败了。 你能帮我吗?

查询已尝试:

SELECT pr.NAME, 
       pr.date_phy, 
       pr.disease 
FROM   physiofit_registration pr 
       LEFT JOIN (SELECT Sum(amount) totalAmount 
                  FROM   physiofit_amt_deposit 
                  GROUP  BY code) pd 
              ON pr.code = pd.code 
       LEFT JOIN (SELECT Count(*) totalatten, 
                  FROM   physiofit_attendence 
                  GROUP  BY code) pa 
              ON pr.code = pa.code; 

1 个答案:

答案 0 :(得分:0)

尝试以下

{{1}}