Hi All,I am working on a query where i had to find the output based on the conditional date logic. Below is the description of the problem.What i want is that order should be greater then or equal to effective date and if there are 2 effective date greater then or equal to order date then pick the minimum effective date record.Please guide me what would be best approach to handle this scenario .--- Test data Script-------IF OBJECT_ID('tempdb..#Customer', 'U') IS NOT NULL DROP TABLE #Customer;CREATE TABLE #Customer ( CustomerKey int, OrderDate Date );INSERT #Customer (CustomerKey,OrderDate) VALUES (1,'11/10/2016'), (2,'11/26/2016'), (3,'11/15/2016'), (4,'11/20/2016')IF OBJECT_ID('tempdb..#CustomerRef', 'U') IS NOT NULL DROP TABLE #CustomerRef;CREATE TABLE #CustomerRef ( CustomerKey INT, CustomerKeyRef INT, EffectiveDate DATE );INSERT #CustomerRef (CustomerKey,CustomerKeyRef,EffectiveDate) VALUES (1,101,'11/1/2016'), (1,101,'11/15/2016'), (2,201,'11/26/2016'), (2,201,'11/28/2016'), (3,301,'11/15/2016'), (4,401,'11/15/2016')---- Output Script -----------------SELECT 1 as CustomerKey,101 as CustomerKeyRef , '11/1/2016' as EffectiveDateunionSELECT 2 as CustomerKey,201 as CustomerKeyRef , '11/26/2016' as EffectiveDateunionSELECT 3 as CustomerKey,301 as CustomerKeyRef , '11/15/2016' as EffectiveDateAny help will be greatly appreciated.thanks
↧