• 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;


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

     

  • 相关阅读:
    无线安全
    下载安装Emacs和基本配置--待更新中
    uv-pv-vv的区别
    tesseract安装及问题处理
    POJ 2187 Beauty Contest【凸包周长】
    POJ 1113 Wall【凸包周长】
    POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
    POJ 2031 Building a Space Station【经典最小生成树】
    URAL 1181 Cutting a Painted Polygon【递归+分治】
    POJ 1845-Sumdiv【经典数学题目---求因子和】
  • 原文地址:https://www.cnblogs.com/liuzhuqing/p/7480579.html
Copyright © 2020-2023  润新知