当前位置: 代码迷 >> Sql Server >> 求一条统计语名!该如何解决
  详细解决方案

求一条统计语名!该如何解决

热度:18   发布时间:2016-04-27 12:27:46.0
求一条统计语名!
源表结构: 

SQL code
CREATE TABLE [dbo].[Results](    [RID] [int] IDENTITY(1,1) NOT NULL,    [LID] [int] NOT NULL,    [HID] [int] NOT NULL,    [Result] [bit] NOT NULL, CONSTRAINT [PK_Results] PRIMARY KEY CLUSTERED (    [HID] ASC,    [LID] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY]表数据RID    LID    HID    Result2    1    2003001    13    2    2003001    14    3    2003001    15    4    2003001    16    5    2003001    17    6    2003001    18    7    2003001    19    8    2003001    110    9    2003001    111    10    2003001    112    11    2003001    113    12    2003001    114    13    2003001    115    14    2003001    116    15    2003001    117    16    2003001    118    17    2003001    119    18    2003001    120    19    2003001    121    20    2003001    1......12872    1    2003002    112873    2    2003002    112874    3    2003002    112875    4    2003002    112876    5    2003002    112877    6    2003002    112878    7    2003002    112879    8    2003002    112880    9    2003002    112881    10    2003002    1.............要求统计结果with [temps] as    (    select row_number() over(order by RID Desc) as TID,HID,LID,Result  from Results where LID = 1    )     select sum(case when TID>=1 and TID <=10 then 1 else 0 end) as [C010]                from temps where Result = 1 这条语句写统计的是 最后一条的数据.可是以前的统计的数据呢?假如HID 有10期则统计的有10条记录如下35785891056


------解决方案--------------------
没有搞明白。。。要统计的对象
------解决方案--------------------
SQL code
with [temps] as    (    select row_number() over(PARTITION BY LID order by RID Desc) as TID,HID,LID,Result  from Results    )     select Lid,sum(case when TID>=1 and TID <=10 then 1 else 0 end) as [C010]               from temps where Result = 1    GROUP BY Lid
  相关解决方案