以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  sql2008 不支持iif 怎么办?  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=132508)

--  作者:sloyy
--  发布时间:2019/3/23 14:48:00
--  sql2008 不支持iif 怎么办?

图片点击可在新窗口打开查看此主题相关图片如下:360截图18141220286664.png
图片点击可在新窗口打开查看

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:小区收费计算.table


感谢 有点蓝大师的帮助,我改写了一下,做了个小区物业收费计算器,很成功,输入起始日期就自动计算物业费
但是 当我把它移植到sql2008 数据库,才发现 sql2008 不支持 iif 语句 ,这个要怎么改写? 
Sel ect 房屋代码,合同号,月租金起始日期,月租金截止日期,月租金,(iif(月租金起始日期 > #" & e.Form.Controls("TextBox1").text & "#,iif(月租金截止日期 < #" & e.Form.Controls("TextBox2").text & "#,DateDiff(\'m\', 月租金起始日期, 月租金截止日期),DateDiff(\'m\', 月租金起始日期, #" & e.Form.Controls("TextBox2").text & "#)),iif(月租金截止日期 < #" & e.Form.Controls("TextBox2").text & "#,DateDiff(\'m\', #" & e.Form.Controls("TextBox1").text & "#, 月租金截止日期),DateDiff(\'m\', #" & e.Form.Controls("TextBox1").text & "#, #" & e.Form.Controls("TextBox2").text & "#)))+1) * 月租金 As 费用 from {表A} where (月租金起始日期 >= #" & e.Form.Controls("TextBox1").text & "# And 月租金起始日期 <= #" & e.Form.Controls("TextBox2").text & "#) Or (月租金截止日期 >= #" & e.Form.Controls("TextBox1").text & "# And 月租金截止日期 <= #" & e.Form.Controls("TextBox2").text & "#) GROUP BY 房屋代码,合同号,月租金起始日期,月租金截止日期,月租金
[此贴子已经被作者于2019/3/23 14:50:52编辑过]

--  作者:有点蓝
--  发布时间:2019/3/23 15:00:00
--  
参考:http://www.foxtable.com/webhelp/scr/2960.htm


--  作者:sloyy
--  发布时间:2019/3/23 15:11:00
--  
case when 遇到第一个满足条的记录, 后面的就忽视了,结果不对
--  作者:sloyy
--  发布时间:2019/3/23 15:18:00
--  
这是我昨晚写的:
s elect case 
when (\'2019-02-01\' BETWEEN 月租金起始日期  and 月租金截止日期) and \'2019-10-31\' > 月租金截止日期 then sum(月租金 * (datediff(month,\'2019-02-01\',月租金截止日期)+1))  
when (\'2019-02-01< 月租金起始日期 and \'2019-10-31\' > 月租金截止日期) then sum(月租金 * (datediff(month,月租金起始日期,月租金截止日期)+1)) 
when  (\'2019-10-31\' BETWEEN 月租金起始日期  and 月租金截止日期) and \'2019-02-01\' < 月租金起始日期 then  sum(月租金 * (datediff(month,月租金起始日期,\'2019-10-31\')+1))  end  from {合同月租金}  where 房屋代码=\'405304\' group by 月租金起始日期 ,月租金截止日期

结果不对
[此贴子已经被作者于2019/3/23 15:19:06编辑过]

--  作者:sloyy
--  发布时间:2019/3/23 15:28:00
--  
哦  我明白了  谢谢大师  
用  SQLGroupTableBuilder 改写



--  作者:有点蓝
--  发布时间:2019/3/23 16:42:00
--  
select 房屋代码, sum(租金) as 租金 from (selec房屋代码, case 
when (\'2019-02-01\' BETWEEN 月租金起始日期  and 月租金截止日期) and \'2019-10-31\' > 月租金截止日期 then (月租金 * (datediff(month,\'2019-02-01\',月租金截止日期)+1))  
when (\'2019-02-01\'< 月租金起始日期 and \'2019-10-31\' > 月租金截止日期) then (月租金 * (datediff(month,月租金起始日期,月租金截止日期)+1)) 
when  (\'2019-10-31\' BETWEEN 月租金起始日期  and 月租金截止日期) and \'2019-02-01\' < 月租金起始日期 then  (月租金 * (datediff(month,月租金起始日期,\'2019-10-31\')+1)) else (月租金 * (datediff(month,\'2019-02-01\',\'2019-10-31\')+1))  end as  租金 from {合同月租金}  where 房屋代码=\'405304\' and  (月租金起始日期 >= #2019-02-01# and 月租金起始日期 <= #2019-10-31#) or (截月租金截止日期 >= #2019-02-01# and 月租金截止日期 <= #2019-10-31#) ) as a group by 房屋代码



--  作者:sloyy
--  发布时间:2019/3/23 19:27:00
--  
正是我想要的效果! 非常感谢 有点蓝大师