简体   繁体   English

子查询只能返回 1 列

[英]Subquery can return 1 column only

The purpose of this query would be to do a calculation based on if today's date is a weekday or a weekend and use it after with Union All, however I am getting此查询的目的是根据今天的日期是工作日还是周末进行计算,并在 Union All 之后使用它,但是我得到了

Msg 116 error:消息 116 错误:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.当不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式。

It looks like that I can return only 1 column with this:看起来我只能用这个返回 1 列:

My query:我的查询:

    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
)

Same issue with the following just a simplified version (if I delete the 'good', 'bad' part it works):以下只是一个简化版本的相同问题(如果我删除“好”、“坏”部分,它会起作用):

SELECT (SELECT CASE WHEN DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday') THEN 
(SELECT 'Weekend', 'good')
ELSE
(SELECT 'Weekday', 'bad')
END
)

You are trying to return two columns from your subquery, into a single column of your main query.您正在尝试将子查询中的两列返回到主查询的单列中。

Instead, you need to make two (very similar) subqueries.相反,您需要创建两个(非常相似的)子查询。 One for each column in your main query.一个用于主查询中的每一列。

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
    )
)

There are other (better) ways to do this, depending upon your expected output.根据您的预期输出,还有其他(更好的)方法可以做到这一点。 But this is probably the easiest/simplest to understand.但这可能是最容易/最容易理解的。

Also, not that it really matters but you have a lot of SELECT s in your query that you don't really need.此外,这并不重要,但您的查询中有很多您并不真正需要的SELECT The above can be simplified to:以上可以简化为:

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

I don't know what your "Status" column is really doing, so I can't optimise it safely for you.我不知道您的“状态”列到底在做什么,所以我无法为您安全地优化它。 But I think this should do what you want:但我认为这应该做你想要的:

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] 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有一种方法可以从子查询返回多列而不是为每一列重复子查询? 它有效但需要太长时间 - Is there a way that I can return multiple columns from a subquery rather than repeating the subquery for each column? It works but takes too long 子查询分组中的Summed列的SQL返回零 - SQL Return Zero For Summed Column In Subquery Grouping 子查询如何返回元组而不是值? - How can a subquery return a tuple instead of a value? SELECT Column值可以在子查询中使用吗? - can a SELECT Column value be used in a subquery? select 列表中不引入EXISTS 子查询时只能指定一个表达式。 (我使用子查询作为列表达式。) - Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. (I am Using sub query as column expression.) 列作为子查询 - Column as a subquery 如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式。 在子查询sqlserver中 - Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. in subquery sqlserver Max()如何才能创建子查询的子查询,以获取行的Count(),但仅显示最新的行? - How can I create a subquery, of a subquery, in order to get the Count() of rows, but only display the newest row?Max() SQL Server 子查询错误 - 当子查询不使用 EXISTS 引入时,选择列表中只能指定一个表达式 - SQL Server SubQuery Error - Only one expression can be specified in the select list when the subquery is not introduced with EXISTS 我可以将带有一行和一列的子查询视为标量吗? - Can I treat a subquery with one row and one column as a scalar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM