我在查询中有这个地址:
CASE
WHEN olnn.olnnNote like '%SEE DRAWING%'
THEN 'Y:\300 ORDER PROCESSING\Majid Ahmadi\Custom Cabs\' + 'CUSTOM' + '_' + CAST (olnav.olnavValue as nvarchar(25)) + '.jpg' --Added 2018-05-23 by MA
ELSE Null
END AS imageFilePath
由于olnn.olnnNote中的重复单元格,导致重复的文件路径。我尝试使用SELECT DISTINCT CASE但我仍然得到了相同的结果。
任何人都知道我的错误?
谢谢
答案 0 :(得分:0)
首先,您永远不需要在CASE语句中包含ELSE NULL,因为在没有条件时NULL是默认值。
要获得所需的结果,您可以使用ROW_NUMBER,PARTITION BY您不想复制的行,然后在ROW_NUMBER = 1时返回文件路径。请注意以下示例数据和解决方案:
-- Sample data
DECLARE @olnn TABLE (olnavValue varchar(20), olnnNote varchar(1000));
INSERT @olnn (olnavValue, olnnNote) VALUES
('abc', 'blah blah SEE DRAWING'),
('abc', 'blah SEE DRAWING blah blah'),
('xxx', 'blah blah blah blah... SEE DRAWING') ,
('yyy', 'SEE DRAWING Please'),
('zzz', 'blah blah blah blah... SEE DRAWING'),
('zzz', 'yada yada... SEE DRAWING'),
('zzz', 'SEE DRAWING');
-- Solution
WITH olnn AS
(
SELECT
olnn.olnavValue,
olnn.olnnNote,
rn = ROW_NUMBER() OVER (PARTITION BY olnn.olnavValue ORDER BY (SELECT NULL))
FROM @olnn olnn
)
SELECT
olnn.olnavValue,
FilePath =
CASE WHEN olnn.olnnNote LIKE '%SEE DRAWING%' AND olnn.rn = 1
THEN 'Y:\300 ORDER PROCESSING\Majid Ahmadi\Custom Cabs\' + 'CUSTOM' + '_' +
CAST(olnn.olnavValue as nvarchar(25)) + '.jpg'
END
FROM olnn;
<强>结果
olnavValue FilePath
-------------------- -------------------------------------------------------------------------------------
abc Y:\300 ORDER PROCESSING\Majid Ahmadi\Custom Cabs\CUSTOM_abc.jpg
abc NULL
xxx Y:\300 ORDER PROCESSING\Majid Ahmadi\Custom Cabs\CUSTOM_xxx.jpg
yyy Y:\300 ORDER PROCESSING\Majid Ahmadi\Custom Cabs\CUSTOM_yyy.jpg
zzz Y:\300 ORDER PROCESSING\Majid Ahmadi\Custom Cabs\CUSTOM_zzz.jpg
zzz NULL
zzz NULL
请注意,当你没有指定olnnNote不喜欢的时候你会看到什么&#39;%SEE DRAWING%&#39;。