我正在为一个精选案例尝试SQL语句。 'when'有一个转换日期。这是在抛出错误。我错过了什么?
SELECT DISTINCT
'detail', n.UserName, n.StatusCodeDesc,
CASE
WHEN n.enddate IS NULL AND (n.statuscode = 'L' or n.statuscode = 'P')
THEN (CONVERT(VARCHAR(10), getdate(), 101) AS [MM/DD/YYYY])
ELSE (CONVERT(VARCHAR(10), n.EndDate, 101) AS [MM/DD/YYYY])
END,
n.TC_OBT_STAT
FROM
tbl_ActNightlyFeed n
INNER JOIN
Tbl_ActExistingFeed g ON n.Empid = g.Empid
答案 0 :(得分:4)
列别名应该只出现在case表达式的结尾之后,如下所示:
SELECT DISTINCT
'detail',
n.UserName,
n.StatusCodeDesc,
CASE
WHEN n.enddate IS NULL AND (n.statuscode = 'L' OR n.statuscode = 'P')
THEN CONVERT(VARCHAR(10), getdate(), 101)
ELSE CONVERT(VARCHAR(10), n.EndDate, 101)
END AS [MM/DD/YYYY],
n.TC_OBT_STAT
FROM tbl_ActNightlyFeed n
JOIN Tbl_ActExistingFeed g ON n.Empid = g.Empid
答案 1 :(得分:0)
您根本没有选择g
表。我建议写这个:
SELECT 'detail', n.UserName, n.StatusCodeDesc,
(CASE WHEN n.enddate IS NULL AND n.statuscode IN ('L', 'P')
THEN CONVERT(VARCHAR(10), getdate(), 101)
ELSE CONVERT(VARCHAR(10), n.EndDate, 101)
END) AS [MM/DD/YYYY],
n.TC_OBT_STAT
FROM tbl_ActNightlyFeed n
WHERE EXISTS (SELECT 1
FROM Tbl_ActExistingFeed g
WHERE n.Empid = g.Empid
);
如果夜间供稿表可能导致重复,您仍需要select distinct
。我猜这个表不会有重复。