我正在尝试向其他人添加的查询添加内部联接,但是我不断出错。我正在尝试找出添加它的正确方法, 要添加的内部联接是:
select
pa.AdmissionID, pad.AdmissionID, pad.AdmTypeID
from
v_dbPatientAdmissions pa
inner join
V_DbPatientAdmissionAddition pad on pad.AdmissionID = pa.AdmissionID
where
pad.AdmTypeID in (3, 37, 42, 43, 44, 51)
我要向其添加内部联接的查询是:
SELECT
PA.RegCode UHID, PA.Specialisation, A.CREATEDATE ADM_ORDER,
PA.CREATEDATE ADM_DATE,
(CAST((DATEDIFF(minute, A.CREATEDATE, PA.CREATEDATE))/60 AS VARCHAR)+' H:'
+ CAST((DATEDIFF(minute,A.CREATEDATE, PA.CREATEDATE))%60 AS VARCHAR)+' M') AS DURATION
FROM
(SELECT
M.IPID, M.PatientID, A.CREATEDATE
FROM
v_dbPatientAdvice A
INNER JOIN
v_dbPatientMonitoring M ON M.MonitorID = A.MonitorID
WHERE
A.FollowUpType = 2) A
INNER JOIN
(SELECT
PA.PatientID, PA.CREATEDATE, PA.RegCode, S.Specialisation, PA.PatientType
FROM
v_dbPatientAdmissions PA
INNER JOIN
v_dbSpecializations S ON S.SpecialiseID = PA.SpecialiseID) PA ON A.PatientID = PA.PatientID
AND PA.PatientType = 2
AND PA.CREATEDATE > A.CREATEDATE
AND PA.CREATEDATE < DATEADD(DAY, 1, A.CREATEDATE)
WHERE
A.CREATEDATE > '2017-01-01'
AND A.CREATEDATE < DATEADD(DAY, 1, '2018-09-20')
要添加的内部联接具有一个新表V_DbPatientAdmissionAddition
,另一个表已在查询v_dbPatientAdmissions
中。
我尝试将其添加为子查询,还尝试不使用From v_dbPatientAdmissions
添加它,但是语法仍然存在问题。
如果有人可以帮助我将内部联接添加到查询中,那将是有帮助的!我希望我能够清楚地解释这个问题。
答案 0 :(得分:1)
您可以使用CTE简化这项工作:
with cte as (
select pa.AdmissionID, pad.AdmissionID,pad.AdmTypeID
from v_dbPatientAdmissions pa inner join
V_DbPatientAdmissionAddition pad on pad.AdmissionID=pa.AdmissionID
where pad.AdmTypeID in (3, 37, 42, 43, 44, 51)
)
select . . .
from . . . join
newtable nt
on ?.? = nt.? -- whatever the `join` conditions are
. . .
答案 1 :(得分:1)
您可以扩展第二个子查询。您还需要使用要返回的新列扩展主SELECT。
Option Explicit
Sub LoopThroughDirectory()
Dim myFile As String, filepath As String
Dim wbc As Long, ws As Worksheet, wb As Workbook
Dim diaFolder As FileDialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
wbc = 0
filepath = diaFolder
Application.ScreenUpdating = False
'only try to open xlsm workbooks
myFile = Dir(filepath & "*.xlsm*")
'Opens workbooks located _Application Assurance\Maturity Assessments in order
Do While Len(myFile) > 0
'Make sure myFile isn't ThisWorkbook
If Split(myFile & ".", ".")(0) <> Split(ThisWorkbook.Name & ".", ".")(0) Then
Set wb = Workbooks.Open(Filename:=filepath & myFile, ReadOnly:=True)
'Check if there is a Results worksheet
On Error Resume Next
Set ws = wb.Worksheets("Results")
On Error GoTo 0
If Not ws Is Nothing Then
'Transfer cells B2 & C2 from the results worksheet
With ws.Range("A2:B2")
ThisWorkbook.Worksheets("AMT").Range("B4").Offset(wbc, 0).Resize(.Rows.Count, .Columns.Count) = .Value
End With
End If
'Close wb most recently opened
wb.Close SaveChanges:=False
wbc = wbc + 1
If wbc > 1000 Then Exit Do
End If
Set ws = Nothing
myFile = Dir
Loop
ActiveWorkbook.Save
End Sub