2017-08-09 92 views
0

我有以下查詢:SQL重命名行結果

SELECT * FROM [dbo].[SellThruDaily852] WHERE RetailerID='AMAZON' --order by StartDate 
     AND StartDate in (20170731,20170801,20170802,20170803,20170804,20170805,20170806); 
WITH AMAZON AS 
    (
    SELECT 
     MAX(ProcessRunId) ProccessID, 
     DATEPART(YY,DATEADD(DD,-1,CAST(CreatedDate AS DATE))) VizioYear , 
     DATEPART(WK,DATEADD(DD,-1,CAST(CreatedDate AS DATE))) WeekID, 
     CAST(DATENAME(dw,CAST(CreatedDate AS DATE)) AS VARCHAR) DayName, 
     FileName, 
     CAST(CreatedDate AS DATE) CreateDate 
     --CreatedDate 
    FROM [dbo].[FileCheckResult] 
    WHERE 
     RetailerID='AMAZON' 
     AND FileCheckType='Received' 
     AND FileDataType='SellThru' 
     AND FileName like '852_AMAZON_9%' 
    GROUP BY FileName,CAST(CreatedDate AS DATE) 
    ) 
SELECT 
    * 
FROM AMAZON 
WHERE 
    VizioYear=2017 
    AND WeekID=31 
ORDER BY CreateDate 

這是結果:

enter image description here

我想要做的是什麼DAYNAME前兩行更改爲 - 週一和週二。

任何想法?

回答

1
--YOUR PREVIOUS CODE-- 
--AFTER CTE 
SELECT 
     ProccessID, 
     VizioYear , 
     WeekID, 
     CASE WHEN rownumber in (1,2) THEN 'Monday and Tuesday' ELSE DayName END AS DayName , 
     FileName, 
     CreateDate 
FROM 
(
SELECT 
    *, ROW_NUMBER()OVER(ORDER BY CreateDate) as rownumber 
FROM AMAZON 
WHERE 
    VizioYear=2017 
    AND WeekID=31 
) as ABC 
ORDER BY ABC.CreateDate 
0

這裏發生的是,你有3個文件的單個日期,它當然會返回相同的DayName(DATENAME(Date))。

理想的解決方案是將2017-08-02到2017-07-31 2017-08-01的CreateDate更新爲突出顯示的兩行,並以此方式獲得所需的結果。

如果您不能修改數據,並希望這樣做的查詢,您可以添加一個CASE得到你想要的結果:

CASE 
WHEN FileName == 'FileMonday.csv' THEN 'Monday' 
WHEN FileName == 'FileTuesday.csv' THEN 'Tuesday' 
ELSE 
CAST(DATENAME(dw,CAST(CreatedDate AS DATE)) AS VARCHAR) 
END AS DayName,