How will I get the all the date even though there is no order between two dates?
Sample Table
OrderID | Date | CusID
33942 | 6-21-2014 | 6005
34059 | 6-20-2014 | 4003
53333 | 6-23-2014 | 6005
59234 | 6-23-2014 | 4003
How I could have this result?
CusID | Date | OrderID |
4003 | 6-20-2004 | 34059 |
4003 | 6-21-2004 | null |
4003 | 6-22-2004 | null |
4003 | 6-23-2004 | 59234 |
6005 | 6-20-2004 | null |
6005 | 6-21-2004 | 33942 |
6005 | 6-22-2004 | null |
6005 | 6-23-2004 | 53333 |
This what I did so far.
I create a calendar table
CREATE TABLE #Calendar
(
[CalendarDate] DATETIME
)
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @EndDate = GETDATE()
SET @StartDate = DATEADD(Year, -1, @EndDate)
WHILE @StartDate <= @EndDate
BEGIN
INSERT INTO #Calendar
(
CalendarDate
)
SELECT
@StartDate
SET @StartDate = DATEADD(dd, 1, @StartDate)
END
Then here is my query but does not give me the needed result
Select t.CusID, c.CalendarDate, t.OrderID
From #Calendar c
left outer join
#temp t
ON
CONVERT(VARCHAR(10), c.CalendarDate, 112) = CONVERT(VARCHAR(10), t.Date, 112)
Where c.CalendarDate Between '6-20-2014' and '6-23-2014'
Order By t.Name