1 if exists( 2 select 3 exp_count 4 from 5 tbl_expend 6 where 7 exp_valid = 1 and exp_public = 0 and exp_monthly = 0 and convert(varchar(7),exp_date,120) = convert(varchar(7),cast('2020/04/03 22:57:20'as datetime ),120) 8 ) 9 begin 10 select 11 exp_count,exp_price,exp_public,exp_date,exp_item,exp_valid,exp_monthly,exp_record,exp_type,exp_level 12 into temp1 13 from 14 tbl_expend 15 where 16 exp_valid = 1 and exp_public = 0 and exp_monthly = 0 and convert(varchar(7),exp_date,120) = convert(varchar(7),cast('2020/04/03 22:57:20'as datetime ),120) 17 end 18 else 19 begin 20 select 0 as exp_count,0 as exp_price,0 as exp_public,getdate() as exp_date,'' as exp_item ,1 as exp_valid,0 as exp_monthly,0 as exp_record,'' as exp_type,0 as exp_level into temp1 21 end 22 go 23 24 select 25 convert(varchar(7),cast('2020/04/03 22:57:20'as datetime ),120) as 月份, 26 isnull(count(isnull(exp_item,0)),0) as 消费计数, isnull(sum(isnull(exp_price,0)*isnull(exp_count,0)),0) as 花费金额 27 ,isnull(inc_amount,0) as 收款金额, (isnull(inc_amount,0)-isnull(sum(isnull(exp_price,0)*isnull(exp_count,0)),0)) as 余额 28 from 29 temp1 a left join ( 30 select 31 sum(isnull(inc_amount,0)) as inc_amount 32 from tbl_income 33 where 34 convert(varchar(7),inc_date,120) = convert(varchar(7),cast('2020/04/03 22:57:20'as datetime ),120) 35 and inc_valid = 1 and inc_monthly = 0 and inc_public = 0 ) b on 1 = 1 36 where 37 38 exp_valid = 1 and exp_monthly = 0 and exp_public = 0 39 and convert(varchar(7),exp_date,120) = convert(varchar(7),cast('2020/04/03 22:57:20'as datetime ),120) 40 group by 41 convert(varchar(7),exp_date,111),inc_amount 42 43 go 44 45 select * from temp1 46 47 go 48 49 drop table temp1