如何写这样复杂的查询?

时间:2015-12-27 16:56:36

标签: mysql sql mysql-workbench

我的表格报告包含:

rid     reparti
R00001  Ortopedi
R00002  Kardiologji
R00003  Gjinekologji

表医生包含:

did      demri    dmbiemri  titullli      rruga        qyteti    kp     shteti  mob         fix       email                 rid     paga
D00001   Shaban   Merovci   Dr Familjar   Mic Sokoli   Vushtrri  42000  Kosove  37744842203 28570369  shmerovci@gmail.com   R00001  1000

表约会:

tid     pid     did     rid     data_koha  konfirmimi
T00001  P00001  D00010  R00002  2015-12-20 08:30:00 1
T00002  P00009  D00004  R00003  2015-12-25 10:30:00 1
表pospilization(没有预约,你可以住院):

  hpid    pid     did     data_shtrimi  sid      terapia    diagnoza         tid
HP0001  P00009  D00004  2015-12-13    S00001   Barera     Hemorislapaskus  T00002
HP0002  P00006  D00002  2015-12-22    S00010   Kimoterapi Fragaria vesca   T00008
HP0003  P00001  D00002  2015-12-20    S00008   Barera     Depresion        T00001

表格账单包含:

 fid     pid     hpid    cmimi_patvsh  tvsh  cmimi_metvsh data_fatures
    F00001  P00002  HP0005  85.00         0.18  100.30       2016-02-13
    F00002  P00009  HP0001  700.00        0.18  826.00       2016-12-10
    F00003  P00001  HP0003  120.00        0.18  141.60       2015-12-29

我的目的是找到每个报告的医生数量(确实)作为NrDoktoreve工作,avg(paga)作为PagaMesatare的那个部分,住院数量为NrHospitalizimeve今年实现的,账单总和(值cmimi_patvsh,价值观cmimi_metvsh)今年实现了。所有这些都应该在一个数据集列表中。

当然,这些表包含了更多数据,我在这里放了一些例子。

2 个答案:

答案 0 :(得分:0)

查询不是太复杂,方法是识别连接列(表格放在一起),写入连接,按RID对表进行分组(因为你想要每个RID的所有结果),最后选择字段。 我没有可用于测试的MySQL,但查询应如下所示:

select 
    rid,
    count(distinct did) NrDoktoreve, 
    avg(cmimi_metvsh) as PagaMesatare,
    count(distinct hpid) as NrHospitalizimeve,
    sum(cmimi_patvsh),
    sum(cmimi_metvsh)
        from reparts r 
        left join appointments a on r.rid = a.rid 
        left join hospitalization h on a.tod = h.tid 
        left join doctors d on r.rid = d.rid
        left join bills b on a.pid = b.pid
    group by rid
    order by rid;

如果您对编写SQL感到不自在,可以考虑像DbSchema(http://www.dbschema.com/)这样的图形工具(只有很多可用工具中的一个例子,但它适用于MySQL)。

答案 1 :(得分:0)

这是我的问题的解决方案

select t1.rid,t1.reparti,t1.NrDoktoreve,t1.PagaMesatare,t2.NrHospitalizimeve,t3.VleraPaTvsh,t3.VleraMeTvsh from(
(select r.rid,r.reparti,count(d.did) as NrDoktoreve,avg(d.paga) as PagaMesatare from repartet r
left join doktoret d on r.rid=d.rid
group by r.rid order by r.rid ) as t1 left join 
(
select count(h.hpid) as NrHospitalizimeve ,r.rid
 from faturat f
inner join hospitalizimet h on f.hpid=h.hpid
inner join terminet t on h.tid=t.tid
inner join repartet r on t.rid=r.rid
where h.data_shtrimit between (curdate()-interval 1 year) and curdate() 
group by r.rid order by r.rid) as t2 on t1.rid=t2.rid left join
(select sum(f.cmimi_patvsh) as VleraPaTvsh,sum(f.cmimi_metvsh) as VleraMeTvsh,r.rid
from repartet r
left join terminet t on r.rid=t.rid
left join hospitalizimet h on t.tid=h.tid
left join faturat f on h.hpid=f.hpid
where data_fatures between (curdate()-interval 1 year) and curdate()
group by r.rid order by r.rid)as t3 on t2.rid=t3.rid)