1
如果組中的任何值爲0,我想掃描表並將組內的值重新賦值爲0。通過搜索Group By
,Partition
和Any
的各種組合來找出解決方案。當組中的任何值爲0時,將組內的所有值設置爲0
我開始看起來像
CREATE TABLE #QP
(
[Method] VARCHAR(1),
[Station] VARCHAR(1),
[Instrument] VARCHAR(20),
[LastAnalysis] DATE,
[DaysPassed] INT
)
INSERT INTO #QP
(Method, Station, Instrument, LastAnalysis, DaysPassed)
VALUES
('A', 1, 'Polaris', '2016-07-19', 21),
('B', 1, 'Polaris', '2016-08-04', 5),
('C', 1, 'Polaris', '2016-07-31', 9),
('A', 2, 'Polaris', '2016-07-31', 9),
('B', 2, 'Polaris', '2016-08-09', 0),
('C', 2, 'Polaris', '2016-07-23', 17),
('A', 3, 'Polaris', '2016-08-09', 0),
('B', 3, 'Polaris', '2016-07-27', 13),
('C', 3, 'Polaris', '2016-07-19', 21)
而且我想結果顯示爲數據(包括爲便於解釋換行符)
Method Station Instrument LastAnalysis DaysPassed Weight
A 1 Polaris 2016-07-19 21 21
B 1 Polaris 2016-08-04 5 5
C 1 Polaris 2016-07-31 9 6
A 2 Polaris 2016-07-31 9 0
B 2 Polaris 2016-08-09 0 0
C 2 Polaris 2016-07-23 17 0
A 3 Polaris 2016-08-09 0 0
B 3 Polaris 2016-07-27 13 0
C 3 Polaris 2016-07-19 21 0
我已經得到的最接近的到目前爲止使用的是
SELECT *,
CASE WHEN 0 = ANY(SELECT DaysPassed FROM #QP) THEN 0 ELSE DaysPassed END AS [Weight]
FROM #QP
WHERE Instrument = 'Polaris'
ORDER BY Station, Method
但是這設定了每個值在Weight
co lumn爲0,Station
組中的值應保持原樣。
如果這有一個答案,我很想知道正確的搜索條件來找到它。