表 BASECASE(CaseNo, PID)
CaseNo| PID
===========
10 | 21
11 | 23
表 CHECKUP(ChkNo, CaseNo, CTime, PID)
ChkNo| CaseNo| CTime | PID
============================================
1 | 10 | 13 Jan 2014 03:05:15 PM| 21
2 | 11 | 17 Jan 2014 04:13:25 PM| 23
3 | 10 | 29 Mar 2014 02:45:34 PM| 21
表 DIAGNOSIS(CheckupNumber, Region, Value)
ChkNo| Region| Value
=====================
1 | AAA | CCC
1 | BBB | AAA
1 | CCC | CCC
2 | DDD | HHH
3 | BBB | BBB
3 | AAA | BBB
表 PATIENT(PID, Name)
PID| Name
==========
21 | John
22 | Smith
23 | Harry
SQL
SELECT B.CaseNo AS CNum, B.PID AS PatientID, C.CTime AS CTime, P.Name AS Name, D.ChkNo AS CheckupNumber, MAX(D.Region+' '+D.Value) AS Disease
FROM BASECASE B, CHECKUP C, PATIENT P, DIAGNOSIS D
WHERE D.ChkNo LIKE C.ChkNo AND C.CaseNo LIKE B.CaseNo AND P.PID LIKE B.PID AND C.CTime >='1 Jan 2014' AND C.CTime <'1 Jan 2015' AND (D.Region LIKE '%AAA%' OR D.Value LIKE '%AAA%')
GROUP BY B.CaseNo, B.PID, C.CTime, P.Name, D.ChkNo
ORDER BY C.CTime ASC
预期结果:
CaseNo| CTime | PID| Name| DISEASE| ChkNo
===========================================================
10 | 13 Jan 2014 03:05:15 PM | 21 | John| AAA CCC| 1
结果发生:
CaseNo| CTime | PID| Name| DISEASE| ChkNo
===========================================================
10 | 13 Jan 2014 03:05:15 PM | 21 | John| AAA CCC| 1
10 | 29 Mar 2014 02:45:34 PM | 21 | John| AAA BBB| 3
逻辑:
在这里,我们需要查看DIAGNOSIS的地区或价值是否为%AAA%&#39;,
如果我们发现地区或价值是%AAA%&#39;然后我们需要显示相应的(CaseNo,ChkNo,PID,Name,CTime FROM BASECASE,CHECKUP和PATIENT
但不应重复CaseNo。
选择
MAX(地区+&#39;&#39; +价值)来自 DIAGNOSIS [(地区或价值)=&#39;%AAA%&#39;]
DISTINCT(CaseNo),来自 BASECASE 的PatientID
CheckupNo,CTime来自 CHECKUP [CTime&gt;&#39; 2014年1月1日&#39;和CTime&lt;&#1; 2015年1月1日&#39;]
PATIENT
如何排序此问题?
答案 0 :(得分:1)
试试这个:
SELECT * FROM
(
SELECT B.CaseNo AS CNum, B.PID AS PatientID, C.CTime AS CTime, P.Name AS Name, D.ChkNo AS CheckupNumber, (D.Region + ' ' + D.Value ) AS Disease, ROW_NUMBER() OVER (PARTITION BY c.caseno ORDER BY c.ChkNo) rn FROM DIAGNOSIS D
JOIN CHECKUP C ON D.chkno = C.ChkNo
JOIN BASECASE B ON B.CaseNo = C.CaseNo
JOIN PATIENT P ON B.PID = P.PID
WHERE
C.CTime >= '1 Jan 2014' AND C.CTime < '1 Jan 2015'
AND (D.Region LIKE '%AAA%' OR D.Value LIKE '%AAA%')
) AS t WHERE rn = 1
注意:您可以将条件ORDER BY c.ChkNo
更改为ORDER BY c.CTime
,以获得最小CTime
或ORDER BY c.CTime DESC
的条件,以获得最大CTime
c.caseno
1}}在具有相同{{1}}。
答案 1 :(得分:0)
试试这个:(如果不工作更新您的状态)
SELECT B.CaseNo AS CNum, B.PID AS PatientID, C.CTime AS CTime, P.Name AS Name, D.CheckupNumber AS CheckupNumber, MAX(D.Region+' '+D.Value) AS Disease
FROM BASECASE B, CHECKUP C, pATIENT P, DIAGNOSIS D
WHERE D.CheckupNumber LIKE C.ChkNo AND C.CaseNo LIKE B.CaseNo AND P.PID LIKE B.PID AND C.CTime >='1 Jan 2014' AND C.CTime <'1 Jan 2015' AND (D.Region LIKE '%AAA%' OR D.Value LIKE '%AAA%')
GROUP BY B.CaseNo, B.PID, C.CTime, P.Name, D.CheckupNumber
ORDER BY C.CTime ASC