我正在处理需要排序行(排序依据)和联合语句的查询。我想做的是根据时间为每个客户获取最新的读数。我知道我不能在 union 语句中使用 order by,但是有什么方法可以实现我想要的吗?我也有几个表加入了 select 语句。以下是我的示例查询:
select top 1 name, ID, Date, Time, Cust_ID
from Table1
join Table2
on ID = Cust_ID
join Table3
on c = d
where ID = 'ID0001'
(order by Time desc)
union all
select top 1 name, ID, Date, Time, Cust_ID
from Table1
join Table2
on ID = Cust_ID
join Table3
on c = d
where ID = 'ID0002'
(order by Time desc)
union all
select top 1 name, ID, Date, Time, Cust_ID
from Table1
join Table2
on ID = Cust_ID
join Table3
on c = d
where ID = 'ID0003'
(order by Time desc)
所以我正在寻找能够产生这些结果的东西。有人知道这种方法吗?谢谢