惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
博客园 - Franky
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
爱范儿
爱范儿
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
Microsoft Security Blog
Microsoft Security Blog
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)

博客园 - stefanie

PowerShell-将CSV导入SQL Server [Powershell] 检查IIS设置 VBS 操作注册表 十六进制 无法在People Picker中选择用户 Microsoft Certification List WINDOWS 2012忘记密码之后。。。 Auto Install Workflow Manager 1.0 批量删除Kindle Personal Documents 导入Excel表格到SharePoint站点 用户中心 - 博客园 [Powershell] FTP Download File [PowerShell] Backup Folder and Files Across Network SSRS 请求并显示SharePoint人员和组字段 VS 2010 安装 .net framework2.0/3.0/3.5 How to Setup AssociatedOwnerGroup / AssociatedMemberGroup / AssociatedVisitorGroup RPC服务不可用 [转]SharePoint Versions 安装 64 位版本的 Office 2010 后,无法查看数据表视图中的列表 [MOSS]关闭弹出窗口
SQL - 获取多机构最近相同节点
stefanie · 2018-10-16 · via 博客园 - stefanie
-- 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