Hi,
I have this query
As you probably deduce asset_ident can have many rows for each type of ident. I only want the latest of these. To complicate matters sometimes there are more than one rows for a particular id_cd and asset_id where the dates are the same, I'd like to reduce the rows returned to one for these. asset_id is the primary key and I would like only one row per asset_id.
The above query does not do what I thought it would (I did not expect it to reduce to one row when there are same id_eff_dt for a given asset_id and id_cd). Is there are way of achieveing my desired result?
Thanks,
David
I have this query
Code:
SELECT
c2.id_number AS Cusip, c1.id_number AS Isin, c3.id_number AS Sedol,
c4.id_number AS Cusip144a, c5.id_number AS Isin144a,
ISNULL(fii.party_short_name, fii.party_long_name) AS DESCRIPTION,
bintostr(a.asset_id) AS ASSET_ID
FROM govcorp..asset a LEFT JOIN
(SELECT asset_id, id_number, MAX(id_eff_dt) AS id_eff_dt FROM govcorp..asset_ident WHERE id_cd='ISN'
GROUP BY asset_id,id_cd,id_number) c1 ON a.asset_id=c1.asset_id
LEFT JOIN
(SELECT asset_id,id_number,MAX(id_eff_dt) AS id_eff_dt FROM govcorp..asset_ident WHERE id_cd='CSP'
GROUP BY asset_id,id_cd,id_number) c2 ON a.asset_id=c2.asset_id
LEFT JOIN
(SELECT asset_id,id_number,MAX(id_eff_dt) AS id_eff_dt FROM govcorp..asset_ident WHERE id_cd='SED'
GROUP BY asset_id,id_cd,id_number) c3 ON a.asset_id=c3.asset_id
LEFT JOIN
(SELECT asset_id,id_number,MAX(id_eff_dt) AS id_eff_dt FROM govcorp..asset_ident WHERE id_cd='UAC'
GROUP BY asset_id,id_cd,id_number) c4 ON a.asset_id=c4.asset_id
LEFT JOIN
(SELECT asset_id,id_number,MAX(id_eff_dt) AS id_eff_dt FROM govcorp..asset_ident WHERE id_cd='UAI'
GROUP BY asset_id, id_cd, id_number) c5 ON a.asset_id = c5.asset_id
As you probably deduce asset_ident can have many rows for each type of ident. I only want the latest of these. To complicate matters sometimes there are more than one rows for a particular id_cd and asset_id where the dates are the same, I'd like to reduce the rows returned to one for these. asset_id is the primary key and I would like only one row per asset_id.
The above query does not do what I thought it would (I did not expect it to reduce to one row when there are same id_eff_dt for a given asset_id and id_cd). Is there are way of achieveing my desired result?
Thanks,
David