求sql语句求sql语句求sql语句求sql语句

lwj7891875 2014-04-17 04:58:06
有一张表 数据是
id usercode time detail
1 1001 13343432 内容
2 1002 14332233 内容

第二张表
id usercode time detail
1 1001 1234232111 内容

我要得到 表一 time字段减去表二time字段的 结果 根据usercode去关联
...全文
237 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
RaphaeL_yb 2014-04-30
  • 打赏
  • 举报
回复
楼主两个表中的ID是关键字吧 或者是自增字段吧? 如果有重复,都不知道怎么搞。。。
發糞塗牆 2014-04-17
  • 打赏
  • 举报
回复
引用 9 楼 t101lian 的回复:
[quote=引用 1 楼 DBA_Huangzj 的回复:] 你哪个time是什么数据类型?就你这个例子,期待结果是怎样的?
版主, 请问下楼主的结贴率怎么会大于100%?[/quote]bug,或者有贴子被删了。。。。吧
t101lian 2014-04-17
  • 打赏
  • 举报
回复
引用 1 楼 DBA_Huangzj 的回复:
你哪个time是什么数据类型?就你这个例子,期待结果是怎样的?
版主, 请问下楼主的结贴率怎么会大于100%?
guguda2008 2014-04-17
  • 打赏
  • 举报
回复
引用 6 楼 lwj7891875 的回复:
第一张表的内容
----------------------------------------------------------------
-- Author  :借测试数据一用
-- Date    :2014-04-17 17:02:48
-- Version:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
--    Apr  2 2010 15:48:46 
--    Copyright (c) Microsoft Corporation
--    Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[A]
if object_id('[A]') is not null drop table [A]
go 
create table [A]([id] int,[usercode] int,[time] int,[detail] nvarchar(4))
insert [A]
select 1,1001,13343432,N'内容A1' union all
select 2,1002,14332233,N'内容A2'
--> 测试数据[B]
if object_id('[B]') is not null drop table [B]
go 
create table [B]([id] int,[usercode] int,[time] int,[detail] nvarchar(4))
insert [B]
select 1,1001,1234232111,N'内容B2'
--------------生成数据--------------------------
select A.usercode,A.time-ISNULL(B.time,0),A.detail
from A
left join (select usercode,sum(time) as time from B group by usercode) B on A.usercode=B.usercode
 
----------------结果----------------------------
/* 
usercode                detail
----------- ----------- ------
1001        -1220888679 内容A1
1002        14332233    内容A2
 
*/
lwj7891875 2014-04-17
  • 打赏
  • 举报
回复
求解答。。。。。。。。
lwj7891875 2014-04-17
  • 打赏
  • 举报
回复
第一张表的内容
lwj7891875 2014-04-17
  • 打赏
  • 举报
回复
列是不相同的 第一张表 列还有好多的
guguda2008 2014-04-17
  • 打赏
  • 举报
回复
引用 3 楼 lwj7891875 的回复:
time 是时间戳 我要的数据结果是 比如表一的1001 减去表2的1001 对应的time 最后的结果是 d usercode time detail 1 1001 相减后的值 内容
内容取哪个表的
lwj7891875 2014-04-17
  • 打赏
  • 举报
回复
time 是时间戳 我要的数据结果是 比如表一的1001 减去表2的1001 对应的time 最后的结果是 d usercode time detail 1 1001 相减后的值 内容
發糞塗牆 2014-04-17
  • 打赏
  • 举报
回复
这样?
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(发粪涂墙)
-- Date    :2014-04-17 17:02:48
-- Version:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
--	Apr  2 2010 15:48:46 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[A]
if object_id('[A]') is not null drop table [A]
go 
create table [A]([id] int,[usercode] int,[time] int,[detail] nvarchar(4))
insert [A]
select 1,1001,13343432,N'内容' union all
select 2,1002,14332233,N'内容'
--> 测试数据[B]
if object_id('[B]') is not null drop table [B]
go 
create table [B]([id] int,[usercode] int,[time] int,[detail] nvarchar(4))
insert [B]
select 1,1001,1234232111,N'内容'
--------------生成数据--------------------------
SELECT usercode,SUM([time])[time],detail
FROM (
select usercode,[time],detail from [A]
UNION 
select usercode,-1*[time],detail from [B]
)a
GROUP BY usercode,detail

----------------结果----------------------------
/* 
usercode    time        detail
----------- ----------- ------
1001        -1220888679 内容
1002        14332233    内容

*/
發糞塗牆 2014-04-17
  • 打赏
  • 举报
回复
你哪个time是什么数据类型?就你这个例子,期待结果是怎样的?

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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