Get empty rows based on sequence table

  • I am trying to get a list of rows for each customer based on quarters of a year.  If I ask for a particular year, I would get 4 records even if they don't already exist for each quarter.  If one exists, I should get 1 row with data and three rows with some customer data and nulls in the other fields.

    I can get it work fine if there are no records using a Cross Join, but if there is even one record I get 4 identical records.

    If you run the following script you can see the issue:

    This is what I would expect if there are no records
    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa4AAABmCAYAAACTOXX3AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAA8USURBVHhe7d3vaxtHGgfwx/cPpO9yTTHlclYKRiT4TSgW9+bApLJDkTnQixyHDg4srhzIFGwI6EVeCAIxBPtNgw2BitK88F2J6V2ihMD1xWFR8sakCEMjkaM17ZF3zV+Qm2d3Vlr9GGlX3tndGX8/ICKttdqdmd19ZnY3+8y8FQgAAMAQM3//6p8IXAAAYIzuiOvnn3+mCxcuOBNhmMn1g7ZNP7SRmm11Y1p5klxf1bJ/Jf8FAAAwAgIXAAAYBYELAACMEihwdXbKlJuZoRl+5XJUbnTkX1Kos0O53A45a8jvvfV2XhrW3VlGjnb8P+tfB0+j7Cx/6Hu8XuWGnMA6tJPjelaVwX31zXIKRrWtZ1RdxsnfvkPtE30dYv9Tm1g3Q9tKhxpinqFtp7FD5Z2Gu94JCl8eV0dM7803onyaJLXciYGrs5OjzP48Vdtvie/jeFuvEi2Xwq2gf2OO2+I2tXm9+dWu0nwtQ7nIa7dJ66Xx5WsctGhtLUv7jwa+tbhIi62XvXk7j2i/Kd97/GWQr928/NspmNq2yrocNHgQHbee3t+nKY/GbQz7n1qQuhneVuYovzJP+5ky9fp+DSov79P8Sl78NTnTlUcQwaNUI998BTouyeCms+2TWq4wIXB16NE+0Xa9QnmvRefytPv2kCpJtvC0xLpX6tskWj3aChU753Z2n7aUo6AOvWxlqbBboOzQsotUFPN2t8P2MYktkxblR31MbdtxdTnGXIUODyvqA9OkvwcV6TaG/U8tSN0othXR1v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JBgUQ7ncfp7AdGBGyYd+HTAQ5wqY735FmR2b03vnv8guFFUKulyfinAuoZoJgvA7IF2S6xMFLBs++J/KeBaIqddFUgvA/p8GieZXRNbhxDihdOXSXyvI6CbhCaRuH2R2VYjOzdLDhsZYDo0YZe6n18KsG5xsoufdYeyVJP+O9PlIHu3HBqe85wQ4hlZhPFC4u1VB7b0TA6v3sbwZYdjWrnhb0Ro7/iRXwowQkjPIMJLgsV+7RBe/Z/HUDz+zVwBfx83FinYXwFiuUCLaRMAN22dB3cloj/5pQDrFpF7GMUCXpl5LXFNlCeWCSEtc/ItXqRjeFZteV3/ouztUAPtX8URXLuuL+ogAzIwesevySd7gYl1rMyPIDp/gNTjoE6G+ZNfCrCuoRp46TmQMRqKuDhC49smJPgcZhdQxHdYlkVS5tAWQOQbTEe38HT4LlLDw3qRwb2L3PQnymKbaOB7xFe4ma9HwryhaK9thtqLsI3cabluZCcOpUgGJSpz2Nrar/zKc7mZLyHkchMJjvAi7YECjBBCSCihACOEEBJKKMAIIYSEkr6zs7OKRRyEEEJIUHBbwCGcW4VICCGEhAG6EAkhhIQSCjBCCCGhhAKMEEJIKKEAI4QQEkKA/wc1iVR1bRk/SAAAAABJRU5ErkJggg==


    DECLARE @Quarters TABLE
    (
       Qtr int
    )
    DECLARE @Customers TABLE
    (
       CustomerID INT,
       NAME varchar(100)
    )
    DECLARE @CustomerActivity TABLE
    (
       CustomerActivityID INT,
       CustomerID INT,
       CAYear INT,
       CAQtr int
    )
    INSERT @Customers
    (
       CustomerID,
        NAME
    )
    VALUES
    (
       1,
       'Tom'
    )
    INSERT @Quarters
    SELECT TOP (4) n = CONVERT(INT, ROW_NUMBER() OVER (ORDER BY s1.[object_id]))
    FROM sys.all_objects AS s1

    SELECT *
    FROM @Customers c
    LEFT JOIN @CustomerActivity ca
    ON c.CustomerID = ca.CustomerID
    RIGHT JOIN @Quarters qt
    ON qt.Qtr = ca.CAQtr


    INSERT @CustomerActivity
    (
        CustomerActivityID,
        CustomerID,
        CAYear,
        CAQtr
    )
    VALUES
    (   1, -- CustomerActivityID - int
        1, -- CustomerID - int
        2017, -- CAYear - int
        4  -- CAQtr - int
        )

    SELECT *
    FROM @Customers c
    LEFT JOIN @CustomerActivity ca
    ON c.CustomerID = ca.CustomerID
    RIGHT JOIN @Quarters qt
    ON qt.Qtr = ca.CAQtr

    I tried to use a right join and got the 4 records, but the customer detail is missing:

    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAasAAABhCAYAAABoFY4LAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAABBtSURBVHhe7Z3faxtXFse/3n8gfUvb4JZ1rRSMSTAsIdj0pWAS2SHYFPSQsmhhwaJlwSZgQ0APfhAEbAj2S4INgYqSPHhbYrpN5GBoHxaLkheTIgyNtF5a03b91vwF2XtGM9Loxx1rpJE0d/T9wOCZka7uPefMzLnnzvU9Q//8+l9vQAghhISYoTcKex+//fYb3n33XfuI1GOyfmjb8EMb6YmabkyTp5/tder+k31MCCGEhBY6K0IIIaGHzooQQkjoadlZlTZTmBoawpBsU1NI5Ur2JyGktImpqU1YLZR9p93W1oW2W3VMYdP9s+42OORSVv0N35N2pXL2CaGEzSnRs06G8lZTpAOMsq1DM132Erd9G+wTvA55/+k5UzcN10oJOVWm4drJbSK1mSu3u4/4l6dMSZ2vlmsiX5foVb0tOavS5hRiO2NIF99A5mO8yaaBmaS/Rrkv4F4zuYGitFu2YhpjmRimAtdoHktJb/lyuwUsLIxj52ndtyYnMVl4VS1beoqdvL3v4JbB3rbi9mcdYKpttbqsp/7B6dVO5/N25OniNcb7T08rumm8VkYRnx3DTiyFan8vh9TMDsZm4+rT/tGePArlMJIZuMrN4ShpO7Ru2r6H9bbgrEp4ugNsZBcRd6w4GsfWmwMs9tOq7aLavpjdgLJ0sMZTN+TG+A7WtdFOCa8K45jbmsN4Q90JJFTZyrVXPIK6GjFpH3YPU23rpUsPRhdxcLCofxid9XmrBH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bAU3gs200T263F46q54gPY47wMKhNbRw79oR1irL9R/hQnLfGm7A+hpg7c/jZHuPWWAJaYPTxyl8ibu40WGKitN/qyhhwYnWes0+Hm4D80a8QhC63146q15wsocfrJun3OU4/9E8YsVi2RldcvXc3PuEEN/IBIkHz+dxL9upk9nHt8/H8JeAczK1yunjNZxcW+zfuzKf9KK9dFaEkEggjuq2clSfuRzV+ffHUDz+j30E/O+4tQy2/XUW5WHM4vqE9Xrg9np5P7wzAnvTXjqrXjB8HVdxB9/a76FkeIHp4AkJkJP7eLA+hpX6iOq9D6ujGOqh+nvxCi68Zx14IA9f4OpH/XpZ5Ez+KG/3lq8gtnyI1VthfXnVm/bSWfUEZczMXWDb7nnIMIUxY9GEhJ+X2Tso4gusyQQme7N69sOfYz72BLcnprE6MWFNALh+1lD7iYrErrXwPdJTuJCtD0xeTDNqC4FGEdpIT8e6kRUsVKfxfEgckGm27ld7pV4uZEsIGRyGw+OoSHvQWRFCCAk9dFaEEEJCD50VIYSQ0MO09oQQQkKJM7lCqJkNSAghhIQP4P+/LjAGrX45FQAAAABJRU5ErkJggg==

    SELECT *
    FROM @Customers c
    LEFT JOIN @CustomerActivity ca
    ON c.CustomerID = ca.CustomerID
    RIGHT JOIN @Quarters qt
    ON qt.Qtr = ca.CAQtr

    Thanks,

    Tom

  • Part of the issue is that you're joining in the wrong order and part is that you need to specify a more complete condition on the join.

    SELECT *
    FROM @Customers c
    CROSS JOIN @Quarters q  /*  Do the CROSS JOIN before the LEFT OUTER JOIN  */
    LEFT JOIN @CustomerActivity ca
      ON c.CustomerID = ca.CustomerID
       AND q.Qtr = ca.CAQtr  /*  Include the quarter in the JOIN conditions  */

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • That appears to be it.

    I am going to try it on my bigger query but it makes absolute sense once I saw it.

    Thanks a lot

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply