我正在尝试根据case语句编写where子句。我正在获得一个多部分标识符" k.Author"无法绑定。
我做了一些研究,发现在Case语句之前处理了where子句,所以我将case语句放在一个子查询中。子查询以K结尾,列以作者结束。
有人可以帮助我弄清楚如何让这个工作。我只希望列显示K.Author ='协调员1,推荐人的位置。
错误
Msg 4104, Level 16, State 1, Line 42
The multi-part identifier "k.Author" could not be bound.
SQL查询
SELECT MCON.MailHeader_DateSent,
VP.Person_Name AS [Patient Name],
MCON.MailHeader_Subject,
COUNT(MCON.MailHeader_ID) AS [Inboxed Mesage]
MTA.MailDetail_Folder,
EP.Person_Name AS [To],
N.Note_DateOccurred,
CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)), --Missing a Column Alias here
CASE WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%ashlee%' THEN 'Ashlee ' + CHAR(10) + 'Castro' --I have assumed the line break mid name is meant to be there
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%cordova%' THEN 'Adrian ' + CHAR(10) + 'Cordova'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%lyndsay%' THEN 'Lyndsay ' + CHAR(10) + 'Frommer'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Lyndsay%' THEN 'Lyndsay ' + CHAR(10) + 'Frommer'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%rivera%' THEN 'Rivera ' + CHAR(10) + 'Margaret'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Margaret%' THEN 'Rivera ' + CHAR(10) + 'Margaret'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Jtroy%' THEN 'Jennifer ' + CHAR(10) + 'Troy'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Adrian C%' THEN 'Adrian ' + CHAR(10) + 'Cordova'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Ann B%' THEN 'Ann ' + CHAR(10) + 'Burdge'
ELSE 'N/A'
END AS [Referal Agent],
(SELECT CASE WHEN CAST(CN.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Coordinator 1, ' + CHAR(10) + 'Referrals%' THEN 'Coordinator 1, Referrals' --Assumed same about line break here
ELSE 'Not A Referral Note'
END AS 'Author'
FROM CHRT_Note CN) AS k
FROM MSG_MailHeader MH
JOIN MSG_MailDetail MD on MCON.MailHeader_ID = MTA.MailHeader_ID
JOIN ENTY_Person EP on MTA.Entity_ID = EP.Entity_ID
JOIN TASK_TaskAttachment TA on MCON.MailHeader_ID = TA.MailHeader_ID
JOIN View_Patient VP on TA.Patient_ID = VP.Patient_ID
JOIN CHRT_Visit CV on VP.Patient_ID = CV.Patient_ID
JOIN CHRT_VisitCPT CVC on CV.Note_ID = CVC.Note_ID
JOIN CHRT_OtherNote CON on TA.Patient_ID = CON.Patient_ID
JOIN CHRT_Note N on CON.Note_ID = N.Note_ID
WHERE MTA.MailDetail_Folder = 3
AND MCON.MailHeader_DateSent BETWEEN '1/22/2018' AND '1/22/2018 23:59:59'
AND EP.Person_Name = 'Coordinator 1, Referrals'
AND CVC.VisitCPT_Code IN (...RemoveTA.. For example)
AND N.Note_DateOccurred > MCON.MailHeader_DateSent
AND k.Author = 'Coordinator 1, Referrals'
GROUP BY VP.Person_Name,
MCON.MailHeader_DateSent,
MTA.MailDetail_FolderP.Person_Name,
N.Note_DateOccurred,
MCON.MailHeader_Subject,
CAST(N.Note_SummaryRTF AS NVARCHAR(MAX));
答案 0 :(得分:2)
好的,既然我已经把SQL写成了可读的东西,是的,其他的是正确的,你没有一个名为或别名k
的表。但是,您确实有一个别名为k
的列(您无法通过别名在WHERE
子句中引用该列。)
其他人再次使用CROSS APPLY
。因此,你最终得到了这个:
SELECT MCON.MailHeader_DateSent,
VP.Person_Name AS [Patient Name],
MCON.MailHeader_Subject,
COUNT(MCON.MailHeader_ID) AS [Inboxed Mesage]
MTA.MailDetail_Folder,
EP.Person_Name AS [To],
N.Note_DateOccurred,
CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)), --Missing a Column Alias here
CASE WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%ashlee%' THEN 'Ashlee ' + CHAR(10) + 'Castro' --I have assumed the line break mid name is meant to be there
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%cordova%' THEN 'Adrian ' + CHAR(10) + 'Cordova'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%lyndsay%' THEN 'Lyndsay ' + CHAR(10) + 'Frommer'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Lyndsay%' THEN 'Lyndsay ' + CHAR(10) + 'Frommer'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%rivera%' THEN 'Rivera ' + CHAR(10) + 'Margaret'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Margaret%' THEN 'Rivera ' + CHAR(10) + 'Margaret'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Jtroy%' THEN 'Jennifer ' + CHAR(10) + 'Troy'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Adrian C%' THEN 'Adrian ' + CHAR(10) + 'Cordova'
WHEN CAST(N.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Ann B%' THEN 'Ann ' + CHAR(10) + 'Burdge'
ELSE 'N/A'
END AS [Referal Agent],
FROM MSG_MailHeader MH
JOIN MSG_MailDetail MD on MCON.MailHeader_ID = MTA.MailHeader_ID
JOIN ENTY_Person EP on MTA.Entity_ID = EP.Entity_ID
JOIN TASK_TaskAttachment TA on MCON.MailHeader_ID = TA.MailHeader_ID
JOIN View_Patient VP on TA.Patient_ID = VP.Patient_ID
JOIN CHRT_Visit CV on VP.Patient_ID = CV.Patient_ID
JOIN CHRT_VisitCPT CVC on CV.Note_ID = CVC.Note_ID
JOIN CHRT_OtherNote CON on TA.Patient_ID = CON.Patient_ID
JOIN CHRT_Note N on CON.Note_ID = N.Note_ID
CROSS APPLY (SELECT CASE WHEN CAST(ca.Note_SummaryRTF AS NVARCHAR(MAX)) LIKE '%Coordinator 1, ' + CHAR(10) + 'Referrals%' THEN 'Coordinator 1, Referrals' --Assumed same about line break here
ELSE 'Not A Referral Note'
END AS Author
FROM CHRT_Note ca) AS CNca
WHERE MTA.MailDetail_Folder = 3
AND MCON.MailHeader_DateSent BETWEEN '1/22/2018' AND '1/22/2018 23:59:59'
AND EP.Person_Name = 'Coordinator 1, Referrals'
AND CVC.VisitCPT_Code IN (...RemoveTA.. For example)
AND N.Note_DateOccurred > MCON.MailHeader_DateSent
AND CNca.Author = 'Coordinator 1, Referrals'
GROUP BY VP.Person_Name,
MCON.MailHeader_DateSent,
MTA.MailDetail_FolderP.Person_Name,
N.Note_DateOccurred,
MCON.MailHeader_Subject,
CAST(N.Note_SummaryRTF AS NVARCHAR(MAX));
注意,我已经更改了别名。使用别名时,请使用具有代表性的内容。调用表格a
,然后下一个b
,下一个c
等,对任何人都没有帮助。任何查看您的查询的人都必须查看您的WHERE
子句,以便在每次看到别名时找出别名(不只是一次扫描)。如果您有3个名为Customer
,Sale
和CustomerSale
的表,则分别为C
,S
和CS
添加别名,通过您的解决方案坚持这些别名。那些别名实际上意味着什么。
否则,Customer
在一个查询中可能是a
,因为它是您FROM
子句中的第一个表,但c
甚至是e
。 ,因为它是第3或第5。