2016-08-15 42 views
1

我想弄清楚如何把另一个表的最大列到视图表! 所以这里是我的表:最大的一个表到一个视图的列

TblProprtyDetails

id Property_id amount  situation 
1   1   152   true 
2   1   545   false 
3   2   5   false 
4   2   87   true 

TblExperties

id PropertyDetails_id ExpertiesDate  ExpertiesPrice 
1    1    2015-10-01    54 
2    1    2015-11-15    546 
3    2    2016-01-05    6895 
4    2    2016-08-01    654 

现在我想把ExpertiesDate的最大和数量的总和成鉴于此结构:

id Property_id amount  situation LastExpertiesDate 

回答

0
select 
id ,Property_id,amount,situation,max(ExpertiesDate) as lastexpertisedate 
from 
TblProprtyDetails t1 
join 
TblExperties t2 
on t1.id=t2.id 
group by 
id ,Property_id,amount,situation 

您还可以使用跨应用

select 
    id ,Property_id,amount,situation,b.* 
    from 
    TblProprtyDetails t1 
cross apply 
(
select max(ExpertiesDate) as lastexpertisedate from table t2 where 
t1.id=t2.id) b 
+0

哇你R快! 但它是错的!它返回来自TblProprtyDetails的所有行而不是返回一个计算行 – Siolishe

+0

我的不好!它的工作就像一个魅力 tnx人 – Siolishe

相关问题