我正在修改现有的SQL查询。问题是我想从表中添加描述到现有数据。我添加了一个子查询,如图所示,但它似乎改变了我的返回数据集。 anyonple请建议如何使用子查询获取正确的数据?
SELECT TOP 100
ROW_NUMBER() OVER ( ORDER BY LTRIM(VW_POLINDEX.SYMBOL) ASC, LTRIM(VW_POLINDEX.POLNUM) ASC, LTRIM(VW_POLINDEX.MODULE) ASC ) AS RowNum
,VW_POLINDEX.POLNUM
,VW_POLINDEX.SYMBOL
,VW_POLINDEX.MODULE
,
--Subquery: 1 This subquery will fetch the description based on the pol details
( SELECT TOP 1
reasonamendedCd
FROM SHPolicyAmendmentHistory SH
WHERE SH.PolicyNbr = VW_POLINDEX.POLNUM
AND SH.PolicySymbolCd = VW_POLINDEX.SYMBOL
AND SH.PolicyModuleNbr = Module
ORDER BY amendmentnbr DESC
) AS POLSECRIPTION
,VW_POLINDEX.CANCELLD
FROM VW_POLINDEX WITH ( NOLOCK )
INNER JOIN LINE_OF_BUS_LIT WITH ( NOLOCK ) ON VW_POLINDEX.LOB = LINE_OF_BUS_LIT.LOB_CD
INNER JOIN POL_STATUS_LIT WITH ( NOLOCK ) ON VW_POLINDEX.TYPEACT = POL_STATUS_LIT.POL_STATUS_CD
WHERE ( CASE VW_POLINDEX.MODULE
WHEN 0 THEN VW_POLINDEX.SYMBOL + '0'
ELSE VW_POLINDEX.SYMBOL + '1'
END ) NOT IN ( 'RJD1', 'RJH1' )
AND VW_POLINDEX.LOB = 'HP'
当前输出:
+------+-------+------+------+-------------+---------+
|RowNum|POLNUM |SYMBOL|MODULE|POLSECRIPTION|CANCELLD |
+------+-------+------+------+-------------+---------+
|3 |0040026|CCS |2 |<NULL> |9/12/2006|
+------+-------+------+------+-------------+---------+
|4 |0040026|CCS |3 |<NULL> |9/12/2010|
+------+-------+------+------+-------------+---------+
|2 |0040026|CCS |1 |<NULL> |9/12/2006|
+------+-------+------+------+-------------+---------+
|7 |2000010|CCS |2 |XM |1/1/1753 |
+------+-------+------+------+-------------+---------+
|8 |2000010|CCS |3 |CPP |1/1/1753 |
+------+-------+------+------+-------------+---------+
|12 |2000025|CCS |0 |RN |1/1/1753 |
+------+-------+------+------+-------------+---------+
|16 |2000025|CCS |4 |RN |1/1/1753 |
+------+-------+------+------+-------------+---------+
|6 |2000010|CCS |1 |IC |1/1/1753 |
+------+-------+------+------+-------------+---------+
|20 |2000029|CCS |1 |RN |1/1/1753 |
+------+-------+------+------+-------------+---------+
|10 |2000010|CCS |5 |CM |1/1/1753 |
+------+-------+------+------+-------------+---------+
|11 |2000010|CCS |6 |Z |7/28/2013|
+------+-------+------+------+-------------+---------+
|24 |2000029|CCS |5 |RN |1/1/1753 |
+------+-------+------+------+-------------+---------+
|14 |2000025|CCS |2 |RN |1/1/1753 |
+------+-------+------+------+-------------+---------+
预期输出 (在prev输出中缺少某些行,并且在添加子查询后也没有排序):
+------+-------+------+------+-------------+---------+
|RowNum|POLNUM |SYMBOL|MODULE|POLSECRIPTION|CANCELLD |
+------+-------+------+------+-------------+---------+
|1 |0040026|CCS |0 |test |9/12/2006|
+------+-------+------+------+-------------+---------+
|2 |0040026|CCS |1 |test |9/12/2006|
+------+-------+------+------+-------------+---------+
|3 |0040026|CCS |2 |test |9/12/2006|
+------+-------+------+------+-------------+---------+
|4 |0040026|CCS |3 |test |9/12/2010|
+------+-------+------+------+-------------+---------+
|5 |2000010|CCS |0 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|6 |2000010|CCS |1 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|7 |2000010|CCS |2 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|8 |2000010|CCS |3 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|9 |2000010|CCS |4 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|10 |2000010|CCS |5 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|11 |2000010|CCS |6 |test |7/28/2013|
+------+-------+------+------+-------------+---------+
|12 |2000025|CCS |0 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|13 |2000025|CCS |1 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|14 |2000025|CCS |2 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|15 |2000025|CCS |3 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
|16 |2000025|CCS |4 |test |1/1/1753 |
+------+-------+------+------+-------------+---------+
答案 0 :(得分:3)
为什么不在INNER JOIN中使用该描述表? LIKE
SELECT TOP 100
ROW_NUMBER() OVER ( ORDER BY LTRIM(VW_POLINDEX.SYMBOL) ASC, LTRIM(VW_POLINDEX.POLNUM) ASC, LTRIM(VW_POLINDEX.MODULE) ASC ) AS RowNum
,VW_POLINDEX.POLNUM
,VW_POLINDEX.SYMBOL
,VW_POLINDEX.MODULE
,VW_POLINDEX.CANCELLD
,SH.reasonamendedCd
FROM VW_POLINDEX WITH ( NOLOCK )
INNER JOIN LINE_OF_BUS_LIT WITH ( NOLOCK ) ON VW_POLINDEX.LOB = LINE_OF_BUS_LIT.LOB_CD
INNER JOIN POL_STATUS_LIT WITH ( NOLOCK ) ON VW_POLINDEX.TYPEACT = POL_STATUS_LIT.POL_STATUS_CD
INNER JOIN SHPolicyAmendmentHistory SH ON SH.PolicyNbr = VW_POLINDEX.POLNUM
AND SH.PolicySymbolCd = VW_POLINDEX.SYMBOL
AND SH.PolicyModuleNbr = Module
WHERE ( CASE VW_POLINDEX.MODULE
WHEN 0 THEN VW_POLINDEX.SYMBOL + '0'
ELSE VW_POLINDEX.SYMBOL + '1'
END ) NOT IN ( 'RJD1', 'RJH1' )
AND VW_POLINDEX.LOB = 'HP'