I have a table that contains thousands of records but only some of those records match a certain criteria. I then need to extract these records that match the criteria and update an external system with a ‘Y’ flag by way of a user category.
My temporary table contains only the records that match the required criteria and all of those records in the temporary table then update in the external system with a ‘Y’ flag.
My problem is that these records change on a daily basis and I have a situation where old records, that were flagged as ‘Y’ previously, do not meet the criteria anymore and should now update to ‘N’. The old records do not update in the external system to ‘N’ as the last record received was a ‘Y’ some time ago.
Currently I use the following SQL query to identify the applicable records and these records then feeds into the temporary table:
declare @Dummyfundsonly table (iss_id varchar(20)) insert into @Dummyfundsonly select iss_id AS iss_id from infoportal..position_dg where as_of_tms >= cast(getdate() - 1 as date) group by iss_id having count(*) = count( case when acct_id in ('FUTUR', 'ZOTCDUM') then 1 end )
Once I have the records from above mentioned sql these records (iss_id’s) update the sValue with a ‘Y’ flag for a certain user category as follows:
select distinct iss_id as sSecurityID, '654' as icategory, 'Y' as sValue, cast( dateadd( hh, 01,( select convert(char(10), getdate(), 120) ) ) as datetime ) as dtPoint from @Dummyfundsonly
One suggestion was to rather insert all the records from the main table into the temporary table and not just the ones matching the criteria and then to make use of a case statement to identify the applicable records in the temporary table that needs to update as sValue to ‘Y’ and the rest must be ‘N’.
I am struggling to incorporate my initial SQL query (to identify the unique records only) into the case statement for the sValue. Any suggestions to get this right?
CASE when ? ? ? then 'Y' else 'N' end as sValue