SQL Server:一列上有多个select语句

时间:2014-05-10 08:16:17

标签: sql sql-server count

我试图在一个表中的一列中计算许多不同的值,所有这些都在一个查询中。

编辑:查询现在可以使用@Red编辑的建议来移动每个选择之外的别名,并通过@dnoeth移动'来自'线 - 谢谢!我还有一个问题,我将为其添加评论。

    select 
        m1.mods_in_study,
        (select count(*) 
         from dbo.study as m2 
         where m2.mods_in_study like '%CT%'
           and study_datetime >= DATEADD(day, datediff(day, 1, getdate()), 0)
           and study_datetime < DATEADD(day, datediff(day, 0, getdate()), 0)) as CT,
        (select count(*)
         from dbo.study as m3 
         where m3.mods_in_study like '%MR%'
           and study_datetime >= DATEADD(day, datediff(day, 1, getdate()), 0)
           and study_datetime < DATEADD(day, datediff(day, 0, getdate()), 0)) as MR,
        (select count(*)
         from dbo.study as m4 
         where m4.mods_in_study like '%CR%'
           and study_datetime >= DATEADD(day, datediff(day, 1, getdate()), 0)
           and study_datetime < DATEADD(day, datediff(day, 0, getdate()), 0)) as CR,
        (select count(*)
         from dbo.study as m5 
         where m5.mods_in_study like '%DX%'
           and study_datetime >= DATEADD(day, datediff(day, 1, getdate()), 0)
           and study_datetime < DATEADD(day, datediff(day, 0, getdate()), 0)) as DX,
        (select count(*) 
         from dbo.study as m6 
         where m6.mods_in_study like '%US%'
           and study_datetime >= DATEADD(day, datediff(day, 1, getdate()), 0)
           and study_datetime < DATEADD(day, datediff(day, 0, getdate()), 0)) as US,
        (select count(*) 
         from dbo.study as m7 
         where m7.mods_in_study like '%PT%'
           and study_datetime >= DATEADD(day, datediff(day, 1, getdate()), 0)
           and study_datetime < DATEADD(day, datediff(day, 0, getdate()), 0)) as PT,
        (select count(*)
         from dbo.study as m8 
         where m8.mods_in_study like '%NM%'
           and study_datetime >= DATEADD(day, datediff(day, 1, getdate()), 0)
           and study_datetime < DATEADD(day, datediff(day, 0, getdate()), 0)) as NM,
        (select count(*) 
         from dbo.study as m9
         where m9.mods_in_study like '%MG%'
           and study_datetime >= DATEADD(day, datediff(day, 1, getdate()), 0)
           and study_datetime < DATEADD(day, datediff(day, 0, getdate()), 0)) as MG
from dbo.study as m1
group by m1.mods_in_study

现在,SQL正在抱怨&#39;(&#39;在第二次计算。我应该在这个查询中使用别名吗?我知道这个联盟的世界有效,但是由于我需要每个计数的列名称,因此无法在我的报告中正确显示。

我做错了什么,或者只是有更好的方法来解决这个问题?

以下是我期待的结果:

CT  MR  CR  DX  US  PT  NM MG
130 39  240 12  45   7  17 121

3 个答案:

答案 0 :(得分:2)

我没有可以测试MSSQL的终端,但是通过查询来看,你似乎只是缺少一个逗号。

答案 1 :(得分:0)

如果这是您所追求的,我建议您针对以下模式进行编码。 相关子查询和更好的实践通常更有效;

SELECT   mods_in_study
        ,CT =   SUM (   CASE 
                            WHEN mods_in_study LIKE '%CT%' 
                            AND  study_datetime >= DATEADD(DAY ,DATEDIFF(DAY ,1 ,GETDATE()) ,0) 
                            AND  study_datetime < DATEADD(DAY ,DATEDIFF(DAY ,0 ,GETDATE()) ,0)
                            THEN 1
                            ELSE 0
                        END
                    )
        ,MR =   SUM (   CASE 
                            WHEN mods_in_study LIKE '%MR%' 
                            AND  study_datetime >= DATEADD(DAY ,DATEDIFF(DAY ,1 ,GETDATE()) ,0) 
                            AND  study_datetime < DATEADD(DAY ,DATEDIFF(DAY ,0 ,GETDATE()) ,0)
                            THEN 1
                            ELSE 0
                        END
                    )
        --  Etc...
FROM   dbo.study
GROUP BY mods_in_study

答案 2 :(得分:0)

添加预期结果集后,更容易理解: - )

要获得单行,您不需要FROMGROUP BY,因此只需删除m1.mods_in_study,from dbo.study as m1 group by m1.mods_in_study

但是因为你总是在同一张桌子上使用相同的条件,所以可以简化为

SELECT 
    COUNT(CASE WHEN mods_in_study LIKE '%CT%' THEN 1 END) AS CT, 
    COUNT(CASE WHEN mods_in_study LIKE '%MR%' THEN 1 END) AS MR, 
    ...
FROM dbo.study
WHERE study_datetime >= DATEADD(day, datediff(day, 1, getdate()), 0)
  AND study_datetime < DATEADD(day, datediff(day, 0, getdate()), 0))    

MarkD的解决方案非常相似......