I have a table that has about seven columns, including a field that contains XML.
There are multiple customer IDs, each of which may have more than one product ID.
For each unique combination of customer ID and product ID, there is more than one table ID (called edw_policy_data_id).
I need to get the XML that corresponds to the max edw_policy_data_id for each unique combination of customer ID and product instance ID.
Finally, I need to restrict the inner query on a date range.
My current query looks like this; I'm returning more columns than I need right now, and ordering them, to debug the query. I haven't been able to do anything meaningful with max() yet.
select edw_policy_data_id, e.customer_id, e.product_instance_id, policy_data_xml
FROM [DB].[dbo].[EDW_POLICY_DATA] e
inner join (SELECT distinct customer_id, product_instance_id
FROM [DB].[dbo].[EDW_POLICY_DATA]
where created_date > '12/1/2011'
and created_date < '12/27/2011 17:16:00') d
on e.customer_id = d.customer_id and e.product_instance_id = d.product_instance_id
order by customer_id, product_instance_id, edw_policy_data_id
The results look something like this:
edw_policy_data_id e.customer_id e.product_instance_id policy_data_xml
1 100 200 xml
2 100 200 xml
3 100 201 xml
4 101 203 xml
5 101 203 xml
I need the results to look like this for debug - note that only the max edw_policy_data_id for each unique customer_ID/product_instance_id combination is present.
edw_policy_data_id e.customer_id e.product_instance_id policy_data_xml
2 100 200 xml
3 100 201 xml
5 101 203 xml
Ultimately, all I want is the XML for each max(edw_policy_id)/customer_id/product_instance_ID combo.
I looked at the responses to this question: no joy.