数据信息存储表【Infos】有字段如下ID【ID号】,UserID【发布人ID】,AddTime【发布日期】,SortID【信息类别ID】,Status【状态值有3个值:正常,审核,过期】
有演示数据如下:ID   UserID    AddTime              SortID       Status
1      1       2012-4-1 0:00:00       2            1
2      1       2012-4-1 0:00:00       1            2
3      1       2012-4-1 0:00:00       4            3
4      1       2012-4-2 0:00:00       5            3
5      1       2012-4-3 0:00:00       6            2
6      1       2012-4-3 0:00:00       1            1
7      1       2012-4-5 0:00:00       2            1
8      1       2012-4-5 0:00:00       3            2
9      1       2012-4-6 0:00:00       4            3
10     2       2012-4-1 0:00:00       2            1
...Sort【信息类别表】有字段如下SortID【信息类别ID】,SortName【信息类别名称】SortID       SortName
1            营销1组
2            营销2组
3            营销3组
4            营销4组
5            营销5组
6            营销6组
...
求一存储过程,根据输入的用户ID与日期时间,统计出状态数据,
例如输入用户ID1,时间为2012-1-1至2012-4-30 应得到的数据如下:类别名称  总计数据数量   正常数据数量   审核中数据数量  过期数据数量
营销1组      2               1               1             0
营销2组      2               2               0             0
营销3组      1               0               1             0
营销4组      2               0               0             2
营销5组      1               0               0             1
营销6组      1               0               1             0

解决方案 »

  1.   


    --> 测试数据:[tbl]
    if object_id('[tbl]') is not null drop table [tbl]
    create table [tbl]([ID] int,[UserID] int,[AddTime] datetime,[SortID] int,[Status] int)
    insert [tbl]
    select 1,1,'2012-4-1 0:00:00',2,1 union all
    select 2,1,'2012-4-1 0:00:00',1,2 union all
    select 3,1,'2012-4-1 0:00:00',4,3 union all
    select 4,1,'2012-4-2 0:00:00',5,3 union all
    select 5,1,'2012-4-3 0:00:00',6,2 union all
    select 6,1,'2012-4-3 0:00:00',1,1 union all
    select 7,1,'2012-4-5 0:00:00',2,1 union all
    select 8,1,'2012-4-5 0:00:00',3,2 union all
    select 9,1,'2012-4-6 0:00:00',4,3 union all
    select 10,2,'2012-4-1 0:00:00',2,1--> 测试数据:[tt]
    if object_id('[tt]') is not null drop table [tt]
    create table [tt]([SortID] int,[SortName] varchar(7))
    insert [tt]
    select 1,'营销1组' union all
    select 2,'营销2组' union all
    select 3,'营销3组' union all
    select 4,'营销4组' union all
    select 5,'营销5组' union all
    select 6,'营销6组'go
    if OBJECT_ID('pro_tracy')is not null
    drop proc pro_tracy
    go
    create proc pro_tracy @UserID int
    as
    create table #tbl(
    [SortName] varchar(20),
    [UserID] int,
    [Status] int
    )
    insert #tbl
    select b.SortName,a.UserID,a.[Status] from tbl a 
    full join tt b on a.SortID=b.SortID
    where UserID=@UserID and [AddTime] between '2012-1-1' and '2012-4-30'
    select SortName as 类别名称 ,COUNT(*) as 总计数据数量,
    SUM(case when [Status]=1 then 1 else 0 end) as 正常数据数量,
    SUM(case when [Status]=2 then 1 else 0 end) as 审核中数据数量,
    SUM(case when [Status]=3 then 1 else 0 end) as 过期数据数量
    from #tbl group by SortNameexec pro_tracy 1
    /*
    类别名称 总计数据数量 正常数据数量 审核中数据数量 过期数据数量
    营销1组 2 1 1 0
    营销2组 2 2 0 0
    营销3组 1 0 1 0
    营销4组 2 0 0 2
    营销5组 1 0 0 1
    营销6组 1 0 1 0
    */
      

  2.   


    --直接查询语句实现
    select SortName as 类别名称 ,COUNT(*) as 总计数据数量,
    SUM(case when [Status]=1 then 1 else 0 end) as 正常数据数量,
    SUM(case when [Status]=2 then 1 else 0 end) as 审核中数据数量,
    SUM(case when [Status]=3 then 1 else 0 end) as 过期数据数量
    from (select b.SortName,a.UserID,a.[Status] from tbl a 
    full join tt b on a.SortID=b.SortID
    where UserID=1 and [AddTime] between '2012-1-1' and '2012-4-30')a
    group by SortName/*
    类别名称 总计数据数量 正常数据数量 审核中数据数量 过期数据数量
    营销1组 2 1 1 0
    营销2组 2 2 0 0
    营销3组 1 0 1 0
    营销4组 2 0 0 2
    营销5组 1 0 0 1
    营销6组 1 0 1 0
    */