Hi,
I am looking to create SQL that performs the reverse of a UNION statement.
A table has been set up for me that contains a list of week start dates (starting on a Sunday). The table tells you the week number each week start date relates to for a particular customer.
WStartDate FinWeekNo Customer
---------- ------ ---------
2006-05-03 1 Tesco
2006 12-03 2 Tesco
2006 19-03 3 Tesco
2006-05-03 4 Waitrose
2006 12-03 5 Waitrose
2006 19-03 6 Waitrose
What I need to do is to restructure this information into the following format:
WStartDate Tesco Week Waitrose Week
---------- ------ ---------
2006-05-03 1 4
2006 12-03 2 5
2006 19-03 3 6
Based on the requirement I am thinking of writing code which does the following:
1. Create a table Customers with a distinct list of customers from the first table
2. Loop through this table using a cursor (this table Customers will typically only have about 10 rows)
3. Create a temp table for each customer.
4. Join the temp tables together on their common Week Start Date fields.
Is this the best way however to go about 'bursting out' a table into smaller tables based on a field value? I am not certain at all. I have had a think about this but cannot think of a way of avoiding a loop.
Nassy
I am looking to create SQL that performs the reverse of a UNION statement.
A table has been set up for me that contains a list of week start dates (starting on a Sunday). The table tells you the week number each week start date relates to for a particular customer.
WStartDate FinWeekNo Customer
---------- ------ ---------
2006-05-03 1 Tesco
2006 12-03 2 Tesco
2006 19-03 3 Tesco
2006-05-03 4 Waitrose
2006 12-03 5 Waitrose
2006 19-03 6 Waitrose
What I need to do is to restructure this information into the following format:
WStartDate Tesco Week Waitrose Week
---------- ------ ---------
2006-05-03 1 4
2006 12-03 2 5
2006 19-03 3 6
Based on the requirement I am thinking of writing code which does the following:
1. Create a table Customers with a distinct list of customers from the first table
2. Loop through this table using a cursor (this table Customers will typically only have about 10 rows)
3. Create a temp table for each customer.
4. Join the temp tables together on their common Week Start Date fields.
Is this the best way however to go about 'bursting out' a table into smaller tables based on a field value? I am not certain at all. I have had a think about this but cannot think of a way of avoiding a loop.
Nassy