Reputation: 11
I have distinct category code that I need to join with every month form a start date to an end data.
DECLARE @Start DATE, @End DATE
SET @Start = '20170101'
SET @End = getdate()
SELECT Category_Cd, CalMonth = (SELECT DATEADD(MONTH,number+1,@Start) [Date]
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(MONTH,number+1,@Start) < @End)
FROM dbo.Category
WHERE (Category_Cd LIKE N'0%')
because there is no join i get this: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I do not want a join, I just want every category code to have every data from the start to the end show. Thanks for your help.
DECLARE @Start DATE, @End DATE
SET @Start = '20170101'
SET @End = getdate()
SELECT DATEADD(MONTH,number+1,@Start) [Date]
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(MONTH,number+1,@Start) < @End
this code does return every start to end date like I want. Now I just need it to be done with each category code.
01001 2017-02-01
01001 2017-03-01
01001 2017-04-01
01001 2017-05-01
01002 2017-02-01
01002 2017-03-01
01002 2017-04-01
01002 2017-05-01
01003 2017-02-01
01003 2017-03-01
01003 2017-04-01
01003 2017-05-01
Upvotes: 0
Views: 53
Reputation: 48177
DECLARE @Start DATE, @End DATE;
SET @Start = '20170101';
SET @End = getdate();
WITH myMonths as (
SELECT DATEADD(MONTH,number+1,@Start) [Date]
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(MONTH,number+1,@Start) < @End
)
SELECT *
FROM category
CROSS JOIN myMonths
Upvotes: 1