November 10, 2017 at 4:21 am
please help in finding missing months from the last one year- 2016-11 till 2017-11.
My data looks like-
Column1 column2
2016-11 A
2017-01 A
2017-04 A
2017-05 A
2017-06 A
2017-08 A
2017-10 A
2016-11 B
2017-03 B
Output be-
2016-12 A
2017-02 A
2017-03 A
2017-07 A
2017-09 A
2017-11 A
similarly for B.
Thanks
November 10, 2017 at 5:32 am
Papil - Friday, November 10, 2017 4:21 AMplease help in finding missing months from the last one year- 2016-11 till 2017-11.
My data looks like-Column1 column2
2016-11 A
2017-01 A
2017-04 A
2017-05 A
2017-06 A
2017-08 A
2017-10 A
2016-11 B
2017-03 BOutput be-
2016-12 A
2017-02 A
2017-03 A
2017-07 A
2017-09 A
2017-11 Asimilarly for B.
Thanks
Can you provide your data in consumable format, please?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
November 10, 2017 at 5:39 am
WITH Calendar AS (
SELECT FirstOfMonth = DATEADD(MONTH,n-1,Startdate)
FROM (SELECT Startdate = '20161101', EndDate = '20171101') d
CROSS APPLY (
SELECT TOP(1 + DATEDIFF(MONTH,Startdate, EndDate))
n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n)
) x
),
Matrix AS (
SELECT
Column1 = CONVERT(CHAR(7),FirstOfMonth,121),
Column2
FROM Calendar c
CROSS JOIN (VALUES ('A'),('B')) d (Column2)
)
SELECT *
FROM Matrix m
WHERE NOT EXISTS (
SELECT 1 FROM [MyTableWithMissingValues] t
WHERE t.Column2 = m.Column2
AND t.Column1 = m.Column1)
ORDER BY Column2, Column1
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
November 10, 2017 at 5:42 am
DDL would be really helpful here as looking at your data, you aren't storing your dates, as dates.
Firstly, have a look at having a Calendar table; they're a staple to a lot of date questions: http://www.sqlservercentral.com/articles/calendar/145206/. Then, you can do something like this. There is likely a quicker way, but does the job. Note, however, that it doesn't include 2017-11 as you dates stop at 2017-10 for 'A'. Without further logic, then I have no idea where this should stop.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
November 10, 2017 at 5:50 am
ChrisM@Work - Friday, November 10, 2017 5:39 AMWITH Calendar AS (
SELECT FirstOfMonth = DATEADD(MONTH,n-1,Startdate)
FROM (SELECT Startdate = '20161101', EndDate = '20171101') d
CROSS APPLY (
SELECT TOP(1 + DATEDIFF(MONTH,Startdate, EndDate))
n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n)
) x
),
Matrix AS (
SELECT
Column1 = CONVERT(CHAR(7),FirstOfMonth,121),
Column2
FROM Calendar c
CROSS JOIN (VALUES ('A'),('B')) d (Column2)
)
SELECT *
FROM Matrix m
WHERE NOT EXISTS (
SELECT 1 FROM [MyTableWithMissingValues] t
WHERE t.Column2 = m.Column2
AND t.Column1 = m.Column1)
ORDER BY Column2, Column1
it worked. thanks
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply