1
我在修改現有的SQL查詢。問題是我想從表中添加一個描述到現有數據。我添加了一個如圖所示的子查詢,但似乎改變了我的返回數據集。可以任何一個請建議如何使用子查詢獲取正確的數據?加入的SQL子查詢
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 |
+------+-------+------+------+-------------+---------+
預計輸出 (有些行缺少的分組輸出以及非加子查詢後排序):
+------+-------+------+------+-------------+---------+
|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 |
+------+-------+------+------+-------------+---------+
我刪除了MySQL標記,因爲語法是SQL Server。 –
輸出不會被排序,因爲您沒有對它進行排序,所以它可以以'SQL SERVER'希望的方式返回。添加一個'ORDER BY'子句。你真的確定你想在那裏有'NOLOCK'嗎? –
是的,輸出可以排序,但添加子查詢會給我不正確的數據,排序後也會丟失一些行。另外,我需要根據修訂日期從SHPolicyAmendmentHistory中獲得前1名,因此不確定加入是否會有所幫助,因爲它會帶來SHPolicyAmendmentHistory – user2814819