Sql server 2008 r2 如何限制虚拟计算列?

Sql server 2008 r2 如何限制虚拟计算列?,sql-server-2008-r2,Sql Server 2008 R2,我想限制sql查询中的虚拟计算列。我不知道怎么做。 这是我的问题 select Distinct(Chief_Complaint), COUNT(Chief_Complaint) as 'No. of Cases Encountered(January)', COUNT(Chief_Complaint) as 'No. of Cases Encountered(February)' from Medical_Treatment group by Chief_Complaint 专栏主任的抱怨

我想限制sql查询中的虚拟计算列。我不知道怎么做。 这是我的问题

select Distinct(Chief_Complaint), COUNT(Chief_Complaint) as 'No. of Cases
Encountered(January)',  COUNT(Chief_Complaint) as 'No. of Cases Encountered(February)'
from Medical_Treatment
group by Chief_Complaint
专栏主任的抱怨是一个字符串

我想在COUNT(Chief_Complaint)中添加一个限制,即“遇到的案例数(一月)”,这样它将只计算从我的日期列开始到一月的不同值的数量,并在COUNT(Chief_Complaint)中添加“遇到的案例数(二月)”,其中月份是二月 可能吗?对不起,我的英语不好


谢谢您的回复。

这样的东西对您有用

CREATE TABLE #Complaint(
     id                 INT IDENTITY(1,1)
    ,Chief_Complaint    NVARCHAR(100)
    ,ComplaintDate      DATETIME
)

INSERT INTO #Complaint
    ( Chief_Complaint, ComplaintDate)
VALUES
     ( 'Complaint1', '20160101')
    ,( 'Complaint1', '20160101')
    ,( 'Complaint1', '20160101')
    ,( 'Complaint2', '20160101')
    ,( 'Complaint2', '20160101')
    ,( 'Complaint3', '20160101')

    ,( 'Complaint1', '20160201')
    ,( 'Complaint1', '20160201')
    ,( 'Complaint2', '20160201')
    ,( 'Complaint2', '20160201')
    ,( 'Complaint2', '20160201')


;WITH cteComplaints
AS(
    SELECT
        C.Chief_Complaint
        ,CASE WHEN DATENAME( month, C.ComplaintDate ) = 'January'
            THEN COUNT( C.Chief_Complaint)
         END    'January_CASES'
        ,CASE WHEN DATENAME( month, C.ComplaintDate ) = 'February'
            THEN COUNT( C.Chief_Complaint)
         END    'February_CASES'
    FROM
        #Complaint C
    GROUP BY
        C.Chief_Complaint,DATENAME( month, C.ComplaintDate )
)
SELECT
    C.Chief_Complaint
    ,SUM(ISNULL(C.January_CASES,0)) 'January_CASES'
    ,SUM(ISNULL(C.February_CASES,0)) 'February_CASES'
FROM
    cteComplaints   C
GROUP BY
    C.Chief_Complaint

DROP TABLE #Complaint

我想你没有明白我的问题。我已经有了一个数据库,其中有一个表“医疗”。现在我只想限制我的select语句。我想要的就是这样。假设这是一张桌子。主诉/案件数量(一月)/案件数量(二月)腹部/3/5你失去了我。请提供您的表的模式,然后我会看看我能做些什么。如果我的问题很难理解,请原谅。好吧,让我更清楚一点,我有一个名为Medical_Treatment的表,其中包含6列,但我只列出2列,因为其余的并不重要。列是Date(显然是日期类型)和Chief_Complaint(是字符串)。Chief_投诉栏中的每个数据都有相应的日期。现在,我想根据我的日期栏中的月份来统计所有的投诉。输出将是Chief_投诉、投诉一月、投诉二月、投诉三月等。希望您能帮助我。抱歉,我没有看到您所有的答案。我试过你的代码,它成功了!这就是我要找的。谢谢!:)我还有一个问题。我想为每个投诉的投诉总数增加一列,为每个月的投诉总数增加一行。希望你能再次帮助我。