Certainly! Let's dive deeper into each section of the SQL script to fully understand what each part does, including its purpose, logic, and potential implications.
1. Initial Insertion into DI_ProppyPropertyMarket
SQL Statement:
INSERT INTO [dbo].[DI_ProppyPropertyMarket] (...)
SELECT [ListingID], [AdvertiserType], [ListingType], ...
FROM [dbo].[ProppyPropertyMarket]
WHERE [PropertyAgentID] IN (
SELECT AgentID
FROM [dbo].[PropertyAgent]
WHERE [LicenceNo] LIKE '%ren%'
)
AND ImportDate >= '2022-10-12'
AND ImportDate <= '2023-01-16'
Explanation:
2. Updating PropertyType
SQL Statements:
UPDATE [DI_ProppyPropertyMarket] SET [PropertyType] = 'Shop Office' WHERE [PropertyType] = 'Shop-Office';
UPDATE [DI_ProppyPropertyMarket] SET [PropertyType] = 'Retail Office' WHERE [PropertyType] = 'Retail-Office';
UPDATE [DI_ProppyPropertyMarket] SET [PropertyType] = 'Semi-D factory' WHERE [PropertyType] = 'Semi- D factory';
Explanation:
3. Updating TmpRenTagNo and AdvertiserType
SQL Statements:
UPDATE [DI_ProppyPropertyMarket]
SET [TmpRenTagNo] = RE.[LicenceNo]
FROM [DI_ProppyPropertyMarket] PM
INNER JOIN [dbo].[PropertyAgent] RE
ON PM.[PropertyAgentID] = RE.[AgentID];
UPDATE [DI_ProppyPropertyMarket]
SET [AdvertiserType] = RE.[Name]
FROM [DI_ProppyPropertyMarket] PM
INNER JOIN [dbo].[REF] RE
ON RE.[Type] = 'Adv' AND PM.[AdvtTypeId] = RE.[Code];
Explanation:
4. Updating Location and Property Information
SQL Statements:
UPDATE [DI_ProppyPropertyMarket]
SET [StateID] = ISNULL(RE.[ID], 0)
FROM [DI_ProppyPropertyMarket] PM
INNER JOIN [dbo].[States] RE
ON PM.[State] = RE.[Name];
UPDATE [DI_ProppyPropertyMarket]
SET [AreaID] = ISNULL(RE.[Code], 0)
FROM [DI_ProppyPropertyMarket] PM
INNER JOIN [dbo].[Area] RE
ON PM.[Area] = RE.[Name];
UPDATE [DI_ProppyPropertyMarket]
SET [PropertyTypeID] = ISNULL(RE.[ID], 0)
FROM [DI_ProppyPropertyMarket] PM
INNER JOIN [dbo].[PropertyType] RE
ON PM.[PropertyType] = RE.[Description];
Explanation:
5. Clearing PropertyAgentID
SQL Statement:
UPDATE [DI_ProppyPropertyMarket]
SET [PropertyAgentID] = '';
Explanation:
6. Inserting and Updating Image Data
SQL Statements:
INSERT INTO [dbo].[DI_PropertyMarketImages] ([PropertyID], [ListingID], ...)
SELECT 0, PM.[ListingID], PM.[ImageURL], ...
FROM [dbo].[PropertyMarketImages] PM
INNER JOIN [DI_ProppyPropertyMarket] RE
ON PM.[ListingID] = RE.[ListingID];
UPDATE [DI_PropertyMarketImages]
SET [PropertyID] = RE.[ID]
FROM [DI_PropertyMarketImages] PM
INNER JOIN [DI_ProppyPropertyMarket] RE
ON PM.[ListingID] = RE.[ListingID];
Explanation:
7. Final Insertion Back into ProppyPropertyMarket
SQL Statement:
INSERT INTO [dbo].[ProppyPropertyMarket] (...)
SELECT ...
FROM [DI_ProppyPropertyMarket]
WHERE (Latitude IS NOT NULL AND Latitude <> 0)
AND (TmpRenTagNo LIKE 'REN%')
AND (ShortDesc IS NOT NULL OR DetailsDesc IS NOT NULL);
Explanation:
8. Final Updates for Image Data
SQL Statements:
UPDATE [DI_PropertyMarketImages]
SET [PropertyID] = RE.[ID]
FROM [DI_PropertyMarketImages] PM
INNER JOIN [ProppyPropertyMarket] RE
ON PM.[ListingID] = RE.[ListingID]
AND RE.[ImportDate] IS NOT NULL;
INSERT INTO [dbo].[PropertyMarketImages] (...)
SELECT PM.[PropertyID], PM.[ListingID], ...
FROM [DI_PropertyMarketImages] PM
INNER JOIN [ProppyPropertyMarket] RE
ON PM.[ListingID] = RE.[ListingID]
AND RE.[ImportDate] IS NOT NULL;
Explanation:
IDinDI_PropertyMarketImagesto match the correct records inProppyPropertyMarket`.
- Final Image Insertion:
- After ensuring all links are correct, it inserts the final image records into
PropertyMarketImages, ensuring all image data is correctly linked to its respective properties.
Conclusion:
This script is a comprehensive data transformation and migration process. It carefully selects, updates, standardizes, and re-inserts data to maintain data integrity and consistency across multiple related tables. The process ensures that all necessary fields are populated, links between records are correct, and that data is ready for further use, such as reporting, querying, or front-end display. Each step is designed to handle specific requirements and constraints, ensuring that the data is clean, accurate, and usable.