Thursday, November 28, 2013

How do I Join Calendar table with other tables with sparse dates

From your join conditions, I would assume that the key for all three tables is (Brand, Vector, xxxxDate). In such case, I am somewhat in doubt of what output you are looking for. I can see that if there are no rows for a certain date, that you want 0 for all three Qty columns. But what if there for a certain Brand and Vendor there is on row on a certain day?


On the other had, if you also have tables with all brands and vendors, it makes more sense - and the query would actually be easier to write.


Generally, for this type of question, it helps a lot if you provide sample data as INSERT statements and the expected result given the sample.





Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

No comments:

Post a Comment