简单select问题

cczeyun 2009-09-10 11:14:08
A表
id,name,type
1 a d
2 b d

b表
id cost1 cost2
1 23.5 10
1 22 12
1 33 10
2 23 15
2 45 16

结果:
如果id=1显示如下结果:
id name type cost
1 a d 总成本

这样的怎么写
...全文
281 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
feixianxxx 2009-09-14
  • 打赏
  • 举报
回复


我是来围观的
chen_ya_ping 2009-09-14
  • 打赏
  • 举报
回复

create table #temp
(
id int not null,
cost1 float not null,
cost2 int not null,
)

select id,sum(cost1),sum(cost2)
from B
group by id
into #temp

select A.id,A.name,A.type sum(#temp.cost1+#temp.cost2)
from A inner join #temp on A.id=#temp.id
lihan6415151528 2009-09-14
  • 打赏
  • 举报
回复

select a.*,cost=sum(b.cost1+b.cost2)
from a,b where a.id=b.id
group by a.id,a.name,a.type
随行的太阳 2009-09-14
  • 打赏
  • 举报
回复
学些了
ahbbyu 2009-09-13
  • 打赏
  • 举报
回复
楼上都有了,不写了。
guguda2008 2009-09-13
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 zsqsc 的回复:]
SQL codeCreatetable #a(
idint,[name]varchar(10),[type]varchar(10)
)insertinto #aselect1,'a','b'unionallselect2,'b','d'createtable #b(
idint,
cost1float,
cost2float
)insertinto #bselect1,23.5,10unionallselect1,22,12unionallselect1,33,10unionallselect2,23,15unionallselect2,45,16--原表--#a1 a b2 b d


#b123.51012212133102231524516select a.id,a.[Name],a.[Type],b.costfrom (select id,sum(cost1*cost2) costfrom #bgroupby id) bleftjoin #a aon b.id=a.id--结果---1 a b8292 b d1065
[/Quote]
这位仁兄真是执着,三天前的问题还能翻出来
xuejiecn 2009-09-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 guguda2008 的回复:]
LS全是正解,我就不复制了。
[/Quote]
我也支持。
影帆 2009-09-13
  • 打赏
  • 举报
回复

Create table #a(
id int,
[name] varchar(10),
[type] varchar(10)
)

insert into #a
select 1,'a','b' union all
select 2,'b','d'


create table #b(
id int,
cost1 float,
cost2 float
)

insert into #b
select 1,23.5,10 union all
select 1,22,12 union all
select 1,33,10 union all
select 2,23,15 union all
select 2,45,16


--原表--
#a
1 a b
2 b d


#b
1 23.5 10
1 22 12
1 33 10
2 23 15
2 45 16



select a.id,a.[Name],a.[Type],b.cost
from (select id,sum(cost1*cost2) cost
from #b
group by id) b left join #a a on b.id=a.id


--结果---

1 a b 829
2 b d 1065

cxmcxm 2009-09-13
  • 打赏
  • 举报
回复
select a.id,a.name,a.type,b.cost 
from a,
(select id,sum(cost1+cost2) cost from b where id=1 group by id) b
whre a.id=b.id
coolyayaya 2009-09-13
  • 打赏
  • 举报
回复
学习了。。
  • 打赏
  • 举报
回复


select id,name,type,(select sum(cost1+cost2))as cost
from a inner join b
where a.id=b.id

试一试 是不是这样更简单
panglin3688 2009-09-13
  • 打赏
  • 举报
回复
LS 都是对的
tree9 2009-09-13
  • 打赏
  • 举报
回复
楼上均正确
小宏 2009-09-13
  • 打赏
  • 举报
回复
1 a d 110.5
2 b d 99.0
小宏 2009-09-13
  • 打赏
  • 举报
回复

if object_id('[A]') is not null drop table [A]
go
create table [A]([id] int,[name] varchar(1),[type] varchar(1))
insert [A]
select 1,'a','d' union all
select 2,'b','d'
if object_id('[b]') is not null drop table [b]
go
create table [b]([id] int,[cost1] numeric(3,1),[cost2] int)
insert [b]
select 1,23.5,10 union all
select 1,22,12 union all
select 1,33,10 union all
select 2,23,15 union all
select 2,45,16

select a.* , b.cost
from A a inner join
(select id , sum(cost1 + cost2) as cost from b group by id)b
on a.id = b.id
zhaodong1014 2009-09-12
  • 打赏
  • 举报
回复
楼上都正!
jayqean 2009-09-10
  • 打赏
  • 举报
回复
呵呵 楼上都写出来了```
我就不写了
silentwins 2009-09-10
  • 打赏
  • 举报
回复
--> 测试数据:@A
declare @A table([id] int,[name] varchar(100),[type] varchar(100))
insert @A
select 1,'a','d' union all
select 2,'b','d'
--> 测试数据:@B
declare @B table([id] int,[cost1] numeric(3,1),[cost2] int)
insert @B
select 1,23.5,10 union all
select 1,22,12 union all
select 1,33,10 union all
select 2,23,15 union all
select 2,45,16

select a.id,sum(cost1+cost2) as total from @A a inner join @B b on a.id = b.id where a.id = '1' group by a.id

/*
id total
----------- ----------------------------------------
1 110.5

(1 row(s) affected)
*/
ming_Y 2009-09-10
  • 打赏
  • 举报
回复
select * from b
drop table b
去掉!
ming_Y 2009-09-10
  • 打赏
  • 举报
回复
create table a (id int,name varchar(12),type varchar(10))
insert into a
select '1','a','d' union all
select '2','b','d'

create table b (id int,cost1 float(5),cost2 float(5))
insert into b
select '1','23.5','10' union all
select '1','22','12' union all
select '1','33','10' union all
select '2','23','15' union all
select '2','45','16'

select * from b
drop table b

select a.id,a.name,a.type,d.totalcost from a,(select id,sum(cost) totalcost from (select id,cost=cost1+cost2 from b) c group by id) d
where a.id=d.id
加载更多回复(21)
内容概要:本文针对无刷直流电机驱动的电子机械制动(EMB)执行器,建立了考虑Stribeck摩擦特性的非线性耦合动力学模型,并在Simulink环境中完成了系统级仿真分析。研究综合集成了电机动力学、齿轮传动机构与制动执行机构的动力学特性,构建了高保真的机电一体化系统模型。重点引入Stribeck摩擦模型以精确描述低速工况下执行器内部存在的静摩擦、粘滞摩擦与库仑摩擦之间的过渡行为,有效提升了系统在启停、反向运动等瞬态过程中的动态响应仿真精度。通过多工况仿真验证了模型的有效性,能够准确反映摩擦引起的爬行、滞后与定位误差等非线性现象,为EMB系统的高性能控制算法设计(如摩擦补偿、滑模控制)与结构优化提供了高可信度的仿真平台。; 适合人群:从事汽车电子制动系统、电机驱动控制、机电系统建模与仿真研究的研究生、科研人员及工程技术人员,需具备扎实的机械动力学、自动控制理论基础和MATLAB/Simulink仿真能力。; 使用场景及目标:①用于高精度电子机械制动系统的设计验证与性能预测;②为消除摩擦非线性影响的先进控制策略(如自适应控制、智能控制)提供精确的被控对象模型;③深入探究Stribeck摩擦等非线性因素对系统动态性能(如响应延迟、稳态误差)的作用机理; 阅读建议:读者应结合提供的Simulink模型文件,深入剖析Stribeck摩擦模块的数学实现与参数辨识方法,建议通过改变输入指令(如阶跃、正弦)和负载条件进行对比仿真,以直观理解非线性摩擦对系统动态特性的影响。

34,875

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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