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
SQL - 获取多机构最近相同节点的更多相关文章
- AngularJS SQL 获取数据
使用PHP从MySQL中获取数据: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- ztree获取当前选中节点子节点id集合的方法(转载)
本文实例讲述了ztree获取当前选中节点子节点id集合的方法.分享给大家供大家参考.具体分析如下: 要求:获取当前选中节点的子节点id集合. 步骤: 1.获取当前节点 2.用ztree的方法trans ...
- SQL获取所有数据库名、表名、储存过程以及参数列表
SQL获取所有数据库名.表名.储存过程以及参数列表 1.获取所有用户名:SELECT name FROM Sysusers where status='2' and islogin='1'islogi ...
- 在Oracle中使用sql获取数据库名称
在Oracle中使用sql获取当前数据库名称 select name from v$database;
- 将SQL获取的信息传递到Email中
将SQL获取的信息传递到Email中 最近在为公司财务开发一个邮件通知时遇到了一个技术问题.原来我设计SSIS的是每天将ERP系统支付数据导出到财务支付平台后 Email 通知财务,然后财务到支付平台 ...
- Zookeeper命令行操作(常用命令;客户端连接;查看znode路径;创建节点;获取znode数据,查看节点内容,设置节点内容,删除节点;监听znode事件;telnet连接zookeeper)
8.1.常用命令 启动ZK服务 bin/zkServer.sh start 查看ZK服务状态 bin/zkServer.sh status 停止ZK服务 bin/zkServer.sh stop 重启 ...
- Postgre Sql获取最近一周、一月、一年日期函数
使用Postgre Sql获取近一周.一年.一月等系统函数调用如下,使用方面相对于Ms Sql server 容易了许多. --当前时间 select now(); --current_timesta ...
- mysql My SQL获取某个表的列名
My SQL获取某个表的列名 DESC TableName SHOW COLUMNS FROM TableName SELECT COLUMN_NAME FROM information_schem ...
- 获取父窗口的xxx节点的方法
window.parent.document.getElementById("xxx");获取父窗口的xxx节点$("#myEle", window.paren ...
随机推荐
- 在iOS上实现一个简单的日历控件
http://blog.csdn.net/jasonblog/article/details/21977481 近期需要写一个交互有点DT的日历控件,具体交互细节这里略过不表. 不过再怎么复杂的控件, ...
- Kubernetes仓库搭建
#生成证书#/etc/pki/tls/openssl.cnf [ v3_ca ]下添加serviceIPsubjectAltName = 169.169.0.11#创建证书169.169.0.11为s ...
- 学习hibernate,这个系列很不错
从这里入,感谢作者啊. 看了很多资料,这个是最能让我入门的.感觉. http://blog.csdn.net/yerenyuan_pku/article/details/52745486
- AC日记——Two poj 1849
Two 思路: 树形DP求直径: 答案是边权总和*2-直径: dp[i][1]::以i为根的子树中最长的路径: dp[i][0]::以i为根的子树中次长的路径: 来,上代码: #include < ...
- Codeforces 954I Yet Another String Matching Problem(并查集 + FFT)
题目链接 Educational Codeforces Round 40 Problem I 题意 定义两个长度相等的字符串之间的距离为: 把两个字符串中所有同一种字符变成另外一种,使得两个 ...
- Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算
中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...
- codevs——1230 元素查找
时间限制: 1 s 空间限制: 128000 Ks 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数 ...
- Codeforces 920 E Connected Components?
Discription You are given an undirected graph consisting of n vertices and edges. Instead of giving ...
- springmvc使用StringHttpMessageConverter需要配置编码
Spring controller 如下 @Controller public class SimpleController { @ResponseBody @RequestMapping(value ...
- [置顶]
kubernetes资源对象--limitranges
概念 LimitRange(简称limits)基于namespace的资源管理,包括pod和container的最小.最大和default.defaultrequests等. 一旦创建limits,以 ...