sql多表求和
㈠ sql语句两个表的查找求和
select
表1.地方
as
地方,表1.一月金额+表2.二月金额
as
总金额
from
表1,表2
where
表1.地方=表2.地方
修改后如下这个比较合理:
select
表1.地方,一月金额+二月金额
as
总金额
from
表1
,表2
where
表1.地方=表2.地方
union
select
'总金额',sum(一月金额+二月金额)
from
表1
,表2
where
表1.地方=表2.地方
㈡ sql 多表关联 求和语句 怎么写
你结果显示有问题吧,最后id=2怎么来的?
创建表:
createtabletable1
(idint,
类别varchar(10),
货号varchar(3))
insertintotable1values(1,'电子','011')
insertintotable1values(2,'零件','022')
insertintotable1values(3,'主板','033')
createtabletable2
(idint,
货号varchar(3),
数量int)
insertintotable2values(1,'011',5)
insertintotable2values(2,'022',6)
insertintotable2values(3,'033',-8)
insertintotable2values(4,'011',22)
insertintotable2values(5,'022',65)
insertintotable2values(6,'033',81)
查询:
selecta.id,a.类别,a.货号,SUM(b.数量)
fromtable1a,table2bwherea.货号=b.货号anda.货号='011'
groupbya.id,a.类别,a.货号
结果:
㈢ SQL两个表求和语句用什么。
select 表1.地方 as 地方,表1.一月金额+表2.二月金额 as 总金额
from 表1,表2
where 表1.地方=表2.地方
修改后如下这个比较合理:
select 表1.地方,一月金额+二月金额 as 总金额
from 表1 ,表2
where 表1.地方=表2.地方
union
select '总金额',sum(一月金额+二月金额)
from 表1 ,表2
where 表1.地方=表2.地方
㈣ 求问:sql中多表关联查询求和,主表的数据根据副表的数据重复相加了,等于数据翻倍,这种请怎么处理
首先,我怀疑你or没用明白,该括号括起来的地方没括起来。
然后,你可以先把a表和d表分别汇总,然后再关联起来。
selectsum(d.go),sum(a.letter1),a.region_id1,d.region_id
from
(selectregion_id1,platform_id1,time1,sum(a.letter1)letter1fromdwdswhere`time1`_id1,platform_id1,time1)a
rightjoin
(selectregion_id,platform_id,time,sum(go)gofrompowerwheretime_id,platform_id,time)d
ONd.platform_id=a.platform_id1ANDd.time=a.time1
groupbya.region_id1,d.region_id
另外,你a.region_id1和d.region_id是相等的,你select里都查出来干嘛?
㈤ SQL语句:多表查询并计算再求和
try this one
SELECT SUM(t1.totalTime* b.时薪)
FROM (SELECT 人员ID, SUM(工作时间编号) AS totalTime FROM A GROUP BY 人员ID) t1, B
WHERE t1.人员ID = B.人员ID
㈥ sql 两个表记录数求和
常用写法
SELECTSUM(ROWNO)FROM(
SELECTCOUNT(1)ROWNOFROMT1
UNIONALL
SELECTCOUNT(1)ROWNOFROMT2
)
也可以这样写,不用UNION ALL,省去要字段别名一致的麻烦。
SELECT(SELECTCOUNT(1)FROMFILE)+(SELECTCOUNT(1)FROMUSER)TOTAL;
㈦ sql 多表查询求和
建表a(插入数据过程略)
create table a
(mingzi varchar2(50),
id int);
㈧ sql两个表数据求和
楼上的想法是这样,先把俩张表的数据都查出来,使用union关键字,相应列使用同样的同名。 这样可以把俩张表当成一张表来操作,应该是可行的。
select t.name , t.brand , t.type, t.package , sum(t.totalcount), sum(t.weight) from (
select 商品名称1 as name , 商品品牌1 as brand , 商品型号1 as type, 商品包装1 as package , 商品数量1 as totalcount, 商品重量1 as weight from 商品表1 union all
select 商品名称2 as name , 商品品牌2 as brand , 商品型号2 as type, 商品包装2 as package , 商品数量2 as totalcount, 商品重量2 as weight from 商品表2
) t group by t.name ,t.brand , t.type, t.package
但是我不明白的是,你这是俩张表吗,这是什么样的两张表。。。 完全一样的列,完全一样的类型,干吗要成两张表。
㈨ sql server 多表之间求和
--假设JE_EmpID就是员工ID的话,统计的语句如下
selectJE_EmpID,SUM(JE_JiFen)asJE_JiFenfrom
(
selectJE_EmpID,JE_JiFenfromJF_'2015-01-01'and'2015-01-31'
unionall
selectJE_EmpID,JE_JiFenfromJF_OnLinewhereCreateTimebetween'2015-01-01'and'2015-01-31'
……
)t1groupbyJE_EmpID