-1
這是我的第一篇文章,我希望有人可以提供幫助。我希望能夠在嘗試自行解決問題之前理解邏輯。這裏的想法是從左到右顯示特定的註釋(因此LOJ,每個LOJ是不同的註釋),對於每個我想拉最大服務日期的連接,最終將數據滾動到每行一行人。代碼如下,我非常感謝幫助。大部分是自學的,我正在使用SQL Server 2008 R2。此外,如果代碼太多,只是讓我知道它將如何幫助我凝結它,我會這樣做。TQL如何獲得每列最大值
SELECT dbo.tConTESTs.FirstName
,dbo.tConTESTs.LastName
,dbo.tConTESTs.MI
,dbo.tConTESTs.ConTESTID
,dbo.tConTESTs.SSN
,dbo.tConTESTOrganizations.OrganizationID
,dbo.tConTESTOrganizations.AdmitDate
,dbo.tConTESTOrganizations.DischargeDate
,ImprovementNote_1.ImprovementNoteID
,ImprovementNote_1.TemplateID
,ImprovementNote_1.ServiceDate
,ImprovementNote_2.ImprovementNoteID AS Expr1
,ImprovementNote_2.TemplateID AS Expr2
,ImprovementNote_2.ServiceDate AS Expr4
,ImprovementNote_3.ImprovementNoteID AS Expr5
,ImprovementNote_3.TemplateID AS Expr6
,ImprovementNote_3.ServiceDate AS Expr8
,ImprovementNote_4.ImprovementNoteID AS Expr9
,ImprovementNote_4.TemplateID AS Expr10
,ImprovementNote_4.ServiceDate AS Expr12
,ImprovementNote_5.ImprovementNoteID AS Expr13
,ImprovementNote_5.TemplateID AS Expr14
,ImprovementNote_5.ServiceDate AS Expr16
,ImprovementNote_6.ImprovementNoteID AS Expr17
,ImprovementNote_6.TemplateID AS Expr18
,ImprovementNote_6.ServiceDate AS Expr20
,ImprovementNote_7.ImprovementNoteID AS Expr21
,ImprovementNote_7.TemplateID AS Expr22
,ImprovementNote_7.ServiceDate AS Expr24
,ImprovementNote_8.ImprovementNoteID AS Expr25
,ImprovementNote_8.TemplateID AS Expr26
,ImprovementNote_8.ServiceDate AS Expr28
,ImprovementNote_9.ImprovementNoteID AS Expr29
,ImprovementNote_9.TemplateID AS Expr30
,ImprovementNote_9.ServiceDate AS Expr32
,ImprovementNote_10.ImprovementNoteID AS Expr33
,ImprovementNote_10.TemplateID AS Expr34
,ImprovementNote_10.ServiceDate AS Expr36
,ImprovementNote_11.ImprovementNoteID AS Expr37
,ImprovementNote_11.TemplateID AS Expr38
,ImprovementNote_11.ServiceDate AS Expr40
,ImprovementNote_13.ImprovementNoteID AS Expr45
,ImprovementNote_13.TemplateID AS Expr46
,ImprovementNote_13.ServiceDate AS Expr48
,ImprovementNote_14.ImprovementNoteID AS Expr49
,ImprovementNote_14.TemplateID AS Expr50
,ImprovementNote_14.ServiceDate AS Expr52
,ImprovementNote_15.ImprovementNoteID AS Expr53
,ImprovementNote_15.TemplateID AS Expr54
,ImprovementNote_15.ServiceDate AS Expr56
,ImprovementNote_16.ImprovementNoteID AS Expr57
,ImprovementNote_16.TemplateID AS Expr58
,ImprovementNote_16.ServiceDate AS Expr60
,ImprovementNote_17.ImprovementNoteID AS Expr61
,ImprovementNote_17.TemplateID AS Expr62
,ImprovementNote_17.ServiceDate AS Expr64
,ImprovementNote_18.ImprovementNoteID AS Expr65
,ImprovementNote_18.TemplateID AS Expr66
,ImprovementNote_18.ServiceDate AS Expr68
,ImprovementNote_19.ImprovementNoteID AS Expr69
,ImprovementNote_19.TemplateID AS Expr70
,ImprovementNote_19.ServiceDate AS Expr72
,ImprovementNote_20.ImprovementNoteID AS Expr73
,ImprovementNote_20.TemplateID AS Expr74
,ImprovementNote_20.ServiceDate AS Expr76
FROM dbo.tConTESTs
INNER JOIN dbo.tConTESTOrganizations ON dbo.tConTESTs.ConTESTID = dbo.tConTESTOrganizations.ConTESTID
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_1
ON ImprovementNote_1.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_1.TemplateID = 10
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_2 ON ImprovementNote_2.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_2.TemplateID = 13
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_3 ON ImprovementNote_3.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_3.TemplateID = 12
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_4 ON ImprovementNote_4.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_4.TemplateID = 14
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_5 ON ImprovementNote_5.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_5.TemplateID = 49
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_6 ON ImprovementNote_6.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_6.TemplateID = 19
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_7 ON ImprovementNote_7.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_7.TemplateID = 24
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_8 ON ImprovementNote_8.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_8.TemplateID = 25
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_9 ON ImprovementNote_9.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_9.TemplateID = 26
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_10 ON ImprovementNote_10.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_10.TemplateID = 27
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_11 ON ImprovementNote_11.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_11.TemplateID = 36
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_13 ON ImprovementNote_13.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_13.TemplateID = 51
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_14 ON ImprovementNote_14.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_14.TemplateID = 6
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_15 ON ImprovementNote_15.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_15.TemplateID = 7
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_16 ON ImprovementNote_16.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_16.TemplateID = 8
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_17 ON ImprovementNote_17.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_17.TemplateID = 35
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_18 ON ImprovementNote_18.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_18.TemplateID = 18
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_19 ON ImprovementNote_19.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_19.TemplateID = 16
LEFT JOIN dbo.ImprovementNote AS ImprovementNote_20 ON ImprovementNote_20.ConTESTID = dbo.tConTESTs.ConTESTID
AND ImprovementNote_20.TemplateID = 11
WHERE dbo.tConTESTOrganizations.DischargeDate IS NULL
AND dbo.tConTESTOrganizations.OrganizationID IN (2,3)
AND dbo.tConTESTs.ConTESTID = 508
ORDER BY dbo.tConTESTs.LastName
使用ROW_NUMBER()謝謝你對清除它Siyual,大加讚賞。 –
建議1:如果您的LOJ指定了精確的TemplateID,則不需要在選擇列表中檢索該TemplateID。 –
建議2:使用像'Expression74'這樣的列別名是一個非常糟糕的主意 - 它永遠不會被維護。由於您正在檢索特定模板ID的數據,因此使用描述該模板ID的別名怎麼樣:比如「ImprovementNoteID_template16」。 –