我需要在SQL中的表中添加一个新列

时间:2014-06-19 15:36:52

标签: sql

我有一张包含以下数据的表格

Account Name tripdate

123     sam  02/20/13

234     sim  02/20/14

我的查询是

SELECT *, 

(CASE 
    WHEN (DATENAME(dw, [tripdate]) ='Sunday') THEN 1
    ELSE 0
END) AS [Sunday],

(CASE 
    WHEN (DATENAME(dw, [tripdate]) ='Monday') THEN 1
    ELSE 0
END) AS [Monday],

(CASE 
    WHEN (DATENAME(dw, [tripdate]) ='Tuesday') THEN 1
    ELSE 0
END) AS [Tuesday],

(CASE 
    WHEN (DATENAME(dw, [Starting Date of Trip]) ='Wednesday') THEN 1
    ELSE 0
END) AS [Wednesday],

(CASE 
    WHEN (DATENAME(dw, [Starting Date of Trip]) ='Thursday') THEN 1
    ELSE 0
END) AS [Thursday],

(CASE 
    WHEN (DATENAME(dw, [tripdate]) ='Friday') THEN 1
    ELSE 0
END) AS [Friday],

(CASE 
    WHEN (DATENAME(dw, [tripdate]) ='Saturday') THEN 1
    ELSE 0
END) AS [Saturday]
FROM [Table]   GROUP BY [ Account]

,输出

Account Name tripdate sunday monday tuesday  Dayname

123     sam  02/20/13  1       0       0     sunday

234     sim  02/20/14  0       0       1     tuesday

现在我需要帮助才能获得最后一列的日期名称。

1 个答案:

答案 0 :(得分:0)

只需使用....

DATENAME(dw, [tripdate]) as dayname