Only one expression can be specified in the select list when the subquery is not introduced with EXISTS nested case statements

给你一囗甜甜゛ 提交于 2019-12-22 10:39:15

问题


I am trying to create a query that gets the number of hours an event was open below is my query. I am using case statements because it needs to take into account to only count weekdays. This is a step in the process my overall goal is to actually get the hours for those days. So for example if the the days is more than one count all those days and multiply by 8.. if its less than one do a datediff hours and just get the hours for that day.. Any help would be greatly appreciated!

but I am getting the following error:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

DECLARE @workdays int
SELECT creationDateTime, 
       closedDateTime,DATEDIFF(dd, creationDateTime, closedDateTime)+1,
       CASE WHEN (DATEDIFF(dd, creationDateTime, closedDateTime)+1 > 1)
            THEN (
                  SELECT creationDateTime ,closedDateTime, 
                    ((DATEDIFF(dd, creationDateTime, closedDateTime)+1)
                      -(DATEDIFF(wk, creationDateTime, closedDateTime) * 2)
                      -(CASE WHEN DATENAME(dw, creationDateTime)
                        = 'Sunday' THEN 1 ELSE 0 END)
                      -(CASE WHEN DATENAME(dw, closedDateTime) 
                        = 'Saturday' THEN 1 ELSE 0 END)
                  )*8 AS workdayhours
                  FROM table.ofevents where closedDateTime IS NOT NULL) END
FROM table.ofevents where closedDateTime IS NOT NULL

回答1:


As the error said, you cannot select more than one column into a value. When doing a select statement:

SELECT A, B, C ...

Each expression A, B, C, represents a column of data to be returned. That column is composed of values that have a data type like integer or double or varchar.

So when you do:

SELECT A, B, (SELECT C, D FROM ...) ...

You are saying "I want a column of As, a column of Bs, and a column of ..." Oops. You just broke SQL because it doesn't know how to put multiple values in a single cell like that. (Well, some SQL variants do know how to do that, but it is not always so simple.)

What you probably want is:

SELECT A, B, (SELECT C FROM ...), (SELECT D FROM ...) etc.

So, that means something like this:

declare @workdays int


 SELECT creationDateTime

      , closedDateTime

      , DATEDIFF(dd, creationDateTime, closedDateTime)+1

      , CASE WHEN (DATEDIFF(dd, creationDateTime, closedDateTime)+1 > 1)
          THEN 
           (SELECT creationDateTime
              as workdayhours
              from table.ofevents where closedDateTime IS NOT NULL)
          END

      , CASE WHEN (DATEDIFF(dd, creationDateTime, closedDateTime)+1 > 1)
          THEN 
           (SELECT closedDateTime
              as workdayhours
              from table.ofevents where closedDateTime IS NOT NULL)
          END

      , CASE WHEN (DATEDIFF(dd, creationDateTime, closedDateTime)+1 > 1)
          THEN ( (DATEDIFF(dd, creationDateTime, closedDateTime)+1 )
                -(DATEDIFF(wk, creationDateTime, closedDateTime) * 2)
                -(CASE WHEN DATENAME(dw, creationDateTime) = 'Sunday' THEN 1 ELSE 0 END)
                -(CASE WHEN DATENAME(dw, closedDateTime) = 'Saturday' THEN 1 ELSE 0 END))*8
              as workdayhours
              from table.ofevents where closedDateTime IS NOT NULL)
          END

   FROM table.ofevents where closedDateTime IS NOT NULL

See how each subquery only selects a single value?

But, I still do not see why you need to select the "creationDateTime" and "closeDateTime", so you can probably remove those columns.

One last piece of advice, because I see this often in people's SQL related questions. Ask yourself, really deeply, really thoroughly, why you want SQL to do something. Why did you type:

   (SELECT creationDateTime ,closedDateTime, 
     ( (DATEDIFF(dd, creationDateTime, closedDateTime)+1 )

Why is that creationDateTime there right before closeDateTime, what does it mean? SQL is a declarative language, you write down your intent and it is the job of the database to produce the data that matches your intent. This is unlike most programming languages which are imperative, like C. In C, you write down how to do something. This makes understanding SQL very difficult for people who mostly understand imperative languages. So, ask yourself, why did you create that subquery, what is your goal?

If my answer does not suffice, please edit your question to clarify what you want your result query to look like. That's very important. If you don't tell us what you expect the query to return, how can you expect to tell the database that, or expect us to tell you how to tell the database that?




回答2:


I think you can just get rid of a sub-query and everything else will work:

declare @workdays int


SELECT creationDateTime ,closedDateTime,DATEDIFF(dd, creationDateTime, closedDateTime)+1,

CASE WHEN (DATEDIFF(dd, creationDateTime, closedDateTime)+1 > 1)

THEN 

  (( (DATEDIFF(dd, creationDateTime, closedDateTime)+1 )
  -(DATEDIFF(wk, creationDateTime, closedDateTime) * 2)
  -(CASE WHEN DATENAME(dw, creationDateTime) = 'Sunday' THEN 1 ELSE 0 END)
  -(CASE WHEN DATENAME(dw, closedDateTime) = 'Saturday' THEN 1 ELSE 0 END))*8

  as workdayhours

END


FROM table.ofevents where closedDateTime IS NOT NULL

I left your work-day-hours logic "as is".



来源:https://stackoverflow.com/questions/17978814/only-one-expression-can-be-specified-in-the-select-list-when-the-subquery-is-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!