日期:2014-05-19  浏览次数:20513 次

关于数据查询的问题
现有表A
ID   Name
--------
1     aaa
2     bbb
3     ccc

表B
ID   Name   Value   Date   Time
------------------------
1     aaa     1.017   20060506   162341
2     ccc     1.025   20060504   104253
3     bbb     2.014   20060404   091523
4     ccc     1.052   20060301   081524
5     aaa     1.034   20060506   094232
6     bbb     1.225   20060302   103214

我希望通过执行SQL得到下面结果

ID   Name   Value
---------------
1     aaa     1.017
2     bbb     2.014
3     ccc     1.025

即查询出所有Name所对应的最新的Value

------解决方案--------------------
Select
A.ID,
A.Name,
B.Value
From
A
Inner Join
B
On A.Name = B.Name
Inner Join
(Select Name, Max([Date] + [Time] )As [DateTime] From B Group By Name) C
On B.Name = C.Name And B.[Date] + B.[Time] = C.[DateTime]
------解决方案--------------------
select 表a.id,表a.name,表b.value from 表a,表b
where 表a.id=表b.id and 表a.name=表b.name order by 表a.id
------解决方案--------------------
Select B.Name,B.Value
From B,(Select Name,max(Date+Time) as Date From B Group by Name) Table1
Where B.Date+B.Time=Table1.Date
Order By B.Name
------解决方案--------------------
SELECT * FROM B a where id=(select top 1 id from B where a.Name=Name order by DateTime desc)
------解决方案--------------------
这个应该不难啊,下面是ID自增长时的情况,如果ID不为唯一,那就把MAX(ID)换成DATE+TIME了

select name,value form table where id in(select max(id) from talbe group by name)