SELECT DISTINCT Campaign_id
FROM Impressions
WHERE Date BETWEEN '2015-03-01' AND '2015-03-31' ;
The above query gives me the result for the Campaign_id
's that have been active on any date between 2015-03-01 and 2015-03-31.
I want the result set to contain the campaign_id
's if the have been active on all the dates in between 2015-03-01 and 2015-03-31.
How would I go about this?
Assuming DATE
is a DATE
datatype and has no time component.
DECLARE @Start DATE = '2015-03-01',
@End DATE = '2015-03-31'
SELECT Campaign_id
FROM Impressions
WHERE Date BETWEEN @Start AND @End
GROUP BY Campaign_id
HAVING COUNT(DISTINCT Date) = 1 + DATEDIFF(DAY, @Start, @End);
Or a version without the variables
SELECT Campaign_id
FROM Impressions
CROSS APPLY (VALUES ({ d '2015-03-01' },
{ d '2015-03-31' })) V([Start], [End])
WHERE [Date] BETWEEN [Start] AND [End]
GROUP BY Campaign_id, [Start], [End]
HAVING COUNT(DISTINCT Date) = 1 + DATEDIFF(DAY, [Start], [End]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With