>>TG - No, that doesnt work.care to expand on why didn't it work? Errors? wrong results? wrong order?Here is some sample code with results. Why don't you post what the output should be based on this data (or add you own data)use pubsset nocount oncreate table calendar (mydate datetime, myPeriod int)create table myTable (mydate datetime, myPeriod int, myArea varchar(3), myValue numeric(4,2))goinsert calendarselect '1-mar-2006', 1 union allselect '1-mar-2006', 2 union allselect '1-mar-2006', 3insert myTableselect '1-mar-2006', 1, 'AAA', 45.67 union allselect '1-mar-2006', 1, 'AAA', 23.34 union allselect '1-mar-2006', 1, 'BBB', 67.23 union allselect '1-mar-2006', 1, 'CCC', 12.78 union allselect '1-mar-2006', 2, 'AAA', 18.95 union allselect '2-mar-2006', 1, 'AAA', 10.11declare @startDate datetime, @enddate datetimeselect @startDate = '2006-3-1' ,@enddate = '2006-3-2'select a.myDate ,a.myPeriod ,b.myArea ,b.myValuefrom dbo.calendar aleft join dbo.myTable b on b.mydate = a.mydate and b.myperiod = a.myperiod --and b.buysell = 'sell'where a.mydate BETWEEN @StartDate AND @EndDateunion allselect b.myDate ,b.myPeriod ,b.myArea ,b.myValuefrom dbo.myTable bleft join dbo.calendar a on a.mydate = b.mydate and a.myperiod = b.myperiodwhere b.mydate BETWEEN @StartDate AND @EndDate--and b.buysell = 'sell'and a.mydate is null --exclude matching rows from calendarorder by 1,2godrop table myTabledrop table calendar--====================================================================================output:myDate myPeriod myArea myValue ------------------------------------------------------ ----------- ------ ------- 2006-03-01 00:00:00.000 1 AAA 45.672006-03-01 00:00:00.000 1 AAA 23.342006-03-01 00:00:00.000 1 BBB 67.232006-03-01 00:00:00.000 1 CCC 12.782006-03-01 00:00:00.000 2 AAA 18.952006-03-01 00:00:00.000 3 NULL NULL --row from calendar that isn't in myTable2006-03-02 00:00:00.000 1 AAA 10.11 --row from myTable that isn't in Calendar
Be One with the OptimizerTG