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
组中的值应保持原样。
如果这有一个答案,我很想知道正确的搜索条件来找到它。