外联统计问题 有点急 题已写好

wuxinyuzhu wuxinyuzhu 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

--这是一个常数表1
create table tab(id int, name varchar(20))
insert into tab
select 1,'aa'
union all select 2,'bb'
union all select 3,'cc'
union all select 4,'dd'

--这是常数表2
create table tab3(lx int,name2 varchar(20))
insert into tab3
select 0,'zhian'
union all select 1,'xingshi'
union all select 2,'minshi'
union all select 3,'qita'

--这是一个数据表 id等于常数表1的id,lx等于常数表2的lx
create table tab2(id int,name1 varchar(20),lx int)
insert into tab2
select 1,'x',0
union all select 1,'xxx',1
union all select 1,'xxx',2
union all select 2,'xxx',1
union all select 2,'xxx',1
union all select 4,'xxx',2
union all select 4,'xxx',2
union all select 4,'xxx',1
union all select 3,'xxx',3


--现在想通过联接的方法得出这样的结果
name name2 count
aa zhian 1
aa xingshi 1
aa minshi 1
aa qita 0
aa 合计 3
bb zhian 0
bb xingshi 2
bb minshi 0
bb qita 0
bb 合计 2
cc zhian 0
cc xingshi 0
cc minshi 0
cc qita 1
cc 合计 1
dd zhian 0
dd xingshi 1
dd minshi 2
dd qita 0
dd 合计 3
总计 null 9

...全文

相关推荐

评论 18
收藏 已收藏
分享
返回顶部
wuxinyuzhu
MS-SQL Server 34,877 成员 254,635 内容