日期段拆分问题

作者在 2015-03-08 15:32:19 发布以下内容

类似问题:http://bbs.csdn.net/topics/390633445


编程对Table1表的日期数据处理,把日期段分拆,形成Table2的形式。

Create Table Table1 (cname C(10), date1 D, date2 D)
Insert Into Table1 Values ("Jack", {^1991-3-31}, {^1994-12-15})
Insert Into Table1 Values ("Lily", {^2000-9-10},{^2003-2-15})
Insert Into Table1 Values ("Marry", {^1995-5-26}, {^1996-2-20})
Insert Into Table1 Values ("Mimi", {^2007-4-1}, {^2008-2-12})
Insert Into Table1 Values ("Cici", {^2006-7-18}, {^2008-11-12})

结果表Table2
Cname  date3     date4        Nmonth
Jack   1991-4-1  1991-6-30     3
Jack   1991-7-1  1992-6-30    12
Jack   1992-7-1  1993-6-30    12
Jack   1993-7-1  1994-6-30    12 
Jack   1994-7-1  1994-12-31    6
Lily   2000-10-1 2001-6-30     9
Lily   2001-7-1  2002-6-30    12
Lily   2002-7-1  2003-2-28     8
Marry  1995-6-1  1995-6-30     1
Marry  1995-7-1  1996-2-29     8
Mimi   2007-5-1  2007-6-30     2
Mimi   2007-7-1  2008-2-29     8
Cici   2006-8-1  2007-6-30    11
Cici   2007-7-1  2008-6-30    12
Cici   2008-7-1  2008-11-30    5
要求:
1、date1 :以当年6月30日为界,当年6月30日后的,以次年6月30日为界。如,Jack 的date1 1991-3-31 那么 分解后第一段,为 Jack 1991-4-1 1991-6-30,第二段类推。
2、date1 变成date3--次月1日

3. date2 变成date4--当月最后一天。 

Create Cursor Table2 (cname C(10),date3 D,date4 D,Nmonth N(2))
Create Cursor Table1 (cname C(10),date1 D,date2 D)
Insert Into Table1 Values ("Jack",{^1991-03-31},{^1994-12-15})
Insert Into Table1 Values ("Lily",{^2000-9-10},{^2003-2-15})
Insert Into Table1 Values ("Marry",{^1995-5-26},{^1996-2-20})
Insert Into Table1 Values ("Mimi",{^2007-4-1},{^2008-2-12})
Insert Into Table1 Values ("Cici",{^2006-7-18},{^2008-11-12})
Scan
    ldDate1=Gomonth(Date(Year(Date1),Month(Date1),1),1)
    ldDate2=Gomonth(Date(Year(Date2),Month(Date2),1),1)-1
    lnI=0
    ldDate3=ldDate1
    ldDate4={}
    Do While ldDate4<=ldDate2
        lnI=lnI+1
        ldDate4=Gomonth(ldDate3,lnI)
        If Month(ldDate4)=7
            Insert Into Table2 Values (Table1.cname,ldDate3,ldDate4-1,lnI)
            ldDate3=ldDate4
            lnI=0
        EndIf
    EndDo
    If ldDate4>ldDate3
        Insert Into Table2 Values (Table1.cname,ldDate3,ldDate2,lnI)
    EndIf
EndScan
Select Table2
Browse 

将上面的代码做了修改,修改后的代码如下


Create Cursor Table2 (cname C(10),date3 D,date4 D,Nmonth N(2))
Create Cursor Table1 (cname C(10),date1 D,date2 D)
Insert Into Table1 Values ("Jack",{^1991-03-31},{^1994-12-15})
Insert Into Table1 Values ("Lily",{^2000-9-10},{^2003-2-15})
Insert Into Table1 Values ("Marry",{^1995-5-26},{^1996-2-20})
Insert Into Table1 Values ("Mimi",{^2007-4-1},{^2008-2-12})
Insert Into Table1 Values ("Cici",{^2006-7-18},{^2008-11-12})
Scan
    ldDate1=Gomonth(Date(Year(Date1),Month(Date1),1),1)
    ldDate2=Gomonth(Date(Year(Date2),Month(Date2),1),1)-1
    ldDate3=ldDate1
    lnI=0
    Do While ldDate1<ldDate2
        lnI=lnI+1
        ldDate1=Gomonth(ldDate1,1)
        If ldDate1<ldDate2
            If Month(ldDate1)=7
                Insert Into Table2 Values (Table1.cname,ldDate3,ldDate1-1,lnI)
                ldDate3=ldDate1
                lnI=0
            EndIf
        Else
            Insert Into Table2 Values (Table1.cname,ldDate3,ldDate2,lnI)
        EndIf
    EndDo
EndScan
Select Table2
Browse


日期时间问题 | 阅读 1630 次
文章评论,共0条
游客请输入验证码
浏览380535次