I would like to see my final query results as per below output.
What I have now is below query where I get 2 instances of traffic Medium "Display" but I need only the first instance
I also need an Unassigned value for any Period Start Date which has a Signupdate which is < than Period Start date
**Currently I have below query where I have a self join to itself to get 2 rows of data next to each other but need help to get to the final result. Please advise.
Thanks in advance
Select
C.Eventid, C.AffiliateId, C.AffiliateLogin, C.SignupDate, C.TrafficMediumLabel, C.PeriodStartDate, C.PeriodEndDate, C.TimeStamp, RN
INTO #TempTbl1
from (
Select X.Eventid,X.AffiliateId, X.AffiliateLogin, X.SignupDate, X.TrafficMediumLabel, X.PeriodStartDate, X.PeriodEndDate, X.TimeStamp, ROW_NUMBER()OVER( ORDER BY X.Timestamp ) AS RN
from (
Select A.Eventid, A.AffiliateId, A.AffiliateLogin, A.SignupDate, A.TrafficMediumLabel, A.PeriodStartDate, A.PeriodEndDate, A.TimeStamp
from AnalyticsEventsAffiliateTraffic A
JOIN (
Select AffiliateId, AffiliateLogin, SignupDate, PeriodStartDate, MAX(TimeStamp) [MaxChange]
from AnalyticsEventsAffiliateTraffic where AffiliateLogin = 'sknilllc'
GROUP BY AffiliateId, AffiliateLogin, SignupDate, PeriodStartDate
) B ON B.PeriodStartDate = A.PeriodStartDate AND B.MaxChange = A.TimeStamp
where
A.AffiliateLogin = 'sknilllc') X
) C
SELECT *
FROM (
SELECT
A.AffiliateId, A.AffiliateLogin, CAST(A.SignupDate AS DATE) [SignupDate],
A.TrafficMediumLabel [P1Medium], A.PeriodStartDate [P1StartDate], A.PeriodEndDate [P1EndDate], A.RN [P1Row], B.RN [P2Row],
B.TrafficMediumLabel [P2Medium], B.PeriodStartDate [P2StartDate], B.PeriodEndDate [P2EndDate]
FROM #TempTbl1 A
FULL OUTER JOIN #TempTbl1 B ON B.RN = A.RN- 1
) X
DROP TABLE #TempTbl1