I need to display some columns on the final result which comes from a join. Those columns comes in 3 groups, say:
name_1, addr_1, prefecture_1, price_1,
name_2, addr_2, prefecture_2, price_2,
name_3, addr_3, prefecture_3, price_3
Where these 3 groups comes from a join which should return only 3 records according to a sort key carrier_cd2. The _1 group is the first row, _2 group the second and _3 is the third row.
My question is pretty much the same as this SO question but I need 3 records instead of 2.
I have come up with my initial SQL query:
SELECT
inner_t12.contract_no,
inner_t13.carrier_cd2,
inner_t13.addr,
inner_t13.prefecture,
inner_t13_2.carrier_cd2,
inner_t13_2.addr,
inner_t13_2.prefecture,
inner_t13_3.carrier_cd2,
inner_t13_3.addr,
inner_t13_3.prefecture
FROM
処分業者契約マスタ inner_t12
LEFT OUTER JOIN 処分契約委託運搬業者マスタ inner_t13
ON inner_t12.contract_no = inner_t13.contract_no
JOIN 処分契約委託運搬業者マスタ inner_t13_2
ON inner_t13_2.contract_no = inner_t13.contract_no
AND inner_t13_2.carrier_cd2 > inner_t13.carrier_cd2
JOIN 処分契約委託運搬業者マスタ inner_t13_3
ON inner_t13_3.contract_no = inner_t13.contract_no
AND inner_t13_3.carrier_cd2 > inner_t13_2.carrier_cd2
It returns the required rows on the final result set, however it need to be left outer join so that it will still display rows even if the join does not produce the required 3 rows, even 2 or 1 or none should work and display NULL instead.
Hope you can give some hints on this.
Thanks in advanced.
SQL Server 2005 Express