总目标:由下面的头两个表yhb(用户表)和xlb(下量表)查询生成第三个表bcb(补充表)
生成的规则:
1.结果bcb中的列sj来自yhb.sj, xlsl来自yhb.des
2.对表xlb以zcm分组,
每组中如果xlrq没有2008年5月的,在结果集bcb中加一行,这行的xlrq的值设为2008-5-1,其它值取自yhb的zcm,sj,des
每组中如果xlrq没有2008年6月的,在结果集bcb中加一行,这行的xlrq的值设为2008-6-1,其它值取自yhb的zcm,sj,des
3.xlrq缺2008年其它月份的,则不加。缺别的年份的5、6月的,也不加。
总之一句话,原来不小心丢失了2008年5和6月的,现在要补上。下面是举例说明,设:
yhb
zcm   sj   des 
1       2   8     
2       3   6     
3       4   4     
4       5   2          
xlb
zcm     sj   xlrq           xlsl
1          2   2008-4-7     8
1          2   2008-5-17    8
1          2   2008-6-23    8
2          3   2008-4-9     6
2          3   2008-5-7     6
3          4   2008-2-9     4
3          4   2008-4-11    4
3          4   2008-6-9     4
4          5   2008-3-3     2
则产生的新表应为:
bcb
zcm     sj   xlrq         xlsl
2       3    2008-6-1     6
3       4    2008-5-1     4
4       5    2008-5-1     2
4       5    2008-6-1     2

解决方案 »

  1.   

    2.对表xlb以zcm分组, 
    每组中如果xlrq没有2008年5月的,在结果集bcb中加一行,这行的xlrq的值设为2008-5-1,其它值取自yhb的zcm,sj,des 
    每组中如果xlrq没有2008年6月的,在结果集bcb中加一行,这行的xlrq的值设为2008-6-1,其它值取自yhb的zcm,sj,des 如果有5月的数据怎么显示?
      

  2.   

    2008年5月任何一日的,对于该zcm的用户结果集中不加行,有就是不缺嘛;2008年6月也是同理
      

  3.   

    -->生成测试数据
     
    declare @yhb table([zcm] int,[sj] int,[des] int)
    Insert @yhb
    select 1,2,8 union all
    select 2,3,6 union all
    select 3,4,4 union all
    select 4,5,2
    --Select * from @yhb 
    declare @xlb table([zcm] int,[sj] int,[xlrq] Datetime,[xlsl] int)
    Insert @xlb
    select 1,2,'2008-4-7',8 union all
    select 1,2,'2008-5-17',8 union all
    select 1,2,'2008-6-23',8 union all
    select 2,3,'2008-4-9',6 union all
    select 2,3,'2008-5-7',6 union all
    select 3,4,'2008-2-9',4 union all
    select 3,4,'2008-4-11',4 union all
    select 3,4,'2008-6-9',4 union all
    select 4,5,'2008-3-3',2
    --Select * from @xlb
    declare @tb table(mth int, dt datetime)
    insert into @tb 
    select 5,'2008-05-01'
    union all select 6,'2008-06-01'select * from 
    (
    select a.zcm,a.sj,a.des,t.dt,x.zcm as xzcm from @yhb a 
    left join @tb t on 1=1
    left join @xlb x on x.[zcm] = a.[zcm] and month(x.[xlrq]) = t.mth
    ) b
    where xzcm is null
      

  4.   


    /*(2 row(s) affected)
    zcm         sj          des         dt                      xzcm
    ----------- ----------- ----------- ----------------------- -----------
    2           3           6           2008-06-01 00:00:00.000 NULL
    3           4           4           2008-05-01 00:00:00.000 NULL
    4           5           2           2008-05-01 00:00:00.000 NULL
    4           5           2           2008-06-01 00:00:00.000 NULL(4 row(s) affected)
    */
      

  5.   


    select a.[zcm],a.[sj],a.[des],a.dt from 
    (
    select y.[zcm],y.[sj],y.[des],b.mth,b.dt,x.[zcm] as xzcm from @yhb y
    left join (select 5 as mth, '2008-05-01' as dt union all select 6, '2008-06-01') b on 1=1
    left join @xlb x on x.[zcm] = y.[zcm] and month(x.[xlrq]) = b.mth
    ) a 
    where a.xzcm is null
    /*
    zcm         sj          des         dt
    ----------- ----------- ----------- ----------
    2           3           6           2008-06-01
    3           4           4           2008-05-01
    4           5           2           2008-05-01
    4           5           2           2008-06-01*/
      

  6.   

             
    insert bcb select zcm,sj,xlrq='2008-05-01',des from yhb where not exists(select 1 from xlb where convert(char(7),cast(xlrq as datetime),120)='2008-05')
    union select zcm,sj,xlrq='2008-06-01',des from yhb where not exists(select 1 from xlb where convert(char(7),cast(xlrq as datetime),120)='2008-06')