帮助处理复杂的联接查询
请记住,我使用的是 SQL 2000,
我有两个表。
tblAutoPolicyList
包含一个名为 PolicyIDList
的字段。
tblLossClaims 包含两个名为LossPolicyID 和LossPolicyID 的字段。 政策审查
。
我正在编写一个存储过程,它将从 PolicyIDList
字段获取不同的 PolicyID
,并循环遍历 LossPolicyID
字段(如果找到匹配,则设置 <代码>PolicyReview 为“Y”)。
示例表布局:
PolicyIDList LossPolicyID
9651XVB19 5021WWA85, 4421WWA20, 3314WWA31, 1121WAW11, 2221WLL99 Y
5021WWA85 3326WAC35, 1221AXA10, 9863AAA44, 5541RTY33, 9651XVB19 Y
0151ZVB19 4004WMN63, 1001WGA42, 8587ABA56, 8541RWW12, 9329KKB08 N
我将如何编写存储过程(寻找逻辑而不是语法)?
请记住我使用的是 SQL 2000。
Keep in mind I am using SQL 2000
I have two tables.
tblAutoPolicyList
contains a field called PolicyIDList
.
tblLossClaims
contains two fields called LossPolicyID
& PolicyReview
.
I am writing a stored proc that will get the distinct PolicyID
from PolicyIDList
field, and loop through LossPolicyID
field (if match is found, set PolicyReview
to 'Y').
Sample table layout:
PolicyIDList LossPolicyID
9651XVB19 5021WWA85, 4421WWA20, 3314WWA31, 1121WAW11, 2221WLL99 Y
5021WWA85 3326WAC35, 1221AXA10, 9863AAA44, 5541RTY33, 9651XVB19 Y
0151ZVB19 4004WMN63, 1001WGA42, 8587ABA56, 8541RWW12, 9329KKB08 N
How would I go about writing the stored proc (looking for logic more than syntax)?
Keep in mind I am using SQL 2000.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
选择 LossPolicyID, * from tableName where charindex('PolicyID',LossPolicyID,1)>0
Select LossPolicyID, * from tableName where charindex('PolicyID',LossPolicyID,1)>0
基本上,想法是这样的:
'Unroll'
tblLossClaims
并返回两列:一个tblLossClaims
键(您没有提到任何内容,所以我猜它会为LossPolicyID
),Item
=LossPolicyID
中的单个项目。在
tblAutoPolicyList.PolicyIDList
中查找unrolled.Item
的匹配项。在
tblLossClaims.LossPolicyID
中查找不同matched.LossPolicyID
的匹配项。相应地更新
tblLossClaims.PolicyReview
。主要更新可能如下所示:
您可以利用固定项目宽度和固定列表格式,从而无需 UDF 即可轻松拆分
LossPolicyID
。我可以在数字表和 SUBSTRING() 的帮助下看到这一点。上面查询中的unrolling_join
实际上是tblLossClaims
与数字表连接。以下是
展开
“放大”的定义:master..spt_values
是一个系统表,此处用作数字表。过滤器v.type = 'P'
为我们提供了一个数字值从 0 到 2047 的行集,该行集范围缩小到从 1 到LossPolicyID
中的项目数的数字列表代码>.最终v.number
充当数组索引并用于剪切单个项目。@ItemLength 当然只是
LEN(tblAutoPolicyList.PolicyIDList)
。我可能还会声明@ItemLength2 = @ItemLength + 2
因此每次应用过滤器时都不会计算它。基本上就是这样了,如果我没有遗漏什么的话。
Basically, the idea is this:
'Unroll'
tblLossClaims
and return two columns: atblLossClaims
key (you didn't mention any, so I guess it's going to beLossPolicyID
) andItem
= a single item fromLossPolicyID
.Find matches of
unrolled.Item
intblAutoPolicyList.PolicyIDList
.Find matches of distinct
matched.LossPolicyID
intblLossClaims.LossPolicyID
.Update
tblLossClaims.PolicyReview
accordingly.The main UPDATE can look like this:
You can take advantage of the fixed item width and the fixed list format and thus easily split
LossPolicyID
without a UDF. I can see this done with the help of a number table andSUBSTRING()
.unrolling_join
in the above query is actuallytblLossClaims
joined with the number table.Here's the definition of
unrolled
'zoomed in':master..spt_values
is a system table that is used here as the number table. Filterv.type = 'P'
gives us a rowset with number values from 0 to 2047, which is narrowed down to the list of numbers from 1 to the number of items inLossPolicyID
. Eventuallyv.number
serves as an array index and is used to cut out single items.@ItemLength is of course simply
LEN(tblAutoPolicyList.PolicyIDList)
. I would probably also declared@ItemLength2 = @ItemLength + 2
so it wasn't calculated every time when applying the filter.Basically, that's it, if I haven't missed anything.
如果 PolicyIDList 字段是分隔列表,您必须首先分隔各个策略 ID,并创建一个包含所有结果的临时表。接下来,在 tblLossClaims 上使用 'where isn't (select * from #temptable tt where tt.PolicyID = LossPolicyID) 的更新查询。
根据表/数据的大小,您可能希望向临时表添加索引。
If the PolicyIDList field is a delimited list, you have to first separate the individual policy IDs and create a temporary table with all of the results. Next up, use an update query on the tblLossClaims with 'where exists (select * from #temptable tt where tt.PolicyID = LossPolicyID).
Depending on the size of the table/data, you might wish to add an index to your temporary table.