是否可以在select / group by语句中使用if子句?或另一种解决方案

时间:2011-03-22 14:25:09

标签: sql sql-server-2008

使用SQLServer 2008

我目前有一个实现如下所示功能的函数(尽管函数中有变量我只是在这里填充它们以更好地了解输入可能是什么样的。)

我想知道是否有一种方法可以将@id定义为null(当前在v.ID上的Coalesce中使用)以匹配所有内容,就好像您没有在select / group中定义它一样条款?

        SELECT
            'ALL' as GeogType,
            CAST(v.AdmissionOn as date) as dtAdmission,
            (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) as timeInterval,
            CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) as nvarchar) + ':00' as fromTime,
            CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1) as nvarchar) + ':00' as toTime,    
            v.ID,
            COUNT(*) as nVisits
        FROM dbo.Table1 v INNER JOIN dbo.Table2 t ON v.FSLDU = t.FSLDU5
        WHERE v.AdmissionOn >= '01-01-2010' AND v.AdmissionOn < DATEADD(day,1,'02-01-2010') 
              AND v.ID = Coalesce(@id,ID)
        GROUP BY    
            CAST(v.AdmissionOn as date),
            (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1),
            (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
            (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),
            v.ID    
        ORDER BY 2,3,4

@id为null时,它将返回与此类似的结果集:

ALL 2010-01-01  0   0:00    24:00   7   4
ALL 2010-01-01  0   0:00    24:00   4   54
ALL 2010-01-01  0   0:00    24:00   0   2303
ALL 2010-01-01  0   0:00    24:00   3   412
ALL 2010-01-01  0   0:00    24:00   6   57
ALL 2010-01-01  0   0:00    24:00   2   64
ALL 2010-01-01  0   0:00    24:00   5   5
ALL 2010-01-01  0   0:00    24:00   1   103

但我想改为:

ALL 2010-01-01  0   0:00    24:00   ?   3002

我不确定会发生什么?专栏,可能是1,2,3,4,5,6,7?

这是否可能由诸如select / group by子句中的case或我不知道的存储过程之类的东西实现?如果不是,我可以创建一个新函数,但我希望通过输入@id作为null来实现这一目的。

2 个答案:

答案 0 :(得分:2)

GROUP BY @id代替GROUP BY v.id,并在SELECT语句中将v.id替换为@id

答案 1 :(得分:2)

将分组更改为:

  GROUP BY CAST(v.AdmissionOn as date), 
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1), 
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),  
     Case @Id When Null Then 0 Else Id End

我编辑过这个但不能单独使用的是:

  GROUP BY CAST(v.AdmissionOn as date), 
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1), 
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),  
     @Id

- 这些中的任何一个都会导致错误,因为select语句中的Id不在Group By中。通过使选择与Group By

一致,可以纠正任何一种情况
    SELECT 'ALL' as GeogType, CAST(v.AdmissionOn as date) as dtAdmission,
         (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) as timeInterval,  
         CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * 
              (24/1) as nvarchar) + ':00' as fromTime, 
         CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * 
              (24/1) + (24/1) as nvarchar) + ':00' as toTime, 
         Case @Id When Null Then 0 Else Id End Id,
         COUNT(*) as nVisits
    FROM dbo.Table1 v 
        JOIN dbo.Table2 t 
          ON v.FSLDU = t.FSLDU5
    WHERE v.AdmissionOn >= '01-01-2010' 
       AND v.AdmissionOn < DATEADD(day,1,'02-01-2010') 
       AND v.ID = Coalesce(@id,ID)      
    GROUP BY CAST(v.AdmissionOn as date), 
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1), 
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),  
     Case @Id When Null Then 0 Else Id End

    SELECT 'ALL' as GeogType, CAST(v.AdmissionOn as date) as dtAdmission,
         (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) as timeInterval,  
         CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * 
              (24/1) as nvarchar) + ':00' as fromTime, 
         CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * 
              (24/1) + (24/1) as nvarchar) + ':00' as toTime, 
         @Id Id, COUNT(*) as nVisits
    FROM dbo.Table1 v 
        JOIN dbo.Table2 t 
          ON v.FSLDU = t.FSLDU5
    WHERE v.AdmissionOn >= '01-01-2010' 
       AND v.AdmissionOn < DATEADD(day,1,'02-01-2010') 
       AND v.ID = Coalesce(@id,ID)      
  GROUP BY CAST(v.AdmissionOn as date), 
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1), 
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
     (DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),  
     @Id