Msg 116,Level 16,State 1

时间:2016-04-20 07:06:58

标签: sql asp.net sql-server sql-server-2008 stored-procedures

请帮我解决此问题

  

Msg 116,Level 16,State 1,Procedure openclosebycat,第18行
  子查询时,只能在选择列表中指定一个表达式   没有引入EXISTS。

     

Msg 116,Level 16,State 1,Procedure openclosebycat,第20行
  当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。

我的代码:

select distinct
    (SELECT COUNT(*) 
     FROM dbo.PROBS 
     WHERE (STATUS LIKE 'closed')
       AND (OPEN_TIME >= @DateFrom)
       AND (CLOSE_TIME <= @DateTo)) AS closen,
   (SELECT NUMBER, STATUS, ID 
    FROM dbo.PROBS 
    WHERE (STATUS LIKE 'closed')
      AND (OPEN_TIME >= @DateFrom)
      AND (CLOSE_TIME <= @DateTo)) AS closedet,
  (SELECT NUMBER, STATUS, ID 
   FROM dbo.PROBS 
   WHERE (STATUS LIKE 'open')
     AND (OPEN_TIME >= @DateFrom)
     AND (CLOSE_TIME <= @DateTo)) AS opendet,
  (SELECT COUNT(*)  
   FROM dbo.PROBS
   WHERE (STATUS = 'closed') 
     AND (CATEGORY = 'problem') 
     AND (OPEN_TIME >= @DateFrom)
     AND (CLOSE_TIME <= @DateTo)) AS closeporb,
 (SELECT COUNT(*) 
  FROM dbo.PROBS
  WHERE (STATUS = 'closed') 
    AND (CATEGORY = 'request for information') 
    AND (OPEN_TIME >= @DateFrom)
    AND (CLOSE_TIME <= @DateTo)) AS closereinfo,
 (SELECT COUNT(*)  
  FROM dbo.PROBS 
  WHERE (STATUS = 'closed') 
    AND (CATEGORY = 'incident') 
    AND (OPEN_TIME >= @DateFrom) 
    AND (CLOSE_TIME <= @DateTo)) AS closeincid,
 (SELECT COUNT(*)  
  FROM dbo.PROBS 
  WHERE (STATUS = 'closed') 
    AND (CATEGORY = 'complaint') 
    AND (OPEN_TIME >= @DateFrom) 
    AND (CLOSE_TIME <= @DateTo)) AS closecomp,
 (SELECT COUNT(*)  
  FROM dbo.PROBS 
  WHERE (STATUS = 'open') 
    AND (CATEGORY = 'problem') 
    AND (OPEN_TIME >= @DateFrom)
    AND (CLOSE_TIME <= @DateTo)) AS openporb,
 (SELECT COUNT(*)  
  FROM dbo.PROBS 
  WHERE (STATUS = 'open') 
    AND (CATEGORY = 'request for information') 
    AND (OPEN_TIME >= @DateFrom)
    AND (CLOSE_TIME <= @DateTo)) AS openreinfo,
 (SELECT COUNT(*)  
  FROM dbo.PROBS 
  WHERE (STATUS = 'open') 
    AND (CATEGORY = 'incident') 
    AND (OPEN_TIME >= @DateFrom)
    AND (CLOSE_TIME <= @DateTo)) AS openincid,
 (SELECT COUNT(*)  
  FROM dbo.PROBS 
  WHERE (STATUS = 'open') 
    AND (CATEGORY = 'complaint') 
    AND (OPEN_TIME >= @DateFrom) 
    AND (CLOSE_TIME <= @DateTo)) AS opencomp,
 (SELECT COUNT(*) 
  FROM dbo.PROBS 
  WHERE (STATUS LIKE 'open')
    AND (OPEN_TIME >= @DateFrom)
    AND (CLOSE_TIME <= @DateTo)) AS openn
FROM
    dbo.PROBS

1 个答案:

答案 0 :(得分:2)

SELECT中的子查询只能选择一个属性。我不确定你要做什么,但是你必须重写一下这样的话:

SELECT   NUMBER
,        STATUS
,        ID
,        SUM(CASE 
                  WHEN STATUS like 'closed' and OPEN_TIME >= @DateFrom and CLOSE_TIME <= @DateTo 
                     THEN 1 
                  ELSE 0 
             END) as closen
,        {same for the other counts}
FROM     dbo.PROBS
GROUP BY NUMBER
,        STATUS
,        ID