2016-08-15 4 views
1

私は、他のテーブルのmaxのカラムをビューテーブルに入れる方法を理解しようとしています!ので、ここで は私のテーブルです:ビューのカラムへのテーブルの最大値

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 

TblProprtyDetailsは今、私は、この構造ではビューに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

すごい! それは間違っています!それはTblProprtyDetailsからすべての行を返す代わりに、ただ1つの計算された行を返します – Siolishe

+0

私の悪い!それは魅力のような仕事だ tnx man – Siolishe

関連する問題