-2

I was not sure about a descriptive Title of this question so I put there what i thought to be the best sentence.

I have a table like this:

StudentID, Month, Year, Present
-------------------------------
1, 1, 2016, P
1, 12, 2016, P
1, 4, 2017, P

I want to write SQL statement, without using cursor, to return a resultset like the below one:

Required output:

1, 1, 2016, P -- <-- it is 'P' in the table
1, 2, 2016, A
1, 3, 2016, A
1, 4, 2016, A
1, 5, 2016, A
1, 6, 2016, A
1, 7, 2016, A
1, 8, 2016, A
1, 9, 2016, A
1, 10, 2016, A
1, 11, 2016, A
1, 12, 2016, P -- <-- it is 'P' in the table
1, 1, 2017, A
1, 2, 2017, A
1, 3, 2017, A
1, 4, 2017, P -- <-- it is 'P' in the table
1, 5, 2017, A
1, 6, 2017, A
1, 7, 2017, A
1, 8, 2017, A
1, 9, 2017, A
1, 10, 2017, A
1, 11, 2017, A
1, 12, 2017, A

is this possible through one SQL statement, without using cursors.

I am working on SQL Server 2000 database.

5
  • 2
    SQL Server 2000 limits the case quite a bit... otherwise I'd say construct the desired interval via a CTE and then left join the present table on it. Does it have to be SQL Server 2000?
    – Adwaenyth
    Jul 11, 2017 at 12:36
  • You'd be surprised how many people are using older versions of SQL Server. I know I was when I was at a SQL Server user group last year and the speaker asked for a show of hands on which version people were using. As he went backwards through the list, he actually said "I'm almost afraid to ask... Anyone on SQL Server 6.5" and he still got a couple of hands go up. Jul 11, 2017 at 12:45
  • Yeah well we recently upgraded to SQL Server 2016 and I'm quite fond of it now. Couldn't imagine going back.
    – Adwaenyth
    Jul 11, 2017 at 12:48
  • This question gets asked so much, it was hard to choose which duplicate to site. There may be a better one. Jul 11, 2017 at 13:01

4 Answers 4

2

You can generate numbers 1 to 12 using tally table and then do cross apply as below:

;with cte1 as 
( select * from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) a(n) ) --number generation
,Cte_numbers as ( select num = Row_number() over (order by (SELECT NULL)) from cte1 c1, cte1 c2, cte1 c3)
Select s.StudentId, n.num as [Month], s.[Year], Coalesce(s1.Present,'A') as Present from (
    Select distinct StudentId, [YEAR] from #student ) s
    cross apply ( Select top (12) num from Cte_numbers ) n
left join #student s1
    on s.StudentId = s1.StudentId
    and s.[Year] = s1.[Year]
    and n.num = s1.[Month]
    order by s.StudentId, s.[Year]
4
  • 1
    No common table expressions in SQL Sever 2000 unfortunately. Jul 11, 2017 at 12:57
  • Then we might go for sub queries, same you can write in sub-query Jul 11, 2017 at 13:01
  • And no CROSS APPLY in SQL 2000 either.
    – Dan Guzman
    Jul 11, 2017 at 13:32
  • Here you can use Cross Join which is available in SQL Server 2000 Jul 11, 2017 at 13:45
0

You'll need to create tables for the missing entities. For instance, a Month entity with all months and Year with all years:

CREATE TABLE Month_Table (
    Month_ID int NOT NULL,
    PRIMARY KEY (Month_ID)
);
CREATE TABLE Year_Table (
    Year_ID int NOT NULL,
    PRIMARY KEY (Year_ID)
);

Fill Month_Table with numbers from 1 to 12, and Year_Table with numbers from 2016 to 2017.

Then use LEFT OUTER joins to fill the missing values with the ones provided by those tables. Use COALESCE to use the values from the auxiliary tables if your table doesn't contain them, and 'A' when your table doesn't contain the data:

SELECT T.StudentID,
   COALESCE(T.Month, Month_Table.Month_ID) Month,
   COALESCE(T.Year, Year_Table.Year_ID) Year,
   COALESCE(T.Present, 'A') Present
FROM YOUR_TABLE T
LEFT OUTER JOIN Month_Table ON Month_Table.Month_ID = YOUR_TABLE
LEFT OUTER JOIN Year_Table ON Year_Table.Year_ID = YOUR_TABLE.Year;
0

Here's two examples, one that uses CTEs, recursion, etc. just to show how easy this is in SQL Server 2005 onwards, then an example that should work in SQL Server 2000.

Like all the other answers this is basically using a numbers or tally table to do the heavy lifting. There's a good article on how to generate numbers here: https://sqlperformance.com/2013/01/t-sql-queries/generate-a-set-1

DECLARE @MyData TABLE (StudentId INT, [Month] INT, [Year] INT, Present CHAR(1));
INSERT INTO @MyData SELECT 1, 1, 2016, 'P';
INSERT INTO @MyData SELECT 1, 12, 2016, 'P';
INSERT INTO @MyData SELECT 1, 4, 2017, 'P';

--Recursive CTE to load numbers table dynamically
WITH Numbers AS (
    SELECT 1 AS [Month], 2016 AS [Year]
    UNION ALL
    SELECT CASE WHEN [Month] = 12 THEN 1 ELSE [Month] + 1 END, CASE WHEN [Month] = 12 THEN [Year] + 1 ELSE [Year] END 
    FROM Numbers 
    WHERE [Year] < 2017 OR [Month] < 12),
Students AS (
    SELECT DISTINCT StudentId FROM @MyData)
SELECT
    s.StudentId,
    n.[Year],
    n.[Month],
    ISNULL(m.Present, 'A') AS Present
FROM
    Students s
    CROSS JOIN Numbers n
    LEFT JOIN @MyData m ON m.StudentId = s.StudentId AND m.[Year] = n.[Year] AND m.[Month] = n.[Month]
OPTION (MAXRECURSION 0);

--SQL Server 2000 doesn't have CTEs so we have to use a different method
DECLARE @Numbers TABLE ([Year] INT, [Month] INT);
DECLARE @Year INT = 2016;
WHILE @Year < 2018
BEGIN
    DECLARE @Month INT = 1;
    WHILE @Month < 13
    BEGIN
        INSERT INTO @Numbers SELECT @Year, @Month;
        SELECT @Month = @Month + 1;
    END;
    SELECT @Year = @Year + 1;
END;

--SQL Server 2000 query
SELECT
    s.StudentId,
    n.[Year],
    n.[Month],
    ISNULL(m.Present, 'A') AS Present
FROM
    (SELECT DISTINCT StudentId FROM @MyData) s
    CROSS JOIN @Numbers n
    LEFT JOIN @MyData m ON m.StudentId = s.StudentId AND m.[Year] = n.[Year] AND m.[Month] = n.[Month];
-2

I think you can do it, by creating a table (tally) with all year-month you'll need. Sample data:

CREATE TABLE TD1 (YYEAR SMALLINT, MMONTH SmALLINT);
INSERT INTO TD1 VALUES (2016,1);
INSERT INTO TD1 VALUES (2016,2);
INSERT INTO TD1 VALUES (2016,3);
INSERT INTO TD1 VALUES (2016,4);
INSERT INTO TD1 VALUES (2016,5);
INSERT INTO TD1 VALUES (2016,6);
INSERT INTO TD1 VALUES (2016,7);
INSERT INTO TD1 VALUES (2016,8);
INSERT INTO TD1 VALUES (2016,9);
INSERT INTO TD1 VALUES (2016,10);
INSERT INTO TD1 VALUES (2016,11);
INSERT INTO TD1 VALUES (2016,12);
INSERT INTO TD1 VALUES (2017,1);
INSERT INTO TD1 VALUES (2017,2);
INSERT INTO TD1 VALUES (2017,3);
INSERT INTO TD1 VALUES (2017,4);
INSERT INTO TD1 VALUES (2017,5);
INSERT INTO TD1 VALUES (2017,6);
INSERT INTO TD1 VALUES (2017,7);
INSERT INTO TD1 VALUES (2017,8);
INSERT INTO TD1 VALUES (2017,9);
INSERT INTO TD1 VALUES (2017,10);
INSERT INTO TD1 VALUES (2017,11);
INSERT INTO TD1 VALUES (2017,12);

CREATE TABLE TS1 (StudentId INT, MMONTH SMALLINT, YYEAR SMALLINT, PRESENT CHAR(1))
INSERT INTO TS1 VALUES (1,1,2016,'P');
INSERT INTO TS1 VALUES (1,12,2016,'P');
INSERT INTO TS1 VALUES (1,4,2017,'P');
INSERT INTO TS1 VALUES (2,3,2017,'P');

Query:

SELECT  A.STUDENTID, TD1.MMONTH, TD1.YYEAR, COALESCE(TS1.PRESENT, 'A') AS PRESENT
FROM TD1 
CROSS JOIN (SELECT DISTINCT STUDENTID FROM TS1) A 
LEFT JOIN TS1 On TD1.MMONTH=TS1.MMONTH AND TD1.YYEAR=TS1.YYEAR AND A.STUDENTID = TS1.STUDENTID
ORDER BY A.STUDENTID, TD1.YYEAR, TD1.MMONTH;

Output:

+-----------+--------+-------+---------+
| STUDENTID | MMONTH | YYEAR | PRESENT |
+-----------+--------+-------+---------+
|         1 |      1 |  2016 | P       |
|         1 |      2 |  2016 | A       |
|         1 |      3 |  2016 | A       |
|         1 |      4 |  2016 | A       |
|         1 |      5 |  2016 | A       |
|         1 |      6 |  2016 | A       |
|         1 |      7 |  2016 | A       |
|         1 |      8 |  2016 | A       |
|         1 |      9 |  2016 | A       |
|         1 |     10 |  2016 | A       |
|         1 |     11 |  2016 | A       |
|         1 |     12 |  2016 | P       |
|         1 |      1 |  2017 | A       |
|         1 |      2 |  2017 | A       |
|         1 |      3 |  2017 | A       |
|         1 |      4 |  2017 | P       |
|         1 |      5 |  2017 | A       |
|         1 |      6 |  2017 | A       |
|         1 |      7 |  2017 | A       |
|         1 |      8 |  2017 | A       |
|         1 |      9 |  2017 | A       |
|         1 |     10 |  2017 | A       |
|         1 |     11 |  2017 | A       |
|         1 |     12 |  2017 | A       |
|         2 |      1 |  2016 | A       |
|         2 |      2 |  2016 | A       |
|         2 |      3 |  2016 | A       |
|         2 |      4 |  2016 | A       |
|         2 |      5 |  2016 | A       |
|         2 |      6 |  2016 | A       |
|         2 |      7 |  2016 | A       |
|         2 |      8 |  2016 | A       |
|         2 |      9 |  2016 | A       |
|         2 |     10 |  2016 | A       |
|         2 |     11 |  2016 | A       |
|         2 |     12 |  2016 | A       |
|         2 |      1 |  2017 | A       |
|         2 |      2 |  2017 | A       |
|         2 |      3 |  2017 | P       |
|         2 |      4 |  2017 | A       |
|         2 |      5 |  2017 | A       |
|         2 |      6 |  2017 | A       |
|         2 |      7 |  2017 | A       |
|         2 |      8 |  2017 | A       |
|         2 |      9 |  2017 | A       |
|         2 |     10 |  2017 | A       |
|         2 |     11 |  2017 | A       |
|         2 |     12 |  2017 | A       |
+-----------+--------+-------+---------+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.