统计交叉相等两列元祖的次数 去掉重复
-- 统计交叉相等两列元祖的次数 去掉重复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([num1] varchar(10),[num2] varchar(10))
insert [tb]
select 'a','b' union all
select 'b','c' union all
select 'b','a' union all
select 'c','b' union all
select 'c','d' union all
select * from tb
select t.num1,t.num2,count(*)as 重复
from (
select num1,num2 from tb
union all
select num2,num1 from tb
) t
where t.num1<t.num2
group by t.num1,t.num2
/*
num1 num2
a b
b c
b a
c b
c d
----------------------------
num1 num2 (重复)
a b 2
b c 2
c d 1
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([num1] varchar(10),[num2] varchar(10))
insert [tb]
select 'a','b' union all
select 'b','c' union all
select 'b','a' union all
select 'c','b' union all
select 'c','d' union all
select * from tb
select t.num1,t.num2,count(*)as 重复
from (
select num1,num2 from tb
union all
select num2,num1 from tb
) t
where t.num1<t.num2
group by t.num1,t.num2
/*
num1 num2
a b
b c
b a
c b
c d
----------------------------
num1 num2 (重复)
a b 2
b c 2
c d 1
*/
分类:
SQL Server
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战