很郁闷的一个看似很难的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 类的写法,谢放大家.
...全文
180 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
  • 打赏
  • 举报
回复
是谢谢大家,打错了.
内容概要:本研究聚焦于基于价格型需求响应的配电网与微电网优化问题,提出了一种结合多元宇宙优化算法(Multi-Verse Optimizer, MVO)的数学模型,用于求解考虑用户用电行为响应的电力系统优化调度问题。该方法通过构建电价与负荷之间的弹性响应关系,充分调动用户侧的调节潜力,在保证系统安全稳定运行的前提下,实现削峰填谷、降低网损、提高可再生能源消纳能力以及提升整体运行经济性的多重目标。研究在典型测试系统(如IEEE33节点)上进行了仿真验证,展示了所提算法在收敛速度、全局寻优能力和调度方案有效性方面的优越性能,适用于多主体、多目标、强非线性的现代主动配电网环境。; 适合人群:具备电力系统基础、优化算法背景及MATLAB编程能力的研究生、科研人员及电力行业相关技术人员,尤其适合从事智能电网、需求响应、分布式能源优化等领域研究的专业人士。; 使用场景及目标:① 掌握价格型需求响应机制在配电网优化中的建模方法;② 学习多元宇宙优化算法在电力系统调度问题中的具体应用与实现流程;③ 复现并改进高水平科研论文(如SCI/EI期刊)中的优化模型,服务于学位论文或科研项目开发。; 阅读建议:此资源以MATLAB代码为核心载体,强调理论与实践相结合,建议读者在理解需求响应经济学原理和MVO算法机制的基础上,仔细研读代码逻辑,进行参数敏感性分析与不同场景的仿真实验,以深化对智能优化算法解决实际工程问题的理解与应用能力。

22,296

社区成员

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

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