makeitwork09
Technical User
I am using Microsoft SQL Server 2005
I am trying to create a ranking where brief has priority over actual which has priorty over priority. These are user defined types.
In addition, it should be the most recent statement date that is extracted for the corresponding user defined type.
The way I have the code below however, if a property has all three of the user defined types and if proforma is the most recent user defined type, it is getting a row number of 1, when I want it to be 3, even though it has the most recent date. The only time proforma should be the row number of 1 is if it is the only user defined type for the property.
Therefore I want
prop usertype statement_Date
empire1 brief 2011-02-13
empire1 actual 2010-12-30
empire1 proforma 2012-05-15
gmbuild actual 2010-12-30
gmbuild proforma 2012-05-15
cardnil proforma 2011-03-20
Any thoughts?
Thanks
I am trying to create a ranking where brief has priority over actual which has priorty over priority. These are user defined types.
In addition, it should be the most recent statement date that is extracted for the corresponding user defined type.
The way I have the code below however, if a property has all three of the user defined types and if proforma is the most recent user defined type, it is getting a row number of 1, when I want it to be 3, even though it has the most recent date. The only time proforma should be the row number of 1 is if it is the only user defined type for the property.
Therefore I want
prop usertype statement_Date
empire1 brief 2011-02-13
empire1 actual 2010-12-30
empire1 proforma 2012-05-15
gmbuild actual 2010-12-30
gmbuild proforma 2012-05-15
cardnil proforma 2011-03-20
Any thoughts?
Code:
select o.prop_code, o.user_def_type,
ROW_NUMBER() OVER (partition by o.prop_code ORDER BY
o.statement_date desc,
case when o.user_def_type = 'brief' then 1
when o.user_def_type = 'actual' then 2
when o.user_def_type = 'proforma' then 3
else 4
end) as [rownum],
o.statement_date, m.max_statement_date,o.debt_serv_covrg,o.net_income,
o.ann_debt_serv,o.gross_income,o.total_expenses
from opstatmt o
left outer join (select x.prop_code,max(x.statement_date) as[max_statement_date]
from opstatmt x
group by x.prop_code) as m on
m.prop_code = o.prop_codewhere
where o.user_def_type in ('brief','actual','proforma')
Thanks