表结构tbSale:SaleDate CustName Qty
按日期和客户名称分组:
实现数据源:
日期(天)    客户名称    数量
1          小王         2
2          小王         3
3          小王         8
4          小王         0
.          小王         3
.          小王         6
.          小王         3
31         小王         5
1          小李         4
.          小李         0
.          小李         0
31         小李         4
.
.
注意如果某一天客户没有销售数据也要显示记录数量为0

解决方案 »

  1.   

    如:
    declare @T table([日期] int,[客户名称] nvarchar(2),[数量] int)
    Insert @T
    select 1,N'小王',2 union all
    select 2,N'小王',3 union all
    select 4,N'小王',3
     
    ;with C
    as
    (select 1 [日期]
    union all
    select [日期]+1 from C where [日期]<4)
    Select 
    c.[日期],c.[客户名称],isnull(sum(t.[数量]),0)[数量]
    from 
    (select c.[日期],t.[客户名称] from C,@T t group by  c.[日期],t.[客户名称])c
    left join 
    @T t on c.[日期]=t.[日期] and c.[客户名称]=t.[客户名称]
    group by c.[日期],c.[客户名称]日期          客户名称 数量
    ----------- ---- -----------
    1           小王   2
    2           小王   3
    3           小王   0
    4           小王   3
    (4 個資料列受到影響)
      

  2.   

    roy_88 给出的代码我不是很明白.
    我要的结果是
    日期          客户名称 数量
    ----------- ---- -----------
    1           小王   2
    2           小王   3
    3           小王   0
    4           小王   3
    5           小王   0
    6           小王   0
    .           小王   0
    .           小王   0
    .           小王   0
    31          小王   0
      

  3.   

    生成一个临时表,产生当月的日期列,然后left join你的表
      

  4.   

    declare @T table([日期] int,[客户名称] nvarchar(2),[数量] int)
    Insert @T
    select 1,N'小王',2 union all
    select 2,N'小王',3 union all
    select 4,N'小王',3 union all
    select 5,N'小张',3 union all
    select 6,N'小张',3select 日期=b.id,a.客户名称,数量=isnull(c.数量,0) from (
    select 客户名称 from  @t  group by 客户名称) a
    cross join #t b
    left join @t c
      on a.客户名称=c.客户名称 and b.id=c.日期/*
    日期          客户名称 数量          
    ----------- ---- ----------- 
    1           小王   2
    2           小王   3
    3           小王   0
    4           小王   3
    5           小王   0
    6           小王   0
    7           小王   0
    8           小王   0
    9           小王   0
    10          小王   0
    11          小王   0
    12          小王   0
    13          小王   0
    14          小王   0
    15          小王   0
    .
    .
    .
    .
    */
      

  5.   

    declare @T table([日期] int,[客户名称] nvarchar(2),[数量] int)
    Insert @T
    select 1,N'小王',2 union all
    select 2,N'小王',3 union all
    select 4,N'小王',3 union all
    select 5,N'小张',3 union all
    select 6,N'小张',3select 日期=b.id,a.客户名称,数量=isnull(c.数量,0) from (
    select 客户名称 from  @t  group by 客户名称) a
    cross join #t b
    left join @t c
      on a.客户名称=c.客户名称 and b.id=c.日期/*
    日期          客户名称 数量          
    ----------- ---- ----------- 
    1           小王   2
    2           小王   3
    3           小王   0
    4           小王   3
    5           小王   0
    6           小王   0
    7           小王   0
    8           小王   0
    9           小王   0
    10          小王   0
    11          小王   0
    12          小王   0
    13          小王   0
    14          小王   0
    15          小王   0
    .
    .
    .
    .
    */
      

  6.   

    记得了 临时表select top 31 id=identity(int,1,1) into #t from sysobjects
      

  7.   

    create table tb_Sales(c_Date int,c_Cust varchar(20),c_Qty int)
    go
    insert into tb_Sales
    select 1,'张三',2 union all
    select 3,'张三',3 union all
    select 8,'张三',8 union all
    select 10,'张三',4 union all
    select 2,'李四',1 union all
    select 5,'李四',5 union all
    select 7,'李四',7 union all
    select 12,'李四',8 
    create table t_temp(c_Date int)godeclare @date int
    set @date=1while(@date<=31)
    begin 
      insert into t_temp select @date
      set @date=@date+1
    endselect e.C_Date as 日期,e.c_Cust as 客户,sum(isnull(e.c_Qty,0)) as 数量
    from 
      (select c.C_Date,c.c_Cust,d.c_Qty
      from 
        (select a.C_Date,b.c_Cust
         from t_temp a cross join (select distinct c_Cust from tb_Sales)b)c 
        left join tb_Sales d on c.C_Date=d.C_Date and c.c_Cust=d.c_Cust)egroup by e.C_Date,e.c_Cust
    order by e.c_Cust,e.C_Date
    Drop table tb_Sales
    Drop table t_temp/*结果*/
    日期 客户 数量
    1 李四 0
    2 李四 1
    3 李四 0
    4 李四 0
    5 李四 5
    6 李四 0
    7 李四 7
    8 李四 0
    9 李四 0
    10 李四 0
    11 李四 0
    12 李四 8
    13 李四 0
    14 李四 0
    15 李四 0
    16 李四 0
    17 李四 0
    18 李四 0
    19 李四 0
    20 李四 0
    21 李四 0
    22 李四 0
    23 李四 0
    24 李四 0
    25 李四 0
    26 李四 0
    27 李四 0
    28 李四 0
    29 李四 0
    30 李四 0
    31 李四 0
    1 张三 2
    2 张三 0
    3 张三 3
    4 张三 0
    5 张三 0
    6 张三 0
    7 张三 0
    8 张三 8
    9 张三 0
    10 张三 4
    11 张三 0
    12 张三 0
    13 张三 0
    14 张三 0
    15 张三 0
    16 张三 0
    17 张三 0
    18 张三 0
    19 张三 0
    20 张三 0
    21 张三 0
    22 张三 0
    23 张三 0
    24 张三 0
    25 张三 0
    26 张三 0
    27 张三 0
    28 张三 0
    29 张三 0
    30 张三 0
    31 张三 0