这是我的代码,但我需要group by选项,因为我需要通过Derparti对子查询进行分组,例如我需要找到每个部门中有多少医生工作,所以我需要我的子查询返回多行。
SELECT d.Dreparti,
(SELECT avg(Paga)
FROM dpaga dp
WHERE d.titulli=dp.titulli
GROUP BY Dreparti),
(SELECT COUNT(*)
FROM Doktori
GROUP BY Dreparti),
(SELECT COUNT(*)
FROM doktori d,
Hospitalizimidata hd
WHERE d.did=hd.did
GROUP BY Dreparti),
(SELECT sum(CmimipaTVSH)
FROM faturat f,
hospitalizimidata h,
doktori d
WHERE f.hid=h.hid
AND h.did=d.did
AND DataFatures BETWEEN CAST(CURDATE() - INTERVAL 365 DAY AS DATE) AND CAST(curdate() AS DATE)
GROUP BY Dreparti) ,
(SELECT sum(Cmimi)
FROM faturat f,
hospitalizimidata h,
doktori d
WHERE f.hid=h.hid
AND h.did=d.did
AND DataFatures BETWEEN CAST(CURDATE() - INTERVAL 365 DAY AS DATE) AND CAST(curdate() AS DATE)
GROUP BY Dreparti) ,
(SELECT sum(Cmimi)
FROM pagesat p,
faturat f,
hospitalizimidata h,
doktori d
WHERE f.hid=h.hid
AND h.did=d.did
AND p.fid=f.fid
AND DataFatures BETWEEN CAST(CURDATE() - INTERVAL 365 DAY AS DATE) AND CAST(curdate() AS DATE)
GROUP BY Dreparti)
FROM doktori d
GROUP BY Dreparti;
答案 0 :(得分:0)
您需要将子查询从选择列表移动到列表中作为派生表。使用表/查询作为第一个返回所有(或至少最多)部门的表,并使用departments字段将其连接到tge 1st表上的其他派生表。我将为您提供3个子查询的示例代码,假设医生表中包含所有部门:
SELECT * FROM
(SELECT Dreparti, COUNT(*) AS NoOfDoctors
FROM Doktori
GROUP BY Dreparti) t1
LEFT JOIN (SELECT Dreparti, avg(Paga) AS AvgPaga
FROM dpaga dp inner join Doktori d
ON d.titulli=dp.titulli
GROUP BY Dreparti) t2 ON t1.Dreparti=d2.Dreparti
LEFT JOIN (SELECT Dreparti, COUNT(*) AS NoOfSomething
FROM doktori d INNER JOIN
Hospitalizimidata hd
ON d.did=hd.did
GROUP BY Dreparti) t3 ON t1.Dreparti=t3.Dreparti
但是,我会将部门的主数据表用作最左边的表。既然你没有提到任何关于它的事情,我没有把它包括在内。