首页 > 开发 > 综合 > 正文

本人的原创代码(SQL及T_SQL)

2024-07-21 02:05:38
字体:
来源:转载
供稿:网友


/*三明电力临时卡处理sql代码,临时卡为专门一类卡,消费时五折优惠,月消费有限额,采取策略是“钱多存,消费少报”*/select dbo.t_station.statname as 工作站, dbo.t_cashiers.name as 出纳员,       lll.opdt as 日期, lll.infare as 存款额, lll.incount as 存款次数, lll.outfare as 取款额,       lll.outcount as 取款次数, isnull(lll.suminfare, 0) / 2 as 让利额, isnull(lll.countinfare,       0) as 让利次数, (lll.infare - lll.outfare) - isnull(lll.suminfare, 0)       / 2 as 存取款应缴金额from (select dbo.t_cashday.statid, dbo.t_cashday.opdt, dbo.t_cashday.cashid,               dbo.t_cashday.infare, dbo.t_cashday.incount, dbo.t_cashday.outfare,               dbo.t_cashday.outcount, ll.suminfare, ll.countinfare        from dbo.t_cashday left outer join /*采用左外联接查询*/                  (select cash.statid, cash.cashid, dbo.t_cashday.opdt,                        dbo.t_cashday.infare, dbo.t_cashday.incount,                        dbo.t_cashday.outfare, dbo.t_cashday.outcount, cash.suminfare,                        countinfare                 from (select cashrec.statid, cashrec.cashid, cashrec.dt,                                sum(cashrec.infare) as suminfare, count(*)                                as countinfare                         from (select statid, cashid, rtrim(cast(year(cashdt)                                        as char)) + '-' + rtrim(cast(month(cashdt) as char))                                        + '-' + rtrim(cast(day(cashdt) as char)) as dt /*存款日期*/, infare,                                        outfare, customerid                                 from dbo.t_cashrec                                 where outfare = 0) cashrec /*存款具体时间转换为日期的视图*/ inner join                               dbo.t_customers on                                cashrec.customerid = dbo.t_customers.customerid                         where (dbo.t_customers.cardtype = 1 /*临时卡类型*/ )                          group by cashrec.statid, cashrec.cashid, cashrec.dt)                        cash  /*以statid,cashid,dt作为分组字段,统计出临时卡的存款总额及存款次数的视图*/ inner join                       dbo.t_cashday on cash.statid = dbo.t_cashday.statid and                        cash.cashid = dbo.t_cashday.cashid and                        cash.dt = dbo.t_cashday.opdt) ll /*cash视图与t_cashday表进行联接查询的视图*/ on               dbo.t_cashday.opdt = ll.opdt and dbo.t_cashday.cashid = ll.cashid and               ll.statid = dbo.t_cashday.statid        where (dbo.t_cashday.opdt between '2004-7-1' and '2004-7-1'/*临时卡存款的时间范围*/)) lll inner join      dbo.t_cashiers on lll.cashid = dbo.t_cashiers.cashid and       lll.statid = dbo.t_cashiers.statid inner join      dbo.t_station on lll.statid = dbo.t_station.statid

select dbo.t_station.statname as 工作站, dbo.t_eatery.stname as 食堂,       dbo.t_group.grpname as 食堂经营组, dbo.t_terms.port as 端口,       dbo.t_terms.termname as 窗机名称, yf.sumdt as 日期,       dbo.t_meal.mealname as 餐别, yf.incomefare as 营业额,       yf.incomecount as 营业次数, isnull(yf.sumopfare, 0) / 2 as 优惠额,       isnull(yf.countopfare, 0) as 优惠次数, yf.mngfare as 管理费,       yf.correctfare as 纠错额, yf.incomefare - isnull(yf.sumopfare, 0)       / 2 + yf.mngfare - yf.correctfare as 实际收入from (select dbo.t_incomerec.statid, dbo.t_incomerec.mealid,               dbo.t_incomerec.port, dbo.t_incomerec.term, dbo.t_incomerec.sumdt,               dbo.t_incomerec.incomefare, dbo.t_incomerec.incomecount,               s_c_opf.sumopfare, s_c_opf.countopfare, dbo.t_incomerec.mngfare,               dbo.t_incomerec.correctfare        from dbo.t_incomerec left outer join                  (select consumerec.statid, consumerec.port, consumerec.term,                        consumerec.mealid, dt, sum(consumerec.opfare) as sumopfare,                        count(*) as countopfare                 from (select statid, port, term, customerid,                                rtrim(cast(year(dbo.t_consumerec.opdt) as char))                                + '-' + rtrim(cast(month(dbo.t_consumerec.opdt)                                as char)) + '-' + rtrim(cast(day(dbo.t_consumerec.opdt)                                as char)) as dt, collectdt, mealid, opfare, mngfare,                                oddfare                         from dbo.t_consumerec) as consumerec inner join                       dbo.t_customers on                        consumerec.customerid = dbo.t_customers.customerid                 where (dbo.t_customers.cardtype = 1)                  group by consumerec.statid, consumerec.port, consumerec.term,                        consumerec.mealid, consumerec.dt) s_c_opf on               s_c_opf.statid = dbo.t_incomerec.statid and               s_c_opf.port = dbo.t_incomerec.port and               s_c_opf.term = dbo.t_incomerec.term and               s_c_opf.mealid = dbo.t_incomerec.mealid and               dbo.t_incomerec.sumdt = s_c_opf.dt        where (dbo.t_incomerec.sumdt between '2004-7-6' and '2004-7-6'))       yf inner join      dbo.t_eatery on yf.statid = dbo.t_eatery.statid inner join      dbo.t_group on yf.statid = dbo.t_group.statid and       dbo.t_eatery.stid = dbo.t_group.stid inner join      dbo.t_station on yf.statid = dbo.t_station.statid inner join      dbo.t_terms on dbo.t_eatery.statid = dbo.t_terms.statid and       dbo.t_group.stid = dbo.t_terms.stid and       dbo.t_group.grpid = dbo.t_terms.grpid and yf.port = dbo.t_terms.port and       yf.term = dbo.t_terms.term inner join      dbo.t_meal on yf.mealid = dbo.t_meal.mealid

-------------------------------------------------------------------------------------

/*列出未注册卡sql代码*/select t_consumerec.customerid as 未注册客户号,t_consumerec.oddfare as 未注册卡余额,t_consumerec.opdt as 未注册卡消费时间 from t_consumerec where t_consumerec.customerid<>all(select customerid from t_customers) and t_consumerec.opdt between '2004-9-12 00:00:00' and '2004-9-15 23:59:59' -----------------------------------------------------------------------/*门禁系统后台数据库idcard中t_customers表的触发器*/create trigger delterms on t_customers for update as declare @id int,@k1 int,@k2 intselect @k1=count(*) from  t_customers where insertedselect @k2=count(*) from  t_customers where deletedif @[email protected] and @k1>0begin select  @id=t_customers.customerid from t_customers,t_terms where t_customers.customerid=t_terms.customerid and          t_customers.cardno<>t_terms.cardno          delete from t_terms where [email protected] end-----------------------------------------------------------------------/*门禁系统的卡号纠正t_sql代码*/declare @id int, @no int while 1=1 begin    select @id=t_customers.customerid,@no=t_customers.cardno  from t_customers,t_terms where      

t_customers.customerid=t_terms.customerid and t_customers.cardno<>t_terms.cardno      order by t_customers.customerid asc

   if exists(select t_customers.customerid from t_customers,t_terms  where t_customers.customerid=t_terms.customerid and    t_customers.cardno<>t_terms.cardno )     begin        update t_terms set [email protected] where [email protected]     end    else     begin       print '更新完毕!'       break     end     end----------------------------------------------------------------------- /*清除冲突补助月份的t_sql代码*/declare @id int declare @month datetime while 1=1 begin          select @id=t_subsidynotputout.customerid ,@month=dbo.t_subsidynotputout.[month]          from dbo.t_subsidynotputout inner join                dbo.t_subsidyputout on                dbo.t_subsidynotputout.customerid = dbo.t_subsidyputout.customerid and                 dbo.t_subsidynotputout.[month] = dbo.t_subsidyputout.[month] if (exists(select * from t_subsidynotputout     where ([email protected] and [month] = @month))) begin    delete from t_subsidynotputout    where ([email protected] and [month] = @month)    continue end else   begin     print '没有相关客户的相关补助发放冲突月份!或补助冲突月份已经清理完毕!'     break   end end----------------------------------------------------------------------- /*前期补助无法发放名单sql代码*/select dbo.t_customers.name as 姓名, dbo.t_subsidynotputout.[month] as 月份 from dbo.t_subsidynotputout inner join       dbo.t_subsidyputout on       dbo.t_subsidynotputout.customerid = dbo.t_subsidyputout.customerid and       dbo.t_subsidynotputout.[month] = dbo.t_subsidyputout.[month] inner join       dbo.t_customers on       dbo.t_subsidynotputout.customerid = dbo.t_customers.customerid--------------------------------------------------------------------------------/*显示人员消费情况及联系sql代码*/select dbo.t_customers.name as 姓名, dbo.t_consumerec.opdt as 消费时间,       dbo.t_consumerec.opfare as 消费额, dbo.t_consumerec.oddfare as 余额,       isnull(dbo.t_department.dpname1, '') + isnull(dbo.t_department.dpname2, '')       + isnull(dbo.t_department.dpname3, '') as 部门,       dbo.t_station.statname as 工作站, dbo.t_eatery.stname as 食堂,       dbo.t_group.grpname as 食堂组, dbo.t_terms.port as 端口号,       dbo.t_terms.termname as 窗机名称from dbo.t_consumerec inner join      dbo.t_customers inner join      dbo.t_department on substring(dbo.t_customers.account, 1, 2)       = dbo.t_department.dpcode1 and substring(dbo.t_customers.account, 3, 2)       = dbo.t_department.dpcode2 and substring(dbo.t_customers.account, 5, 3)       = dbo.t_department.dpcode3 on       dbo.t_consumerec.customerid = dbo.t_customers.customerid inner join      dbo.t_eatery on dbo.t_consumerec.statid = dbo.t_eatery.statid inner join      dbo.t_group on dbo.t_consumerec.statid = dbo.t_group.statid and       dbo.t_eatery.stid = dbo.t_group.stid inner join      dbo.t_station on dbo.t_consumerec.statid = dbo.t_station.statid inner join      dbo.t_terms on dbo.t_eatery.statid = dbo.t_terms.statid and       dbo.t_eatery.stid = dbo.t_terms.stid and       dbo.t_group.grpid = dbo.t_terms.grpid and       dbo.t_consumerec.port = dbo.t_terms.port and       dbo.t_consumerec.term = dbo.t_terms.term----------------------------------------------------------------------------------------/*列出存取款及联系sql代码*/select dbo.t_customers.name as 姓名, isnull(dbo.t_department.dpname1, '') + isnull(dbo.t_department.dpname2, '')       + isnull(dbo.t_department.dpname3, '') as 部门,       dbo.t_cashrec.cashdt as 存取款时间, dbo.t_cashrec.infare as 存款额,       dbo.t_cashrec.outfare as 取款额, dbo.t_cashrec.oddfare as 余额,       dbo.t_station.statname as 工作站, dbo.t_cashiers.name as 出纳员,       dbo.t_cashrec.port as 出纳机端口, dbo.t_cashrec.term as 出纳机机器号 from dbo.t_station inner join       dbo.t_cashiers on dbo.t_station.statid = dbo.t_cashiers.statid inner join       dbo.t_customers inner join       dbo.t_department on substring(dbo.t_customers.account, 1, 2)       = dbo.t_department.dpcode1 and substring(dbo.t_customers.account, 3, 2)       = dbo.t_department.dpcode2 and substring(dbo.t_customers.account, 5, 3)       = dbo.t_department.dpcode3 inner join       dbo.t_cashrec on dbo.t_customers.customerid = dbo.t_cashrec.customerid on       dbo.t_cashiers.cashid = dbo.t_cashrec.cashid and       dbo.t_station.statid = dbo.t_cashrec.statid-----------------------------------------------------------------------------------------/*列出incomerec帐本中与现有窗机比较后不存在的窗机*/select port, termfrom t_incomerecwhere (term <> all          (select term         from t_terms         where statid = 2)) and (statid = 2)----------------------------------------------------------------------------------------/*公安专发放补助前处理的sql代码(当月补助形成后)*/if exists(select customerid from t_subsidynotputout where (cardtype=5 or cardtype=6) and subsidy=0 group by customerid having count(*)>1)  begin /*判断5、6类卡是否存在一人两条前期记录即存在去年没领补助的教师*/    select month ,customerid as 去年没领补助的教师的客户号,cardtype,subsidy from t_subsidynotputout where     customerid    =any(select customerid from t_subsidynotputout where (cardtype=5 or cardtype=6) and subsidy=0 group by     customerid having count(*)>1)  endelse begin--go  insert into t_subsidymonthplan(month,customerid,cardtype,subsidy)  select * from t_subsidynotputout where (cardtype=5 or cardtype=6) and subsidy=0/*公安专在当月补助形成后(5、6类卡为教师卡,一年只领一次补助),发补助前:向表monthplan插入符合在表notputout中5、6类卡subsidy为0的记录,注意是否存在一人两条前期记录(一般不会出现这种况,除非去年没领补助),否则在monthplan表中后面的更新时间会出错!!*/--go  delete t_subsidynotputout where (cardtype=5 or cardtype=6) and subsidy=0/*删除notputout被复制的记录*/--go  update t_subsidymonthplan set month='2004-12-1' where (cardtype=5 or cardtype=6) and subsidy=0 /*更改表monthplan中的month日期,月份根据实际定*/--go  update t_customers set subsidydt='2004-12-1' where (cardtype=5 or cardtype=6) and cursubsidyfare>0/*更改t_customers表中的补助时间,注意与t_subsidymonthplan表中的month日期值保持一致!!*/ end  go  select sum(cursubsidyfare) as 客户帐本的补助总额 from t_customers  go  select sum(subsidy) as 前期补助总额 from t_subsidynotputout  go select sum(subsidy) as 当月补助总额 from t_subsidymonthplan/*查询客户帐本的补助总额是否等于前期补助总额与当月补助总额之和!*/--------------------------------------------------------------------------------------------------                 /*师大补助月报表信息纠正t_sql代码*/declare @k int, @k1 int,@k2 int,@month datetime,@subsidyfare money,@cardtype tinyint,@subsidyfare1 money,@subsidyfare2 money

   /*清除冲突补助月份的t_sql代码开始*/declare @id int while 1=1 begin          select @id=t_subsidynotputout.customerid ,@month=dbo.t_subsidynotputout.[month]          from dbo.t_subsidynotputout inner join                dbo.t_subsidyputout on                dbo.t_subsidynotputout.customerid = dbo.t_subsidyputout.customerid and                 dbo.t_subsidynotputout.[month] = dbo.t_subsidyputout.[month] if (exists(select * from t_subsidynotputout     where ([email protected] and [month] = @month))) begin    delete from t_subsidynotputout    where ([email protected] and [month] = @month)    end else   begin     print '没有相关客户的相关补助发放冲突月份!或补助冲突月份已经清理完毕!'     break   end end   /**/ while 1=1 begin          select @id=t_subsidymonthplan.customerid ,@month=dbo.t_subsidymonthplan.[month]          from dbo.t_subsidymonthplan inner join                dbo.t_subsidyputout on                dbo.t_subsidymonthplan.customerid = dbo.t_subsidyputout.customerid and                 dbo.t_subsidymonthplan.[month] = dbo.t_subsidyputout.[month] if (exists(select * from t_subsidymonthplan     where ([email protected] and [month] = @month))) begin    delete from t_subsidymonthplan    where ([email protected] and [month] = @month)    end else   begin     print '没有相关客户的本月补助冲突!或本月补助冲突已经清理完毕!'     break   end end

/*清除冲突补助月份的t_sql代码结束*/

set @month='2004-9-1' /*补助的月份,根据实际定*/set @cardtype=4       /*卡的类别,根据实际定*/   select @k=count(*),@subsidyfare=sum(subsidy) from t_subsidypre where [email protected] and [email protected] /*统计当月补助计划人数及金额,以t_subsidypre帐本为准*/if exists(select * from t_subsidymonth where [email protected] and [email protected] and [email protected] and [email protected])   /*判断当月补助计划数及金额是否正确*/ begin   select @k1=count(*),@subsidyfare1=sum(subsidy) from t_subsidymonthplan where   [email protected] and [email protected] /*统计当月补助未发人数及金额*/   if @subsidyfare1 is null    begin      set @subsidyfare1=0    end   set @[email protected]@k1    set @[email protected]@subsidyfare1   update t_subsidymonth set [email protected],[email protected] where [email protected]    and    [email protected] /*当月已发人数及金额=当月补助计划人数及金额-当月补助未发人数及金额*/   print '更改当月的补助信息完成!' endelse begin  print '计划总数不一致!'  end

select @k=count(*),@subsidyfare=sum(subsidy) from t_subsidynotputout where [email protected] and month<@month /*统计前期补助未发人数及金额*/select @k1=planprecount,@subsidyfare1=planpre from t_subsidymonth where [email protected] and [email protected] /*统计当月前期补助计划人数及金额*/set @[email protected]@kset @[email protected]@subsidyfareif @subsidyfare2 is null    begin      set @subsidyfare2=0    endupdate t_subsidymonth set [email protected],[email protected] where [email protected] and [email protected] /*当月前期已发人数及金额=当月前期补助计划人数及金额-前期补助未发人数及金额*/print '更改当月的前期补助信息完成!'-------------------------------------------------------------------------------------------------           /*清除管理费的触发器及生成t_mngfarelog表的脚本*/create trigger tr_mngfarelog on t_incomerec for insert,update as if exists(select * from t_incomerec where mngfare>0)    begin      declare @statid tinyint,@mealid tinyint,@port tinyint,@term tinyint,         @sumdt datetime,@incomefare money,@mngfare money,@avginc money,         @incomecount int      select @statid=statid,@mealid=mealid,@port=port,@term=term,      @sumdt=sumdt, @incomefare=incomefare,@incomecount=incomecount,      @mngfare=mngfare from t_incomerec where mngfare>0      update t_incomerec set mngfare=0 where [email protected] and [email protected] and             [email protected] and [email protected] and [email protected]      set @[email protected]/@incomecount      if @avginc>5/*平均消费值,根据实际定*/        begin          update t_incomerec set incomefare=4.5*incomecount where          [email protected] and [email protected] and [email protected] and [email protected] and [email protected]        end        insert into t_mngfarelog values(@statid,@mealid,@port,@term,                  @sumdt,@incomefare,@incomecount,@mngfare)      end go

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[t_mngfarelog]') andobjectproperty(id, n'isusertable') = 1) /*生成t_mngfarelog表的脚本*/ drop table [dbo].[t_mngfarelog] go

create table [dbo].[t_mngfarelog] (     [statid] [tinyint] not null ,     [mealid] [tinyint] not null ,     [port] [tinyint] not null ,     [term] [tinyint] not null ,     [sumdt] [datetime] not null ,     [incomefare] [money] not null ,     [incomecount] [int] not null ,     [mngfare] [money] null ) on [primary] go 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表