SQL server left join not returning expected records from left table

A.Steer

I have two objects within a SQl Server 2008 R2 database, which I am trying to join together with a left join but I am unable to get the left join to return all records from the table.

  • 1 table - tt_activityoccurrence
  • 1 view - vw_academicweeks

The vw_academicweeks, is a view that contains for each academic year a week number, and the first day and last day of the week and contains 52 records for each academic year.

tt_activityoccurrence is a table which contains occurrences of lessons within a year, lessons will not occur in all 52 weeks of the year.

With my query I am trying to return all instances from the vw_academicweeks view to return the following information

+------------+------------+------------+------------+---------+
| ActivityID | WeekStart  | StartTime  |  EndTime   | week_no |
+------------+------------+------------+------------+---------+
|      59936 | 04/09/2017 | 05/09/2017 | 05/09/2017 |       6 |
|      59936 | 11/09/2017 | 12/09/2017 | 12/09/2017 |       7 |
|      59936 | 18/09/2017 | 19/09/2017 | 19/09/2017 |       8 |
|      59936 | 25/09/2017 | 26/09/2017 | 26/09/2017 |       9 |
|      59936 | 02/10/2017 | 03/10/2017 | 03/10/2017 |      10 |
|      59936 | 09/10/2017 | 10/10/2017 | 10/10/2017 |      11 |
|      59936 | 16/10/2017 | 17/10/2017 | 17/10/2017 |      12 |
|      59936 | Null       | Null       | Null       |      13 |
|      59936 | 30/10/2017 | 31/10/2017 | 31/10/2017 |      14 |
|      59936 | 06/11/2017 | 07/11/2017 | 07/11/2017 |      15 |
|      59936 | 13/11/2017 | 14/11/2017 | 14/11/2017 |      16 |
|      59936 | 20/11/2017 | 21/11/2017 | 21/11/2017 |      17 |
|      59936 | 27/11/2017 | 28/11/2017 | 28/11/2017 |      18 |
|      59936 | 04/12/2017 | 05/12/2017 | 05/12/2017 |      19 |
|      59936 | 11/12/2017 | 12/12/2017 | 12/12/2017 |      20 |
|      59936 | 18/12/2017 | 19/12/2017 | 19/12/2017 |      21 |
|      59936 | Null       | Null       | Null       |      22 |
|      59936 | Null       | Null       | Null       |      23 |
+------------+------------+------------+------------+---------+

With the left join I can return all values except the nulls, so that the week_no column is missing rows, 13,22 and 23. I have also tried this with an outer join but receive the same information.

I feel I am missing something obvious but it is escaping me at the moment.

select 
    ttao.ActivityID
    ,dateadd(dd,datediff(dd,0,DATEADD(dd, -(DATEPART(dw, ttao.StartTime)-1), ttao.StartTime)),0) WeekStart
    ,ttao.StartTime
    ,ttao.EndTime
    ,aw.week_no

from

vw_AcademicWeeks AW
left join TT_ActivityOccurrence TTAO on
(dateadd(dd,datediff(dd,0,DATEADD(dd, -(DATEPART(dw, ttao.StartTime)-1), ttao.StartTime)),0))=aw.ay_start
where 

ay_code='1718' and
TTAO.ActivityID='59936'

order by aw.week_no asc
scsimon

Your where clause makes it an inner join by eliminating rows outside of the scope of your join. You need to move this logic up to your join statement. Note, I didn't validate your join condiditon (the dateadd...datediff logic)

select 
    ttao.ActivityID
    ,dateadd(dd,datediff(dd,0,DATEADD(dd, -(DATEPART(dw, ttao.StartTime)-1), ttao.StartTime)),0) WeekStart
    ,ttao.StartTime
    ,ttao.EndTime
    ,aw.week_no    
from    
vw_AcademicWeeks AW
left join TT_ActivityOccurrence TTAO on
    (dateadd(dd,datediff(dd,0,DATEADD(dd, -(DATEPART(dw, ttao.StartTime)-1), ttao.StartTime)),0)) = aw.ay_start
    and ay_code='1718' 
    and TTAO.ActivityID='59936'    
order by aw.week_no asc

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Not getting all records from Table A in left join

From Java

Left Join Not Returning Expected Results

From Dev

Updating sql table with results from left join (sql server)

From Dev

Updating sql table with results from left join (sql server)

From Dev

LEFT join not returning all rows from LEFT table

From Dev

LEFT JOIN does not return all the records from the left side table

From Dev

Left Join not returning all rows in left table

From Dev

Returning distinct records based on left join

From Dev

Left Outer Join not returning expected result

From Dev

MySQL - SQL LEFT JOIN selects null records not found on other table

From Dev

Left join is not returning all rows from other table

From Dev

Linq - Inner Join not retrieving all records from left table

From Dev

unable to get records from left join in 0:m relationship table

From Dev

Linq - Inner Join not retrieving all records from left table

From Dev

unable to get records from left join in 0:m relationship table

From Dev

Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN

From Dev

Which join i want to use to get rows from left table that not found in right table in SQL Server

From Dev

MySQL returning all the records from the left table excluding the records that have a match in the right table

From Dev

Left join to same table SQL

From Dev

SQL LEFT JOIN ON A SINGLE TABLE

From Dev

SQL LEFT JOIN in history table

From Dev

SQL Server UPDATE with left join?

From Dev

SQL Server Left Join With 'Or' Operator

From Dev

Left Outer Join in SQL Server

From Dev

SQL SERVER: Left join and subquery

From Dev

SQL SERVER: Left join and subquery

From Dev

SQL Server Left Join Counting

From Dev

mysql left join and group by not returning all results in left table

From Dev

Access SQL LEFT JOIN not returning results

Related Related

  1. 1

    Not getting all records from Table A in left join

  2. 2

    Left Join Not Returning Expected Results

  3. 3

    Updating sql table with results from left join (sql server)

  4. 4

    Updating sql table with results from left join (sql server)

  5. 5

    LEFT join not returning all rows from LEFT table

  6. 6

    LEFT JOIN does not return all the records from the left side table

  7. 7

    Left Join not returning all rows in left table

  8. 8

    Returning distinct records based on left join

  9. 9

    Left Outer Join not returning expected result

  10. 10

    MySQL - SQL LEFT JOIN selects null records not found on other table

  11. 11

    Left join is not returning all rows from other table

  12. 12

    Linq - Inner Join not retrieving all records from left table

  13. 13

    unable to get records from left join in 0:m relationship table

  14. 14

    Linq - Inner Join not retrieving all records from left table

  15. 15

    unable to get records from left join in 0:m relationship table

  16. 16

    Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN

  17. 17

    Which join i want to use to get rows from left table that not found in right table in SQL Server

  18. 18

    MySQL returning all the records from the left table excluding the records that have a match in the right table

  19. 19

    Left join to same table SQL

  20. 20

    SQL LEFT JOIN ON A SINGLE TABLE

  21. 21

    SQL LEFT JOIN in history table

  22. 22

    SQL Server UPDATE with left join?

  23. 23

    SQL Server Left Join With 'Or' Operator

  24. 24

    Left Outer Join in SQL Server

  25. 25

    SQL SERVER: Left join and subquery

  26. 26

    SQL SERVER: Left join and subquery

  27. 27

    SQL Server Left Join Counting

  28. 28

    mysql left join and group by not returning all results in left table

  29. 29

    Access SQL LEFT JOIN not returning results

HotTag

Archive