zoukankan      html  css  js  c++  java
  • sql server 循环算法

    sql <wbr>server <wbr>循环算法

    sql <wbr>server <wbr>循环算法



    sql <wbr>server <wbr>循环算法

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

    对账查询

     

    select a.oTime,a.Attribution,a.Operator,a.PackageName,olinecount,ISNULL( newcount,0)
    as newcount,a.PackagePrice,((olinecount + ISNULL( newcount,0))* a.PackagePrice) as amount
    from
    (select COUNT(m.Mobile) olinecount,m.oTime,(nb.Province+nb.City) as Attribution,
    nb.Operator,m.Package as PackageCode,d.PackageName,d.PackagePrice
    from AndroidWS.dbo.AWS_StateAll m LEFT JOIN
    AndroidWS.dbo.Aws_Package_Dict d
    on m.package=d.PackageCode
    LEFT JOIN
    BussinessAnalyse.dbo.NumberSegment nb ON SUBSTRING(m.Mobile,0,8)=nb.Segment
    GROUP BY m.oTime,nb.Province,nb.City,nb.Operator,m.Package,d.PackageName,d.PackagePrice) a left join
    (select COUNT(*) as newcount ,oline,package from (select mobile,package,MIN(oTime) oline from  AndroidWS.dbo.AWS_StateAll
          group by package,mobile ) mintime group by oline,package )b on a.PackageCode=b.package and a.otime=b.oline 
           order by oTime desc,a.PackageCode desc

     

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

    sql <wbr>server <wbr>循环算法

     

    ----------------------------------------------
    查看明细

    SELECT Convert(varchar(4),YEAR(uu.t1))+'-'+Convert(varchar(2),MONTH(uu.t1)), nb.Province + nb.City AS Attribution, nb.Operator,  u.UserHomePhone

    AS UserMobile,  d.PackageName,
     uu.t1 AS OpenTime
    FROM         (SELECT     a.UserID, a.PaCode, a.CreateDate AS t1, b.CreateDate AS t2
                           FROM          (SELECT     ID, UserID, CreateDate, Status, PaCode, Remark, SpNumber
                                                   FROM          dbo.Aws_Pakeage_Order_list
                                                   WHERE      (Remark = '开通')) AS a LEFT OUTER JOIN
                                                      (SELECT     ID, UserID, CreateDate, Status, PaCode, Remark,

    SpNumber
                                                        FROM          dbo.Aws_Pakeage_Order_list AS

    Aws_Pakeage_Order_list_1
                                                        WHERE      (Remark = '退订')) AS b ON a.UserID = b.UserID AND

    a.PaCode = b.PaCode AND a.CreateDate < b.CreateDate) AS uu LEFT OUTER JOIN
                          dbo.Aws_Package_Dict AS d ON uu.PaCode = d.PackageCode LEFT OUTER JOIN
                          dbo.AWS_User AS u ON uu.UserID = u.UserId LEFT OUTER JOIN
                          BussinessAnalyse.dbo.NumberSegment AS nb ON SUBSTRING(u.UserHomePhone, 0, 8) = nb.Segment
    GROUP BY   uu.t1, nb.Operator, nb.City, nb.Province, d.PackageName, u.UserHomePhone
    ORDER BY   uu.t1, nb.Operator, nb.City, nb.Province, d.PackageName, u.UserHomePhone

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



    sql <wbr>server <wbr>循环算法

     

     

    ----------------------------------------------------------------------
    视图

    SELECT     TOP (100) PERCENT u.UserHomePhone AS UserMobile, uu.PaCode AS PackageCode, uu.t1 AS OpenTime, MIN(uu.t2) AS CloseTime
    FROM         (SELECT     a.UserID, a.PaCode, a.CreateDate AS t1, b.CreateDate AS t2
                           FROM          (SELECT     ID, UserID, CreateDate, Status, PaCode, Remark, SpNumber
                                                   FROM          dbo.Aws_Pakeage_Order_list
                                                   WHERE      (Remark = '开通')) AS a LEFT OUTER JOIN
                                                      (SELECT     ID, UserID, CreateDate, Status, PaCode, Remark, SpNumber
                                                        FROM          dbo.Aws_Pakeage_Order_list AS Aws_Pakeage_Order_list_1
                                                        WHERE      (Remark = '退订')) AS b ON a.UserID = b.UserID AND a.PaCode = b.PaCode AND a.CreateDate < b.CreateDate) AS uu LEFT OUTER JOIN
                          dbo.AWS_User AS u ON uu.UserID = u.UserId
    GROUP BY uu.PaCode, uu.t1, u.UserHomePhone
    ORDER BY PackageCode, OpenTime, UserMobile

    ----------------------------------
    游标

    use AndroidWS
    declare @mobile varchar(50)
    declare @packagecode varchar(50)
    declare @opentime DateTime
    declare @closetime DateTime
    declare @str varchar(20)

    declare cur Cursor FORWARD_ONLY   For --声明一个游标

    select  UserMobile,PackageCode,OpenTime,
    case when CloseTime IS null then GETDATE()
    when CloseTime  IS not null then CloseTime
    end as offtime
    from AWS_Business_State

    open cur
    fetch next from cur into @mobile,@packagecode,@opentime,@closetime--把游标信息放到局部变量中
    while(@@fetch_status=0)
    begin
     while(DATEDIFF(month,@opentime,@closetime)>=0) 
     begin
      if(MONTH(@opentime)<10)
       set @str=Convert(varchar(4),YEAR(@closetime))+'-0'+Convert(varchar(2),MONTH(@closetime))
      else
       set @str=Convert(varchar(4),YEAR(@closetime))+'-'+Convert(varchar(2),MONTH(@closetime))
      if not exists(
       select * from AWS_StateAll where Mobile=@mobile and Package=@packagecode and oTime=@str
      )
      begin
       insert into AWS_StateAll(Mobile,Package,oTime) values(@mobile,@packagecode,@str)
      end
      else
      begin
       break
      end
      set @closetime=dateadd(month,-1,@closetime)
     end
     fetch next from cur into @mobile,@packagecode,@opentime,@closetime--把游标信息放到局部变量中,提前准备数据
    end
    close cur
    DEALLOCATE cur

    go

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


    sql <wbr>server <wbr>循环算法

     


    sql <wbr>server <wbr>循环算法

     

    -------------------------------------
    用户收入查询

    select m.Mobile,(n.Province+n.City) as Attribution,
    n.Operator,SUM(v.num) n,SUM(v.price) p
    FROM
    (select Mobile from AndroidWS.dbo.AWS_StateAll mp group by
     mp.Mobile) m  
    left join 
    (select Package,Mobile,COUNT(*) num,COUNT(*)*d.PackagePrice price from AndroidWS.dbo.AWS_StateAll a left join
     AndroidWS.dbo.Aws_Package_Dict d
               on a.Package=d.PackageCode group by Package,Mobile,PackagePrice) v on m.Mobile=v.Mobile left join
               BussinessAnalyse.dbo.NumberSegment n on Substring(m.Mobile,1,7)=n.Segment
               group by m.Mobile,n.Province,n.City,n.Operator;


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

     

  • 相关阅读:
    Go通过闭包避免程序运行崩溃
    占个坑
    Go多核并行
    一个计划
    python入门:模拟简单用户登录(自写)
    python入门:if、elif、else 条件语句的基本用法
    python入门:if和else的基本用法
    python入门:print打印输出的用法
    python入门:最基本的用户登录
    Python入门:Python基础笔记
  • 原文地址:https://www.cnblogs.com/liuzhuqing/p/7480579.html
Copyright © 2011-2022 走看看