SQL 多表查询

pw1withnn 2014-03-21 11:55:44
现在有两个表,一个企业表(A),一个企业消费表(B),企业消费表里存储了企业的每笔消费,结构如下

A B
id name eid,price,addtime
1 m-1 1 30 2014-1-1
2 m-2 1 20 2014-1-2
3 m-3 1 79 2014-1-3
2 100 2014-1-6

现在想实现的是查询每个企业的剩余金额(price),按日期排序,最后一条记录为每个企业剩余金额。

select A.ID,A.name,B.price from A
left join (select top 1 ye,dwid from B group by dwid order by addtime desc) as d on A.id= d.eid


这样的SQL实现不了。有没有更好的SQL语句啊。求解
...全文
116 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2014-03-21
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2014-03-21 12:00:26
-- Verstion:
--      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
--	Jul  9 2008 14:43:34 
--	Copyright (c) 1988-2008 Microsoft Corporation
--	Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go 
create table [A]([id] int,[name] varchar(3))
insert [A]
select 1,'m-1' union all
select 2,'m-2' union all
select 3,'m-3'
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go 
create table [b]([eid] int,[price] int,[addtime] datetime)
insert [b]
select 1,30,'2014-1-1' union all
select 1,20,'2014-1-2' union all
select 1,79,'2014-1-3' union all
select 2,100,'2014-1-6'
--------------开始查询--------------------------
select
   a.id,a.name,b.price
from  
  a inner join b on a.id=b.eid
where 
  not exists(select 1 from b as t where eid=b.eid and addtime>b.addtime)
----------------结果----------------------------
/* id          name price
----------- ---- -----------
1           m-1  79
2           m-2  100

(2 行受影响)
*/
--小F-- 2014-03-21
  • 打赏
  • 举报
回复
select
   a.id,a.name,b.price
from  
  a inner join b on a.id=b.eid
where 
  not exists(select 1 from b as t where eid=b.eid and addtime<b.addtime)
国士_枫 2014-03-21
  • 打赏
  • 举报
回复
select top 1 id,name,price from ( select id,name,price,row_number()over(partition by id order by addtime desc) as ord from A join B on A.id=B.eid )comp
  • 打赏
  • 举报
回复
引用 5 楼 pw1withnn 的回复:
[quote=引用 4 楼 DBA_Huangzj 的回复:] 你的数据有多大会担心效率?
每天估计1000多条吧。[/quote] 哦,那数据量还不是很大,一个月就是3万条把 而且他用的是not exists 效率应该还是可以的
發糞塗牆 2014-03-21
  • 打赏
  • 举报
回复
1000条,爱怎么搞怎么搞....
pw1withnn 2014-03-21
  • 打赏
  • 举报
回复
引用 4 楼 DBA_Huangzj 的回复:
你的数据有多大会担心效率?
每天估计1000多条吧。
發糞塗牆 2014-03-21
  • 打赏
  • 举报
回复
你的数据有多大会担心效率?
pw1withnn 2014-03-21
  • 打赏
  • 举报
回复
引用 2 楼 fredrickhu 的回复:
----------------------------------------------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2014-03-21 12:00:26
-- Verstion:
--      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
--	Jul  9 2008 14:43:34 
--	Copyright (c) 1988-2008 Microsoft Corporation
--	Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go 
create table [A]([id] int,[name] varchar(3))
insert [A]
select 1,'m-1' union all
select 2,'m-2' union all
select 3,'m-3'
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go 
create table [b]([eid] int,[price] int,[addtime] datetime)
insert [b]
select 1,30,'2014-1-1' union all
select 1,20,'2014-1-2' union all
select 1,79,'2014-1-3' union all
select 2,100,'2014-1-6'
--------------开始查询--------------------------
select
   a.id,a.name,b.price
from  
  a inner join b on a.id=b.eid
where 
  not exists(select 1 from b as t where eid=b.eid and addtime>b.addtime)
----------------结果----------------------------
/* id          name price
----------- ---- -----------
1           m-1  79
2           m-2  100

(2 行受影响)
*/
高手啊,谢谢,这样可以达到效果,不过这个算子查询吗? 如果数据量大的话 会影响效率吗?

22,209

社区成员

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

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