sql server - MS SQL: Help integrating two chunks of SQL -
i have query pulls data 2 tables, giving precedence first table.
select row_number() over(order date asc) 'union_id', id, [date], data_from, total from( select id, date, 'a' data_from, total tbldailyconsumptionsfroma (tbldailyconsumptionsfroma.active = 1) , (tbldailyconsumptionsfromstark.matched_metering_point = x) union select id, date, 'b' data_from, total tbldailyconsumptionsfromb (tbldailyconsumptionsfromb.active = 1) , (tbldailyconsumptionsfromb.matched_metering_point = x) , not date in (select date tbldailyconsumptionsfroma tbldailyconsumptionsfroma.matched_metering_point = x , tbldailyconsumptionsfroma.active = 1) ) c;
my aim select rescent 365 days of data, outcome of union.
@t-clausen-dk kindly provided query below, believe on right path...
declare @gapdays int = 2 -- replace 365 in case ;with x ( select datediff(d, '2014-01-01', [date])-dense_rank()over(order [date]) grp ,[date] @t ) select top 1 max([date]) last_date, min([date]) first_date, count(distinct [date]) days_in_a_row x group grp having count(distinct [date]) >= @gapdays order max([date]) desc
can put end rescent 365 continuous days of data returned? (and no rows returned if there isn't continuous run of 365 days).
thank in advance!!!
Comments
Post a Comment