2016-08-15 2 views
1

나는 다른 테이블의 최대 열을 뷰 테이블에 집어 넣는 방법을 알아 내려고하고있다!보기의 열로 테이블의 최대 값

id Property_id amount  situation LastExpertiesDate 
:

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의 최대 및 금액의 합을 데려 가고 싶다는

TblProprtyDetails : 그래서 여기 내 테이블입니다

답변

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에서 모든 행을 반환합니다 – Siolishe

+0

나쁜! 그것은 매력처럼 작동합니다 tnx man – Siolishe

관련 문제