很郁闷的一个看似很难的SQL查询,各位给想想,谢放

no_mIss 2007-01-16 02:14:52
具体业务太麻烦,我提供模拟业务表如下:
第一个表:
create table T_test
(id int identity(1,1) primary key,
a int,b int ,c int ,d int, e int ,f int )
insert into t_test
select 1,2,3,3,5,6 union all
select 1,2,3,3,5,6 union all
select 1,2,3,4,5,6 union all
select 1,3,3,3,5,6 union all
select 1,3,3,3,5,6

第二个表:
create table T_test2
(
id int identity(1,1) primary key,
a int,g int,b int
)
insert into T_test2
select 1,2,2 union all
select 1,1,2 union all
select 1,2,2 union all
select 1,2,3 union all
select 1,2,3

要的结果用数据说吧:
第一个表:id,a,b,c,d,e,f
1 1 2 3 3 5 6
2 1 2 3 3 5 6
3 1 2 3 4 5 6
4 1 3 3 3 5 6
5 1 3 3 3 5 6
第二个表:id,a,g,b
1 1 2 2
2 1 1 2
3 1 2 2
4 1 2 3
5 1 2 3

我要的结果第一个表按a,b分组:
分别为:a,b,(第一个表按a,b分组后的count),sum(e),sum(f),(第二个表按a,b分组后,不同a,g组合的count)
1 2 2 15 18 2
1 3 1 10 12 1

目前我写出了一种方法:
select distinct a,b,
(select count(*) from (select distinct c,d from T_test where a=t.a and b=t.b)a)as coun,
(select sum(e) from T_test where a=t.a and b=t.b) as sum_e,
(select sum(f) from T_test where a=t.a and b=t.b) as sum_f,
(select count(*) from (select distinct a,g from T_test2 where a=t.a and b=t.b)a)as coun2
from T_test t

但效率不高,所以请教用group by 类的写法,谢放大家.
...全文
160 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
no_mIss 2007-01-16
  • 打赏
  • 举报
回复
libin_ftsafe 还是有问题,最后的那一列
marco08 的可以了
我仔细看看
谢谢你们
python二级题库 2007-01-16
  • 打赏
  • 举报
回复
xue xi!
子陌红尘 2007-01-16
  • 打赏
  • 举报
回复
maybe:

select
isnull(a.a,b.a) as a,
isnull(a.b,b.b) as b,
a.cnt1,
a.e,
a.f,
b.cnt2
from
(select a,b,count(*) as cnt1,sum(e) as e,sum(f) as f from T_test group by a,b) a
full outer join
(select a,b,count(*) as cnt2 from T_test2 group by a,b) b
on
a.a=b.a and a.b=b.b
marco08 2007-01-16
  • 打赏
  • 举报
回复
--錯了, 改改
select tmpA.*, tmpB.[count] from
(
select a, b, [count]=count(*), e_sum=sum(e), f_sum=sum(f) from T_test
group by a, b
)tmpA
left join
(
select a, b, [count]=(select count(distinct g) from T_test2 where a=A.a and b=A.b)
from T_test2 as A
group by
a, b
)tmpB on tmpA.a=tmpB.a and tmpA.b=tmpB.b

--result
a b count e_sum f_sum count
----------- ----------- ----------- ----------- ----------- -----------
1 2 3 15 18 2
1 3 2 10 12 1

(2 row(s) affected)
no_mIss 2007-01-16
  • 打赏
  • 举报
回复
太感谢了
libin_ftsafe的结果好象有点问题
marco08(天道酬勤)应该对了
我试试
子陌红尘 2007-01-16
  • 打赏
  • 举报
回复
create table T_test
(id int identity(1,1) primary key,
a int,b int ,c int ,d int, e int ,f int )
insert into t_test
select 1,2,3,3,5,6 union all
select 1,2,3,3,5,6 union all
select 1,2,3,4,5,6 union all
select 1,3,3,3,5,6 union all
select 1,3,3,3,5,6

create table T_test2
(
id int identity(1,1) primary key,
a int,g int,b int
)
insert into T_test2
select 1,2,2 union all
select 1,1,2 union all
select 1,2,2 union all
select 1,2,3 union all
select 1,2,3
go


select
a.*,b.cnt2
from
(select a,b,count(*) as cnt1,sum(e) as e,sum(f) as f from T_test group by a,b) a,
(select a,b,count(*) as cnt2 from T_test2 group by a,b) b
where
a.a=b.a and a.b=b.b
go


/*
a b cnt1 e f cnt2
----------- ----------- ----------- ----------- ----------- -----------
1 2 3 15 18 3
1 3 2 10 12 2
*/

drop table t_test,t_test2
go
marco08 2007-01-16
  • 打赏
  • 举报
回复
select tmpA.*, tmpB.[count] from
(
select a, b, [count]=count(*), e_sum=sum(e), f_sum=sum(f) from T_test
group by a, b
)tmpA
left join
(
select a, b, [count]=(select top 1 count(*) from T_test2 where a=A.a and b=A.b group by a,g) from T_test2 as A
group by
a, b
)tmpB on tmpA.a=tmpB.a and tmpA.b=tmpB.b

--result
a b count e_sum f_sum count
----------- ----------- ----------- ----------- ----------- -----------
1 2 3 15 18 1
1 3 2 10 12 2

(2 row(s) affected)
no_mIss 2007-01-16
  • 打赏
  • 举报
回复
to marco08(天道酬勤)
感谢,我不是想要这个结果
marco08 2007-01-16
  • 打赏
  • 举报
回复
select a, b, [count]=count(*), e_sum=sum(e), f_sum=sum(f) from T_test
group by a, b
no_mIss 2007-01-16
  • 打赏
  • 举报
回复
是谢谢大家,打错了.
VR(Virtual Reality)即虚拟现实,是一种可以创建和体验虚拟世界的计算机技术。它利用计算机生成一种模拟环境,是一种多源信息融合的、交互式的三维动态视景和实体行为的系统仿真,使用户沉浸到该环境中。VR技术通过模拟人的视觉、听觉、触觉等感觉器官功能,使人能够沉浸在计算机生成的虚拟境界中,并能够通过语言、手势等自然的方式与之进行实时交互,创建了一种适人化的多维信息空间。 VR技术具有以下主要特点: 沉浸感:用户感到作为主角存在于模拟环境中的真实程度。理想的模拟环境应该使用户以分辨真假,使用户全身心地投入到计算机创建的三维虚拟环境中,该环境中的一切看上去是真的,听上去是真的,动起来是真的,甚至闻起来、尝起来等一切感觉都是真的,如同在现实世界中的感觉一样。 交互性:用户对模拟环境内物体的可操作程度和从环境得到反馈的自然程度(包括实时性)。例如,用户可以用手去直接抓取模拟环境中虚拟的物体,这时手有握着东西的感觉,并可以感觉物体的重量,视野中被抓的物体也能立刻随着手的移动而移动。 构想性:也称想象性,指用户沉浸在多维信息空间中,依靠自己的感知和认知能力获取知识,发挥主观能动性,寻求解答,形成新的概念。此概念不仅是指观念上或语言上的创意,而且可以是指对某些客观存在事物的创造性设想和安排。 VR技术可以应用于各个领域,如游戏、娱乐、教育、医疗、军事、房地产、工业仿真等。随着VR技术的不断发展,它正在改变人们的生活和工作方式,为人们带来全新的体验。

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧