下面的代码是关于合同的,看起来很长,其实非常简单,筛选的条件就是TypeOfPayment(交款时间)和LeaseDateFrom(合同起始日)。----TypeOfPayment(交款时间)有两种方式:1.月末; 2.季度末1、4、7、10作为第一种筛选条件。----LeaseDateFrom(合同起始日)和变量@Time2做比较的值,作为筛选的第二个条件---- 我写的代码虽然可以得到正确的结果,但我想把代码变得更简单。我尝试用if 、case 等嵌套,但没有成功。下面的代码貌似很长,其实简单,几个when后面的条件都只有微小的区别。请大家帮忙看看!declare @time2 datetime
select @time2='2012-1-31'
select a.TypeOfPayment,a.LeaseDateFrom,
(case
when a.TypeOfPayment like '%月末%'
then DATEDIFF(mm, DATEADD (MONTH ,-1,a.LeaseDateFrom), @time2)/12*12*a.MonthRent +
(DATEDIFF (MM,DATEADD (MONTH ,-1,a.LeaseDateFrom),@time2)%12+1)*a.MonthRent when TypeOfPayment='季度末7日前预交1、4、7、10'
and ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=1) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=4) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=7) or (DATEPART(month,DATEADD (DD,-7,a.LeaseDateFrom))=10))
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent +3*a.MonthRent
when TypeOfPayment='季度末7日前预交1、4、7、10'
and ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>1) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>4) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>7) or (DATEPART(month,DATEADD (DD,-7,a.LeaseDateFrom))<>10))
and DATEPART (MONTH,@time2)>DATEPART (MONTH ,a.LeaseDateFrom )
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent +3*MonthRent
when TypeOfPayment='季度末7日前预交1、4、7、10'
and ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>1) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>4) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>7) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>10))
and DATEPART (MONTH,@time2)<DATEPART (MONTH ,a.LeaseDateFrom)
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent
else nullend) as 总应收
from ContractTable a join PropertyTable b on a.City +a.Road +a.[Address] =b.City +b.Road +b.[Address]
select @time2='2012-1-31'
select a.TypeOfPayment,a.LeaseDateFrom,
(case
when a.TypeOfPayment like '%月末%'
then DATEDIFF(mm, DATEADD (MONTH ,-1,a.LeaseDateFrom), @time2)/12*12*a.MonthRent +
(DATEDIFF (MM,DATEADD (MONTH ,-1,a.LeaseDateFrom),@time2)%12+1)*a.MonthRent when TypeOfPayment='季度末7日前预交1、4、7、10'
and ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=1) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=4) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=7) or (DATEPART(month,DATEADD (DD,-7,a.LeaseDateFrom))=10))
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent +3*a.MonthRent
when TypeOfPayment='季度末7日前预交1、4、7、10'
and ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>1) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>4) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>7) or (DATEPART(month,DATEADD (DD,-7,a.LeaseDateFrom))<>10))
and DATEPART (MONTH,@time2)>DATEPART (MONTH ,a.LeaseDateFrom )
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent +3*MonthRent
when TypeOfPayment='季度末7日前预交1、4、7、10'
and ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>1) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>4) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>7) or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>10))
and DATEPART (MONTH,@time2)<DATEPART (MONTH ,a.LeaseDateFrom)
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent
else nullend) as 总应收
from ContractTable a join PropertyTable b on a.City +a.Road +a.[Address] =b.City +b.Road +b.[Address]
条件写在 join on那里与 写在where 后边 有什么区别么?
select @time2='2012-1-31'
select a.TypeOfPayment,a.LeaseDateFrom,
(case
when a.TypeOfPayment like '%月末%'
then DATEDIFF(mm, DATEADD (MONTH ,-1,a.LeaseDateFrom), @time2)/12*12*a.MonthRent +
(DATEDIFF (MM,DATEADD (MONTH ,-1,a.LeaseDateFrom),@time2)%12+1)*a.MonthRent when TypeOfPayment='季度末7日前预交1、4、7、10' then
case
when ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=1)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=4)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=7)
or (DATEPART(month,DATEADD (DD,-7,a.LeaseDateFrom))=10))
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent +3*a.MonthRent when ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>1)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>4)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>7)
or (DATEPART(month,DATEADD (DD,-7,a.LeaseDateFrom))<>10))
and DATEPART (MONTH,@time2)>DATEPART (MONTH ,a.LeaseDateFrom )
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent +3*MonthRentwhen ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>1)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>4)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>7)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>10))
and DATEPART (MONTH,@time2)<DATEPART (MONTH ,a.LeaseDateFrom)
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent
end
else null
end) as 总应收
from ContractTable a join PropertyTable b on a.City +a.Road +a.[Address] =b.City +b.Road +b.[Address]
select @time2='2012-1-31'
select a.TypeOfPayment,a.LeaseDateFrom,
(case
when a.TypeOfPayment like '%月末%'
then DATEDIFF(mm, DATEADD (MONTH ,-1,a.LeaseDateFrom), @time2)/12*12*a.MonthRent +
(DATEDIFF (MM,DATEADD (MONTH ,-1,a.LeaseDateFrom),@time2)%12+1)*a.MonthRent when TypeOfPayment='季度末7日前预交1、4、7、10' then
case
when ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=1)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=4)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))=7)
or (DATEPART(month,DATEADD (DD,-7,a.LeaseDateFrom))=10))
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent +3*a.MonthRent when ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>1)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>4)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>7)
or (DATEPART(month,DATEADD (DD,-7,a.LeaseDateFrom))<>10))
and DATEPART (MONTH,@time2)>DATEPART (MONTH ,a.LeaseDateFrom )
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent +3*MonthRentwhen ((DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>1)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>4)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>7)
or (DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom))<>10))
and DATEPART (MONTH,@time2)<DATEPART (MONTH ,a.LeaseDateFrom)
then DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)/12*a.MonthRent +
DATEDIFF (MM,dateadd(DD ,-7, a.LeaseDateFrom),@time2)%12/3*3*a.MonthRent
end
else null
end) as 总应收
from ContractTable a join PropertyTable b on a.City +a.Road +a.[Address] =b.City +b.Road +b.[Address]
我写出来的代码只是很少的一部分,在代码的结尾部分才有where 条件! 我想问一下,您的意思是不是在代码的中间部分添加where条件呢?如果是的话该怎么写呢?
这一句有办法合并起来么?DATEPART(month, DATEADD (DD,-7,a.LeaseDateFrom)都是一样的,不同的是后面的1、4、7、10