zoukankan      html  css  js  c++  java
  • SQL

    -- Create Branches Table
    create table Branches
    (
        BranchCode varchar(16)
        ,BranchName nvarchar(32)
        ,L0BCode varchar(16)
        ,L1BCode varchar(16)
        ,L2BCode varchar(16)
        ,L3BCode varchar(16)
        ,L4BCode varchar(16)
        ,L5BCode varchar(16)
        ,L6BCode varchar(16)
        ,L7BCode varchar(16)
    )
    go
    
    
    -------------------------------------------------------------------------------------------
    
    declare @branches varchar(512) = '02078,31696,90100'
            ,@count int
            ,@branchcode varchar(16)
    
    select @count = COUNT(*) from string_split(@branches,',') 
    
    ;with t1
    as
    (
    select b.L0BCode,b.L1BCode,b.L2BCode,b.L3BCode,b.L4BCode,b.L5BCode,b.L6BCode,b.L7BCode
    ,iif(isnull(b.L0BCode,'') = '',@count,dense_rank()over(order by b.L0BCode ))  as L0Rank
    ,iif(isnull(b.L1BCode,'') = '',@count,dense_rank()over(order by b.L1BCode ))  as L1Rank
    ,iif(isnull(b.L2BCode,'') = '',@count,dense_rank()over(order by b.L2BCode ))  as L2Rank
    ,iif(isnull(b.L3BCode,'') = '',@count,dense_rank()over(order by b.L3BCode ))  as L3Rank
    ,iif(isnull(b.L4BCode,'') = '',@count,dense_rank()over(order by b.L4BCode ))  as L4Rank
    ,iif(isnull(b.L5BCode,'') = '',@count,dense_rank()over(order by b.L5BCode ))  as L5Rank
    ,iif(isnull(b.L6BCode,'') = '',@count,dense_rank()over(order by b.L6BCode ))  as L6Rank
    ,iif(isnull(b.L7BCode,'') = '',@count,dense_rank()over(order by b.L7BCode ))  as L7Rank
    from 
        Branches b
            inner join
                string_split(@branches,',')  b2
                    on
                        b.BranchCode = b2.value
    )
    
    ,t2
    as
    (
    select 
        top 1 * 
    from
    (
        select 
            b.L0BCode,b.L1BCode,b.L2BCode,b.L3BCode,b.L4BCode,b.L5BCode,b.L6BCode,b.L7BCode
            ,sum(L0Rank)over(partition by 1) as L0Sum
            ,sum(L1Rank)over(partition by 1) as L1Sum
            ,sum(L2Rank)over(partition by 1) as L2Sum
            ,sum(L3Rank)over(partition by 1) as L3Sum
            ,sum(L4Rank)over(partition by 1) as L4Sum
            ,sum(L5Rank)over(partition by 1) as L5Sum
            ,sum(L6Rank)over(partition by 1) as L6Sum
            ,sum(L7Rank)over(partition by 1) as L7Sum
        from t1 as b
    ) as b
    order by b.L7BCode desc,b.L6BCode desc,b.L5BCode desc,b.L4BCode desc,b.L3BCode desc,b.L2BCode desc,b.L1BCode desc
    )
    
    select 
        @branchcode = 
        (
            case 
                when L7Sum = @count then L7BCode
                when L6Sum = @count then L6BCode
                when L5Sum = @count then L5BCode
                when L4Sum = @count then L4BCode
                when L3Sum = @count then L3BCode
                when L2Sum = @count then L2BCode
                when L1Sum = @count then L1BCode
                when L0Sum = @count then L0BCode
            else
                L0BCode
            end
        )
        
    from t2
    
    
    select @branchcode
  • 相关阅读:
    [原]openstack-kilo--issue(十一)Failed connect to 169.254.169.254:80; No route to host
    [转]pycharm active code
    [原]openstack-kilo--issue(九) heat stacks topology中图形无法正常显示
    [原]CentOS 7 网卡子接口的创建
    [转]Shell中read的常用方式
    [转]输出带颜色的shell
    第十七节:从状态机的角度async和await的实现原理(新)
    第十六节:时隔两年再谈异步及深度剖析async和await(新)
    第十一节:SQLServer事务写法、各种锁、事务隔离级别
    第十七节:SQL中的一些常用SQL积累(持续更新)
  • 原文地址:https://www.cnblogs.com/wpsl5168/p/9800436.html
Copyright © 2011-2022 走看看