子查询只能 return 1 列
Subquery can return 1 column only
此查询的目的是根据今天的日期是工作日还是周末进行计算,并在 Union All 之后使用它,但是我得到
Msg 116 error:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
看起来我只能 return 1 列:
我的查询:
SELECT (SELECT CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday')
THEN
(SELECT --WeekEND
'X' AS Table_name
,CAST(MAX(date_1) as date) AS max_date
,DATEDIFF(DD,(CAST(MAX(date_1)as date)),GETDATE()) as NrOfDays
,CASE
WHEN DATEDIFF(DD,(CAST(MAX(date_1)as date)),GETDATE()) <= 3 THEN 'good'
ELSE 'bad'
END AS Status
FROM [Table_1])
ELSE
(SELECT --WeekDAY
'X' AS Table_name
,CAST(MAX(date_1) as date) AS max_date
,DATEDIFF(DD,(CAST(MAX(date_1)as date)),GETDATE()) as NrOfDays
,CASE
WHEN DATEDIFF(DD,(CAST(MAX(date_1)as date)),GETDATE()) <= 1 THEN 'good'
ELSE 'bad'
END AS Status
FROM [Table_1])
END
)
同样的问题与以下只是一个简化版本(如果我删除 'good'、'bad' 部分它有效):
SELECT (SELECT CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
(SELECT 'Weekend', 'good')
ELSE
(SELECT 'Weekday', 'bad')
END
)
您正在尝试 return 将子查询中的两列合并到主查询的单列中。
相反,您需要创建两个(非常相似的)子查询。一个用于主查询中的每一列。
SELECT(
SELECT(
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
(SELECT 'Weekend')
ELSE
(SELECT 'Weekday')
END AS Column1,
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
(SELECT 'good')
ELSE
(SELECT 'bad')
END AS Column2
)
)
根据您的预期输出,还有其他(更好的)方法可以做到这一点。不过这个大概是easiest/simplest理解的。
此外,这并不重要,但您的查询中有很多您并不真正需要的 SELECT
。上面可以简化为:
SELECT
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
'Weekend'
ELSE
'Weekday'
END AS Column1,
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
'good'
ELSE
'bad'
END AS Column1
我不知道你的“状态”栏到底在做什么,所以我不能为你安全地优化它。但我认为这应该可以满足您的需求:
SELECT
'X' AS Table_name,
CAST(MAX(date_1) as date) AS max_date,
DATEDIFF(DD,(CAST(MAX(date_1)as date)), GETDATE()) as NrOfDays,
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
CASE WHEN DATEDIFF(DD,(CAST(MAX(date_1)as date)), GETDATE()) <= 3 THEN
'good'
ELSE
'bad'
END
ELSE
CASE WHEN DATEDIFF(DD,(CAST(MAX(date_1)as date)), GETDATE()) <= 1 THEN
'good'
ELSE
'bad'
END
END AS Status
FROM [Table_1]
此查询的目的是根据今天的日期是工作日还是周末进行计算,并在 Union All 之后使用它,但是我得到
Msg 116 error:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
看起来我只能 return 1 列:
我的查询:
SELECT (SELECT CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday')
THEN
(SELECT --WeekEND
'X' AS Table_name
,CAST(MAX(date_1) as date) AS max_date
,DATEDIFF(DD,(CAST(MAX(date_1)as date)),GETDATE()) as NrOfDays
,CASE
WHEN DATEDIFF(DD,(CAST(MAX(date_1)as date)),GETDATE()) <= 3 THEN 'good'
ELSE 'bad'
END AS Status
FROM [Table_1])
ELSE
(SELECT --WeekDAY
'X' AS Table_name
,CAST(MAX(date_1) as date) AS max_date
,DATEDIFF(DD,(CAST(MAX(date_1)as date)),GETDATE()) as NrOfDays
,CASE
WHEN DATEDIFF(DD,(CAST(MAX(date_1)as date)),GETDATE()) <= 1 THEN 'good'
ELSE 'bad'
END AS Status
FROM [Table_1])
END
)
同样的问题与以下只是一个简化版本(如果我删除 'good'、'bad' 部分它有效):
SELECT (SELECT CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
(SELECT 'Weekend', 'good')
ELSE
(SELECT 'Weekday', 'bad')
END
)
您正在尝试 return 将子查询中的两列合并到主查询的单列中。
相反,您需要创建两个(非常相似的)子查询。一个用于主查询中的每一列。
SELECT(
SELECT(
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
(SELECT 'Weekend')
ELSE
(SELECT 'Weekday')
END AS Column1,
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
(SELECT 'good')
ELSE
(SELECT 'bad')
END AS Column2
)
)
根据您的预期输出,还有其他(更好的)方法可以做到这一点。不过这个大概是easiest/simplest理解的。
此外,这并不重要,但您的查询中有很多您并不真正需要的 SELECT
。上面可以简化为:
SELECT
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
'Weekend'
ELSE
'Weekday'
END AS Column1,
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
'good'
ELSE
'bad'
END AS Column1
我不知道你的“状态”栏到底在做什么,所以我不能为你安全地优化它。但我认为这应该可以满足您的需求:
SELECT
'X' AS Table_name,
CAST(MAX(date_1) as date) AS max_date,
DATEDIFF(DD,(CAST(MAX(date_1)as date)), GETDATE()) as NrOfDays,
CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN
CASE WHEN DATEDIFF(DD,(CAST(MAX(date_1)as date)), GETDATE()) <= 3 THEN
'good'
ELSE
'bad'
END
ELSE
CASE WHEN DATEDIFF(DD,(CAST(MAX(date_1)as date)), GETDATE()) <= 1 THEN
'good'
ELSE
'bad'
END
END AS Status
FROM [Table_1]