很郁闷的一个看似很难的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 类的写法,谢放大家.
...全文
173 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
  • 打赏
  • 举报
回复
是谢谢大家,打错了.
本资源包收录了一套采用Python编程语言与Flask轻量级框架构建的考勤管理平台,作为一项获得优异评价的学术研究成果,其设计过程得到了专业教师的全面指导与正式认可。在最终答辩环节中,该系统的评审得分高达97分,体现了其在技术实现与学术规范方面的卓越表现。 项目文件经过系统化的环境适配测试,分别在macOS与Windows 10/11操作系统平台上完成了全面验证,确保用户获取后可直接部署运行。配套文档详细阐述了系统的安装流程与配置步骤,为使用者提供了完整的技术参考。 该考勤管理系统具备完善的员工出勤记录、请假审批及考勤统计等核心功能模块,采用模块化架构设计,代码结构清晰规范。除作为毕业设计参考外,该系统亦适用于计算机相关专业的课程实践教学,可作为软件工程、数据库原理等课程的配套实践项目。 资源包内包含完整的源代码集合与系统使用指南,所有技术文档均采用标准化格式编写。开发过程中严格遵循软件工程规范,确保了系统的可维护性与扩展性。各功能模块均经过多轮测试验证,保证了系统运行的稳定性与数据处理的准确性。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!

22,298

社区成员

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

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