使用两个可以返回多个值的子查询的 SQL 之间的语句
Posted
技术标签:
【中文标题】使用两个可以返回多个值的子查询的 SQL 之间的语句【英文标题】:SQL Between Statement using two Sub-queries that could return multiple values 【发布时间】:2013-05-08 20:27:22 【问题描述】:目的:根据分配给用户的角色对工作列表执行 alpha 拆分。如果用户只有一个 alpha 拆分(例如 A-CZZZ),则以下逻辑有效,但我需要一些用户具有多个拆分的灵活性(例如 A-CZZZ 和 T-ZZZZ)。
高级:
SELECT Name
FROM PatientDatabase
WHERE Name Between
(SELECT UserRole.AlphaFrom
FROM UserRole
WHERE UserRole.HasRole = 1)
AND
(SELECT UserRole.AlphaThru
FROM UserRole
WHERE UserRole.HasRole = 1)
PatientDatabase 中的潜在名称:
ABC,PERSON
LMN,PERSON
XYZ,PERSON
当前用户有两个 UserRoles(alpha 拆分):
A-CZZZ
T-ZZZZ
我想回来:
ABC,PERSON
XYZ,PERSON
鉴于以下与安全相关的逻辑,我认为子查询可能是必要的。
信息:
-'Roledef->ReportingCategory1->Name' is the AlphaFrom
-'Roledef->ReportingCategory2->Name' is the AlphaThru
当前的 alpha 分割逻辑仅在一个时起作用(如果超过一个,则在第一次分割时起作用):
AND T.PatId->PatNm BETWEEN
(SELECT E2.Roledef->ReportingCategory1->Name
FROM SECURITYPLUS.USR S2, SecurityPlus.UsrETMRole E2,
SecurityPlus.UsrETMApplication A2
WHERE A2.USR = S2.USERNAME AND
E2.USRETMAPPLICATION = A2.ID AND S2.Username = ?
AND E2.Roledef->Name ['Alpha')
AND
(SELECT E3.Roledef->ReportingCategory2->Name
FROM SECURITYPLUS.USR S3, SecurityPlus.UsrETMRole E3,
SecurityPlus.UsrETMApplication A3
WHERE A3.USR = S3.USERNAME AND
E3.USRETMAPPLICATION = A3.ID AND S3.Username = ?
AND E3.Roledef->Name ['Alpha')
上下文的整个 SQL 逻辑:
SELECT
T.Id,
T.PatId->Mrn,
T.PatId->PatNm,
T.Invoice,
T.Invoice->Fsc,
T.Stage->Name As Stage,
T.Status->Name As Status,
T.Invoice->InvBal,
T.ReviewDate,
T.HasNoteFlag
FROM
TaskManager.Task T
WHERE
T.TaskNm->Name = 'Insurance Followup'
AND T.Status IN (SELECT ID FROM Dict.ETMTaskStatus TS
WHERE StatusType NOT IN ('DELETED','DONE'))
AND T.Invoice->Fsc->EtmRole IN
(SELECT E1.Roledef
FROM SECURITYPLUS.USR S1, SecurityPlus.UsrETMRole E1,
SecurityPlus.UsrETMApplication A1
WHERE A1.USR = S1.USERNAME AND
E1.USRETMAPPLICATION = A1.ID AND S1.Username = ?)
AND T.PatId->PatNm BETWEEN
(SELECT E2.Roledef->ReportingCategory1->Name
FROM SECURITYPLUS.USR S2, SecurityPlus.UsrETMRole E2,
SecurityPlus.UsrETMApplication A2
WHERE A2.USR = S2.USERNAME AND
E2.USRETMAPPLICATION = A2.ID AND S2.Username = ?
AND E2.Roledef->Name ['Alpha')
AND
(SELECT E3.Roledef->ReportingCategory2->Name
FROM SECURITYPLUS.USR S3, SecurityPlus.UsrETMRole E3,
SecurityPlus.UsrETMApplication A3
WHERE A3.USR = S3.USERNAME AND
E3.USRETMAPPLICATION = A3.ID AND S3.Username = ?
AND E3.Roledef->Name ['Alpha')
下一个逻辑返回我需要的表,但我不知道如何获得一个 between 语句来使用它:
SELECT E2.Roledef->ReportingCategory1->Name As AlphaFrom,
E2.Roledef->ReportingCategory2->Name As AlphaThru
FROM SECURITYPLUS.USR S2, SecurityPlus.UsrETMRole E2,
SecurityPlus.UsrETMApplication A2
WHERE A2.USR = S2.USERNAME AND
E2.USRETMAPPLICATION = A2.ID AND S2.Username = ?
AND E2.Roledef->Name ['Alpha'
返回:
AlphaFrom AlphaThru
A CZZZ
T ZZZZ
我怀疑我必须放弃子查询才能完成此操作,因为加入它们的尝试失败了,但我不确定如何继续。
【问题讨论】:
。 .复杂的 SQL 代码很难理解。您能否提供一些输入数据和期望结果的示例? @ypercube - 它基于 Intersystems Cache 版本 5.something 并在计费系统中使用。 “->” 钻到其他表。 @GordonLinoff - 我试图在帖子顶部添加一个高级示例。它不一定有意义,但可以更清楚地说明我想做什么。 【参考方案1】:我最终的答案是在我们的角色字典中创建一个自定义字段,然后为字母表中的每个字母创建一个角色,并使用“IN”语句而不是“BETWEEN”。
使用高级示例:
SELECT Name
FROM PatientDatabase
WHERE LEFT(Name,1) In
(SELECT UserRole.Alpha
FROM UserRole
WHERE UserRole.HasRole = 1)
【讨论】:
以上是关于使用两个可以返回多个值的子查询的 SQL 之间的语句的主要内容,如果未能解决你的问题,请参考以下文章