数据是
TableA
ID Name Money OperateTime
01 小明 1 2011-9-15 13:24:00
01 小明 3 2011-9-15 13:25:00
01 小明 10 2011-9-15 15:19:00
TableC
PeronID PeronName Money2 Money3 LogTime
01 小明 5 1 2011-9-15 13:24:00
01 小明 3 3 2011-9-15 13:25:00
01 小明 2 7 2011-9-15 15:19:00
需要的结果是tableA的这几行数据和tableC的这几行数据通过用sum和视图的方式(或者用临时表也行)并成一行
13 个解决方案
#1
TableC
PeronID PeronName Money2 Money3 LogTime
01 小明 5 1 2011-9-15 13:24:00
01 小明 3 3 2011-9-15 13:25:00
01 小明 2 7 2011-9-15 15:19:00
PeronID PeronName Money2 Money3 LogTime
01 小明 5 1 2011-9-15 13:24:00
01 小明 3 3 2011-9-15 13:25:00
01 小明 2 7 2011-9-15 15:19:00
#2
楼主要说清楚是怎么个合并法!
#3
结出想要的结果是什么
#4
用3个SUM求出SUM(Money),SUM(Money2),SUM(Money3),再合并成一行
#5
请楼主直接给出想要的结果
#6
select a.id,a.name,a.sum_money1,b.sum_money2,b.sum_money3
from
(
select ID,Name,sum(money) as sum_money1
from tableA
group by ID,Name
) a inner join
(
select PeronID ,PeronName ,sum(money2) as sum_money2,sum(money3) as sum_money3
from tableC
group by PeronID ,PeronName
)b
on a.ID=b.PeronID and a.name=b.PeronName
#7
是不是有种联结之类的语句,有点忘了,inner join,outer join,简单的AND。
#8
我想要得出:
Id Name TotalMoney TotalMoney2 TotalMoney3 Operatetime
01 小明 14 10 11 2011-9-15
Id Name TotalMoney TotalMoney2 TotalMoney3 Operatetime
01 小明 14 10 11 2011-9-15
#9
select isnull(a.ID,b.PeronID) ID,isnull(a.Name,b.PeronName) Name,
sum(isnull(a.[Money],0)) [Money],sum(isnull(b.Money2,0)) Money2,
sum(isnull(b.Money3,0)) Money3
from tableA a full join TableC b on a.ID = b.PeronID
group by isnull(a.ID,b.PeronID),isnull(a.Name,b.PeronName)
#10
后边再加个 max(Operatetime) Operatetime
#11
--这样?
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-21 14:07:17
-- Verstion:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[TableA]
if object_id('[TableA]') is not null drop table [TableA]
go
create table [TableA]([ID] varchar(2),[Name] varchar(4),[Money] int,[OperateTime] datetime)
insert [TableA]
select '01','小明',1,'2011-9-15 13:24:00' union all
select '01','小明',3,'2011-9-15 13:25:00' union all
select '01','小明',10,'2011-9-15 15:19:00'
--> 测试数据:[TableC]
if object_id('[TableC]') is not null drop table [TableC]
go
create table [TableC]([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime)
insert [TableC]
select '01','小明',5,1,'2011-9-15 13:24:00' union all
select '01','小明',3,3,'2011-9-15 13:25:00' union all
select '01','小明',2,7,'2011-9-15 15:19:00'
--------------开始查询--------------------------
select
a.id,a.name,a.money,b.money2,b.money3
from
(select id,name,OperateTime,sum([Money]) as [Money] from TableA group by id,name,OperateTime)a,
(select PeronID,PeronName,LogTime,sum([Money2]) as Money2,SUM([money3])as Money3 from TableC group by PeronID,PeronName,LogTime)b
where
a.OperateTime=b.LogTime
----------------结果----------------------------
/* id name money money2 money3
---- ---- ----------- ----------- -----------
01 小明 1 5 1
01 小明 3 3 3
01 小明 10 2 7
(3 行受影响)
*/
#12
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-21 14:07:17
-- Verstion:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[TableA]
if object_id('[TableA]') is not null drop table [TableA]
go
create table [TableA]([ID] varchar(2),[Name] varchar(4),[Money] int,[OperateTime] datetime)
insert [TableA]
select '01','小明',1,'2011-9-15 13:24:00' union all
select '01','小明',3,'2011-9-15 13:25:00' union all
select '01','小明',10,'2011-9-15 15:19:00'
--> 测试数据:[TableC]
if object_id('[TableC]') is not null drop table [TableC]
go
create table [TableC]([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime)
insert [TableC]
select '01','小明',5,1,'2011-9-15 13:24:00' union all
select '01','小明',3,3,'2011-9-15 13:25:00' union all
select '01','小明',2,7,'2011-9-15 15:19:00'
--------------开始查询--------------------------
;with f as
(
select
a.id,a.name,a.money,b.money2,b.money3
from
(select id,name,OperateTime,sum([Money]) as [Money] from TableA group by id,name,OperateTime)a,
(select PeronID,PeronName,LogTime,sum([Money2]) as Money2,SUM([money3])as Money3 from TableC group by PeronID,PeronName,LogTime)b
where
a.OperateTime=b.LogTime
)
select ID,name,SUM(money) as money,SUM(money2) as money2,SUM(money3) as money3 from f group by ID,name
----------------结果----------------------------
/*ID name money money2 money3
---- ---- ----------- ----------- -----------
01 小明 14 10 11
(1 行受影响)
*/
#13
select a.id,a.name,a.sum_money1 as TotalMoney ,b.sum_money2 as TotalMoney2 ,b.sum_money3 as TotalMoney3 ,convert(varchar(10),OperateTime,120) as OperateTime
from
(
select ID,Name,sum(money) as sum_money1,max(OperateTime) as OperateTime
from tableA
group by ID,Name
) a inner join
(
select PeronID ,PeronName ,sum(money2) as sum_money2,sum(money3) as sum_money3
from tableC
group by PeronID ,PeronName
)b
on a.ID=b.PeronID and a.name=b.PeronName
#1
TableC
PeronID PeronName Money2 Money3 LogTime
01 小明 5 1 2011-9-15 13:24:00
01 小明 3 3 2011-9-15 13:25:00
01 小明 2 7 2011-9-15 15:19:00
PeronID PeronName Money2 Money3 LogTime
01 小明 5 1 2011-9-15 13:24:00
01 小明 3 3 2011-9-15 13:25:00
01 小明 2 7 2011-9-15 15:19:00
#2
楼主要说清楚是怎么个合并法!
#3
结出想要的结果是什么
#4
用3个SUM求出SUM(Money),SUM(Money2),SUM(Money3),再合并成一行
#5
请楼主直接给出想要的结果
#6
select a.id,a.name,a.sum_money1,b.sum_money2,b.sum_money3
from
(
select ID,Name,sum(money) as sum_money1
from tableA
group by ID,Name
) a inner join
(
select PeronID ,PeronName ,sum(money2) as sum_money2,sum(money3) as sum_money3
from tableC
group by PeronID ,PeronName
)b
on a.ID=b.PeronID and a.name=b.PeronName
#7
是不是有种联结之类的语句,有点忘了,inner join,outer join,简单的AND。
#8
我想要得出:
Id Name TotalMoney TotalMoney2 TotalMoney3 Operatetime
01 小明 14 10 11 2011-9-15
Id Name TotalMoney TotalMoney2 TotalMoney3 Operatetime
01 小明 14 10 11 2011-9-15
#9
select isnull(a.ID,b.PeronID) ID,isnull(a.Name,b.PeronName) Name,
sum(isnull(a.[Money],0)) [Money],sum(isnull(b.Money2,0)) Money2,
sum(isnull(b.Money3,0)) Money3
from tableA a full join TableC b on a.ID = b.PeronID
group by isnull(a.ID,b.PeronID),isnull(a.Name,b.PeronName)
#10
后边再加个 max(Operatetime) Operatetime
#11
--这样?
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-21 14:07:17
-- Verstion:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[TableA]
if object_id('[TableA]') is not null drop table [TableA]
go
create table [TableA]([ID] varchar(2),[Name] varchar(4),[Money] int,[OperateTime] datetime)
insert [TableA]
select '01','小明',1,'2011-9-15 13:24:00' union all
select '01','小明',3,'2011-9-15 13:25:00' union all
select '01','小明',10,'2011-9-15 15:19:00'
--> 测试数据:[TableC]
if object_id('[TableC]') is not null drop table [TableC]
go
create table [TableC]([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime)
insert [TableC]
select '01','小明',5,1,'2011-9-15 13:24:00' union all
select '01','小明',3,3,'2011-9-15 13:25:00' union all
select '01','小明',2,7,'2011-9-15 15:19:00'
--------------开始查询--------------------------
select
a.id,a.name,a.money,b.money2,b.money3
from
(select id,name,OperateTime,sum([Money]) as [Money] from TableA group by id,name,OperateTime)a,
(select PeronID,PeronName,LogTime,sum([Money2]) as Money2,SUM([money3])as Money3 from TableC group by PeronID,PeronName,LogTime)b
where
a.OperateTime=b.LogTime
----------------结果----------------------------
/* id name money money2 money3
---- ---- ----------- ----------- -----------
01 小明 1 5 1
01 小明 3 3 3
01 小明 10 2 7
(3 行受影响)
*/
#12
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-21 14:07:17
-- Verstion:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[TableA]
if object_id('[TableA]') is not null drop table [TableA]
go
create table [TableA]([ID] varchar(2),[Name] varchar(4),[Money] int,[OperateTime] datetime)
insert [TableA]
select '01','小明',1,'2011-9-15 13:24:00' union all
select '01','小明',3,'2011-9-15 13:25:00' union all
select '01','小明',10,'2011-9-15 15:19:00'
--> 测试数据:[TableC]
if object_id('[TableC]') is not null drop table [TableC]
go
create table [TableC]([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime)
insert [TableC]
select '01','小明',5,1,'2011-9-15 13:24:00' union all
select '01','小明',3,3,'2011-9-15 13:25:00' union all
select '01','小明',2,7,'2011-9-15 15:19:00'
--------------开始查询--------------------------
;with f as
(
select
a.id,a.name,a.money,b.money2,b.money3
from
(select id,name,OperateTime,sum([Money]) as [Money] from TableA group by id,name,OperateTime)a,
(select PeronID,PeronName,LogTime,sum([Money2]) as Money2,SUM([money3])as Money3 from TableC group by PeronID,PeronName,LogTime)b
where
a.OperateTime=b.LogTime
)
select ID,name,SUM(money) as money,SUM(money2) as money2,SUM(money3) as money3 from f group by ID,name
----------------结果----------------------------
/*ID name money money2 money3
---- ---- ----------- ----------- -----------
01 小明 14 10 11
(1 行受影响)
*/
#13
select a.id,a.name,a.sum_money1 as TotalMoney ,b.sum_money2 as TotalMoney2 ,b.sum_money3 as TotalMoney3 ,convert(varchar(10),OperateTime,120) as OperateTime
from
(
select ID,Name,sum(money) as sum_money1,max(OperateTime) as OperateTime
from tableA
group by ID,Name
) a inner join
(
select PeronID ,PeronName ,sum(money2) as sum_money2,sum(money3) as sum_money3
from tableC
group by PeronID ,PeronName
)b
on a.ID=b.PeronID and a.name=b.PeronName