Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Update a Temp table based on a condition

$
0
0
DECLARE @CokeTypes TABLE (CampaignID INT, CokeTypeName NVARCHAR(max), StartDate DATETIME)
INSERT INTO @CokeTypes
SELECT
DISTINCT
cc2.CampaignID,
(SELECT cc1.CokeTypeName + '- (' + CAST (cc1.Percentage as NVARCHAR(10)) +'%); '  as [text()]
FROM Anode.CampaignCoke cc1
WHERE cc1.CampaignID=cc2.CampaignID for XML PATH ('')) AS CokeTypeName,
AC.StartDate
FROM
Anode.CampaignCoke cc2
JOIN Anode.Campaign AC
on ac.CampaignID=CC2.CampaignID

UPDATE @CokeTypes
SET CokeTypeName=SUBSTRING(CokeTypeName,1,LEN(CokeTypeName)-1)
WHERE CokeTypeName IS NOT NULL

--SELECT * FROM @CokeTypes

DECLARE @TopCokeTypes TABLE (CampaignID INT, CokeTypeName NVARCHAR(max), StartDate DATETIME)
INSERT INTO @TopCokeTypes
SELECT DISTINCT TOP 5  * FROM @CokeTypes ORDER BY StartDate DESC

DECLARE @CokeTypesSearch TABLE (CampaignID INT, CokeTypeName NVARCHAR(max), StartDate DATETIME)
INSERT INTO @CokeTypesSearch
SELECT TCT.CampaignID, CT.CokeTypeName,CT.StartDate FROM @TopCokeTypes TCT 
JOIN @CokeTypes CT
ON TCT.CokeTypeName=CT.CokeTypeName --AND TCT.CampaignID <> CT.CampaignID

select * from @TopCokeTypes

select * from @CokeTypesSearch

Above query will give me two results in two temp tables.

Now I need to search in @CokeTypesSearch and find whether any of the copketypename from @TopCokeTypes is present in @CokeTypesSearch  or not. If thats the case I should update the "StartDate" of the @TopCokeTypesof that particular filed as the oldest startdate (Min) from @CokeTypesSearch table.

for eg: for CokeType " ABP 03/10- (100%)" StartDate should be "2010-08-12 10:30:51.223" and EndDate should be "2014-05-06 10:31:58.730" in @TopCokeTypes Once I complete my logic.

Can anypne please let me know the most easiest way to do this?

Thanks

-pep


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>