--查询参数
declare @编号 char(3),@年月 char(6)
select @编号='001',@年月='200503'--查询
select a.*,b.*
from 表1 a,表2 b,表2 b1,表2 b2
where a.编号=@编号
and a.编号=b.编号
and (
(
datediff(month,b.Date,@编号+'01')=0
and 
datediff(month,b1.Date,@编号+'01')=1
and 
datediff(month,b2.Date,@编号+'01')=2)
or (
datediff(month,b.Date,@编号+'01')=-1
and 
datediff(month,b1.Date,@编号+'01')=0
and 
datediff(month,b2.Date,@编号+'01')=1)
or (
datediff(month,b.Date,@编号+'01')=-2
and 
datediff(month,b1.Date,@编号+'01')=-1
and 
datediff(month,b2.Date,@编号+'01')=0))

解决方案 »

  1.   


    declare @s_cid char(3)
    declare @s_yearmonth char(6)
    declare @d_yearmonth datetime
    declare @i_months int --连续月数select @s_cid = '001'
    select @s_yearmonth = '200503'
    select @d_yearmonth = convert(datetime,left(@s_yearmonth,4) + '-' + right(@s_yearmonth,2) + '-01'
    select @i_months = 3
    select a.cid,b.name,count(*)
    from 表2 as a,表1 as b 
    where a.cid = b.id and
          datediff(mm,a.date,@d_yearmonth) >= -@i_months and
          datediff(mm,a.date,@d_yearmonth) <= @i_months
    group by count(*) < 2*@i_months - 1
      

  2.   

    有错误,改为:declare @s_cid char(3)
    declare @s_yearmonth char(6)
    declare @d_yearmonth datetime
    declare @i_months int --连续月数select @s_cid = '001'
    select @s_yearmonth = '200503'
    select @d_yearmonth = convert(datetime,left(@s_yearmonth,4) + '-' + right(@s_yearmonth,2) + '-01'
    select @i_months = 3
    select a.cid,b.name,count(*)
    from 表2 as a,表1 as b 
    where a.cid = b.id and
          datediff(mm,a.date,@d_yearmonth) >= -@i_months and
          datediff(mm,a.date,@d_yearmonth) <= @i_months
    group by a.cid,b.name
    having count(*) < 2*@i_months - 1
      

  3.   

    --建立測試環境
    create table table1(Id varchar(20),Name varchar(50))
    insert into  table1
    select '001','A' union all
    select '002','B' create table table2(Cid varchar(20),Date datetime,Amount int )
    insert into table2
    select '001',convert(char(10),'2005-01-25',120),20 union all
    select '001',convert(char(10),'2005-02-26',120),30 union all
    select '001',convert(char(10),'2005-03-24',120),24 union all
    select '001',convert(char(10),'2005-04-08',120) ,25union all
    select '002',convert(char(10),'2005-01-21',120),20 union all
    select '002',convert(char(10),'2005-02-10',120),30 union all
    select '002',convert(char(10),'2005-03-04',120),24 union all
    select '002',convert(char(10),'2005-04-14',120),25
    --測試
    declare @date varchar(10)
    declare @month int 
    set @date='2005-05-25'
    set @month=cast(substring(convert(char(10),@date,120),6,2) as int)
    select a.id,a.name,b.date,b.amount  from table1 a,table2 b
    where  cast( substring(convert(char(10),[Date],120),6,2) as int) 
    between @month -2 and @month+2
    and a.id=b.cid and a.id='001'
    --刪除測試用例
    drop table table1
    drop table table2不過此方法有一個缺陷就是只有查詢某一年的。
    比如@date='2005-05-01'的話就會出現負數(我的方法)。不過還可以查詢。
    本事是查詢'2005-04-11','2005-04-12','2005-05-01','2005-05-02','2005-05-03'白月分。
    這個還沒有想出來。
    誰想出來學習學習﹗﹗
    請見笑了﹗﹗