多个 table 加入操作中的问题
Issue in multiple table join operation
我有两个表 tbLicence 和 tbLicenceTypes.
tbLicence 有三个字段 AgentId LineOfAuthority, LicenceTypes
tbLicence 包含类似
的数据
AgentId LineOfAuthority LicenceTypes
......... .................... ................
287 Bail Bond,Casualty,Dental Services 1
287 Casualty,Industrial Fire,Life 4
287 Industrial Fire,Life 4
tbLicenceTypes 有两个字段 LicenceTypes, LicenceTypesX
tbLicenceTypes 包含类似
的数据
LicenceTypes LicenceTypesX
............ .............
1 Producer
2 Sales Representative
3 Insurance Agent
4 Title Agent
现在我想获取分配给 Casualty 的所有许可证类型。我的意思是 AgentId 是 287 并且授权线是伤亡......预期的输出应该是这样的
LicenceTypes LicenceTypesX
........... ...............
1 Producer
4 Title Agent
我试过下面的代码行...它不起作用
;WITH cte AS (
SELECT
AgentId,
CAST('<r>' + REPLACE(REPLACE(LineOfAuthority,'&','&'), ',', '</r> <r>') + '</r>' AS XML) AS LineOfAuthoritys
FROM tbLicence
)
,FinalList AS (
SELECT
AgentId,
RTRIM(LTRIM (PTable.PColumn.value('.', 'VARCHAR(MAX)'))) AS LineOfAuthoritys
FROM cte
CROSS APPLY LineOfAuthoritys.nodes('//r') AS PTable(PColumn)
)
SELECT DISTINCT
T.LicenceTypes AS LicenceTypes ,
F.LineOfAuthoritys AS LicenceTypesX
FROM FinalList F
CROSS APPLY (SELECT LicenceTypes FROM tbLicenceTypes TP WHERE TP.LicenceTypesX = f.LineOfAuthoritys) AS T
WHERE F.LineOfAuthoritys = @LineOfAuthority
AND F.AgentId = @AgentId
ORDER BY T ASC
请帮帮我!!!
LineOfAuthority 应使用附加表重新设计为多对多关系。完成后,查询将是一件容易的事。如果你不知道如何建立多对多关系,你最好先阅读一些关于 RDBMS 的好书。
样本TABLE
CREATE TABLE #tbLicence (AgentId INT, LineOfAuthority VARCHAR(200), LicenceTypes INT)
INSERT INTO #tbLicence
SELECT 287, 'Bail Bond,Casualty,Dental Services',1
UNION ALL
SELECT 287, 'Casualty,Industrial Fire,Life',4
UNION ALL
SELECT 287, 'Industrial Fire,Life',4
CREATE TABLE #tbLicenceTypes(LicenceTypes INT, LicenceTypesX VARCHAR(200))
INSERT INTO #tbLicenceTypes
SELECT 1, 'Producer'
UNION ALL
SELECT 2, 'Sales Representative'
UNION ALL
SELECT 3, 'Insurance Agent'
UNION ALL
SELECT 4, 'Title Agent'
查询
;WITH cte AS
(
SELECT
AgentId,LicenceTypes,
CAST('<r>' + REPLACE(REPLACE(LineOfAuthority,'&','&'), ',', '</r> <r>') + '</r>' AS XML) AS LineOfAuthoritys
FROM tbLicence
)
,FinalList AS (
SELECT
AgentId,LicenceTypes,
RTRIM(LTRIM (PTable.PColumn.value('.', 'VARCHAR(MAX)'))) AS LineOfAuthoritys
FROM cte
CROSS APPLY LineOfAuthoritys.nodes('//r') AS PTable(PColumn)
)
SELECT DISTINCT
L.LicenceTypes AS LicenceTypes ,
L.LicenceTypesX AS LicenceTypesX
FROM FinalList F
JOIN tbLicenceTypes L ON F.LicenceTypes=L.LicenceTypes
WHERE F.LineOfAuthoritys = 'Casualty'
AND F.AgentId = 287
ORDER BY L.LicenceTypesX ASC
- Click here 查看结果
我有两个表 tbLicence 和 tbLicenceTypes.
tbLicence 有三个字段 AgentId LineOfAuthority, LicenceTypes
tbLicence 包含类似
的数据AgentId LineOfAuthority LicenceTypes
......... .................... ................
287 Bail Bond,Casualty,Dental Services 1
287 Casualty,Industrial Fire,Life 4
287 Industrial Fire,Life 4
tbLicenceTypes 有两个字段 LicenceTypes, LicenceTypesX
tbLicenceTypes 包含类似
的数据 LicenceTypes LicenceTypesX
............ .............
1 Producer
2 Sales Representative
3 Insurance Agent
4 Title Agent
现在我想获取分配给 Casualty 的所有许可证类型。我的意思是 AgentId 是 287 并且授权线是伤亡......预期的输出应该是这样的
LicenceTypes LicenceTypesX
........... ...............
1 Producer
4 Title Agent
我试过下面的代码行...它不起作用
;WITH cte AS (
SELECT
AgentId,
CAST('<r>' + REPLACE(REPLACE(LineOfAuthority,'&','&'), ',', '</r> <r>') + '</r>' AS XML) AS LineOfAuthoritys
FROM tbLicence
)
,FinalList AS (
SELECT
AgentId,
RTRIM(LTRIM (PTable.PColumn.value('.', 'VARCHAR(MAX)'))) AS LineOfAuthoritys
FROM cte
CROSS APPLY LineOfAuthoritys.nodes('//r') AS PTable(PColumn)
)
SELECT DISTINCT
T.LicenceTypes AS LicenceTypes ,
F.LineOfAuthoritys AS LicenceTypesX
FROM FinalList F
CROSS APPLY (SELECT LicenceTypes FROM tbLicenceTypes TP WHERE TP.LicenceTypesX = f.LineOfAuthoritys) AS T
WHERE F.LineOfAuthoritys = @LineOfAuthority
AND F.AgentId = @AgentId
ORDER BY T ASC
请帮帮我!!!
LineOfAuthority 应使用附加表重新设计为多对多关系。完成后,查询将是一件容易的事。如果你不知道如何建立多对多关系,你最好先阅读一些关于 RDBMS 的好书。
样本TABLE
CREATE TABLE #tbLicence (AgentId INT, LineOfAuthority VARCHAR(200), LicenceTypes INT)
INSERT INTO #tbLicence
SELECT 287, 'Bail Bond,Casualty,Dental Services',1
UNION ALL
SELECT 287, 'Casualty,Industrial Fire,Life',4
UNION ALL
SELECT 287, 'Industrial Fire,Life',4
CREATE TABLE #tbLicenceTypes(LicenceTypes INT, LicenceTypesX VARCHAR(200))
INSERT INTO #tbLicenceTypes
SELECT 1, 'Producer'
UNION ALL
SELECT 2, 'Sales Representative'
UNION ALL
SELECT 3, 'Insurance Agent'
UNION ALL
SELECT 4, 'Title Agent'
查询
;WITH cte AS
(
SELECT
AgentId,LicenceTypes,
CAST('<r>' + REPLACE(REPLACE(LineOfAuthority,'&','&'), ',', '</r> <r>') + '</r>' AS XML) AS LineOfAuthoritys
FROM tbLicence
)
,FinalList AS (
SELECT
AgentId,LicenceTypes,
RTRIM(LTRIM (PTable.PColumn.value('.', 'VARCHAR(MAX)'))) AS LineOfAuthoritys
FROM cte
CROSS APPLY LineOfAuthoritys.nodes('//r') AS PTable(PColumn)
)
SELECT DISTINCT
L.LicenceTypes AS LicenceTypes ,
L.LicenceTypesX AS LicenceTypesX
FROM FinalList F
JOIN tbLicenceTypes L ON F.LicenceTypes=L.LicenceTypes
WHERE F.LineOfAuthoritys = 'Casualty'
AND F.AgentId = 287
ORDER BY L.LicenceTypesX ASC
- Click here 查看结果