Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2010-03-03 : 12:26:45
|
Here's the Tables and existing query, which I am having trouble getting to work right.Declare @DateToCheck datetime set @DateToCheck ='6/1/2009'Declare @Dates table (MyDate datetime)Declare @Invoice table (DateReceived datetime,InvoiceDate datetime)Declare @Interest table (StartDate datetime,EndDate datetime)Declare @Close table (DateClosed datetime)insert into @Datesselect '1/1/2009' Union allselect '2/1/2009' Union allselect '3/1/2009' Union Allselect '4/1/2009' Union allselect '5/1/2009' Union allselect '6/1/2009' Union allselect '7/1/2009' Union allselect '8/1/2009' Union allselect '9/1/2009' Union allselect '10/1/2009' Union allselect '11/1/2009' Union allselect '12/1/2009' Union allselect '1/1/2010' Union allselect '2/1/2010'insert into @Invoiceselect '1/1/2009','1/1/2009' Union allselect '2/1/2009','2/1/2009' Union allselect '8/1/2009','3/1/2009' Union allselect '8/1/2009','4/1/2009' Union allselect '8/1/2009','5/1/2009' Union allselect '2/1/2010','2/1/2010'Insert into @Interest select '1/1/2009', '5/1/2009' Union allselect '6/1/2009', '12/1/2009'insert into @Closeselect '3/1/2009' Select * from@Dates aLeft Join@Invoice bon a.MyDate = b.InvoiceDateLeft Join@Interest con case when b.DateReceived > @Datetocheck then b.DateReceived else a.Mydate END between c.StartDate and c.EndDateLeft Join@Close don (isnull(b.DateReceived,1) > @DateToCheck and a.MyDate = d.DateClosed and isnull(b.DateReceived,0) = isnull(b.InvoiceDate,0) )or (isnull(b.DateReceived,1) <= @DateToCheck and a.Mydate = d.DateClosed )[code]Now what I need is to somehow take the @Close table and have it create a extra record with null values accross the board using a conditional statement.The logic is as follows. If the a.datereceived in the above query is <= the @datetocheck, I just do a join on a.Mydate = @Close.DateClosedHowever if the a.DateReceived >= the @DateToCheck, and the INVOICEDate <> the DATERECEIVED then I want it to create a blank record. So here are my current results.[code]MyDate DateReceived InvoiceDate StartDate EndDate DateClosed----------------------- ----------------------- ----------------------- ----------------------- ----------------------- -----------------------2009-01-01 00:00:00.000 2009-01-01 00:00:00.000 2009-01-01 00:00:00.000 2009-01-01 00:00:00.000 2009-05-01 00:00:00.000 NULL2009-02-01 00:00:00.000 2009-02-01 00:00:00.000 2009-02-01 00:00:00.000 2009-01-01 00:00:00.000 2009-05-01 00:00:00.000 NULL2009-03-01 00:00:00.000 2009-08-01 00:00:00.000 2009-03-01 00:00:00.000 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-04-01 00:00:00.000 2009-08-01 00:00:00.000 2009-04-01 00:00:00.000 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-05-01 00:00:00.000 2009-08-01 00:00:00.000 2009-05-01 00:00:00.000 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-06-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-07-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-08-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-09-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-10-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-11-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-12-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2010-01-01 00:00:00.000 NULL NULL NULL NULL NULL2010-02-01 00:00:00.000 2010-02-01 00:00:00.000 2010-02-01 00:00:00.000 NULL NULL NULLHere are the Results I want (SEE THE LAST RECORD)MyDate DateReceived InvoiceDate StartDate EndDate DateClosed----------------------- ----------------------- ----------------------- ----------------------- ----------------------- -----------------------2009-01-01 00:00:00.000 2009-01-01 00:00:00.000 2009-01-01 00:00:00.000 2009-01-01 00:00:00.000 2009-05-01 00:00:00.000 NULL2009-02-01 00:00:00.000 2009-02-01 00:00:00.000 2009-02-01 00:00:00.000 2009-01-01 00:00:00.000 2009-05-01 00:00:00.000 NULL2009-03-01 00:00:00.000 2009-08-01 00:00:00.000 2009-03-01 00:00:00.000 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-04-01 00:00:00.000 2009-08-01 00:00:00.000 2009-04-01 00:00:00.000 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-05-01 00:00:00.000 2009-08-01 00:00:00.000 2009-05-01 00:00:00.000 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-06-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-07-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-08-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-09-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-10-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-11-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2009-12-01 00:00:00.000 NULL NULL 2009-06-01 00:00:00.000 2009-12-01 00:00:00.000 NULL2010-01-01 00:00:00.000 NULL NULL NULL NULL NULL2010-02-01 00:00:00.000 2010-02-01 00:00:00.000 2010-02-01 00:00:00.000 NULL NULL NULL2009-03-01 00:00:00.000 NULL NULL NULL NULL 2009-03-01 00:00:00.000 See how even though the dateclosed matched the mydate on the 3/1/2009 record, since the datereceived on the 3/1/2009 record was > than the @datetocheck and also the datereceived <> invoicedate mean I do not want it to link. When there is no match I want a blank record as illustrated at the end, not NO record as it is currently doing.I spent a while on this and it's one of those things that has become far more difficult than I hopedThanks Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
|
|
DP978
Constraint Violating Yak Guru
269 Posts |
Posted - 2010-03-03 : 12:42:25
|
Well you can perform a UNION on you results to get that last line, your condition may be confusing though, since there are multiple Invoice Dates to check against.To append the last line just doUnion AllSelect DateClosed, NULL, NULL, NULL, NULL, DateClosed From @CloseDeclare @DateToCheck datetime set @DateToCheck ='6/1/2009'Declare @Dates table (MyDate datetime)Declare @Invoice table (DateReceived datetime,InvoiceDate datetime)Declare @Interest table (StartDate datetime,EndDate datetime)Declare @Close table (DateClosed datetime)insert into @Datesselect '1/1/2009' Union allselect '2/1/2009' Union allselect '3/1/2009' Union Allselect '4/1/2009' Union allselect '5/1/2009' Union allselect '6/1/2009' Union allselect '7/1/2009' Union allselect '8/1/2009' Union allselect '9/1/2009' Union allselect '10/1/2009' Union allselect '11/1/2009' Union allselect '12/1/2009' Union allselect '1/1/2010' Union allselect '2/1/2010'insert into @Invoiceselect '1/1/2009','1/1/2009' Union allselect '2/1/2009','2/1/2009' Union allselect '8/1/2009','3/1/2009' Union allselect '8/1/2009','4/1/2009' Union allselect '8/1/2009','5/1/2009' Union allselect '2/1/2010','2/1/2010'Insert into @Interest select '1/1/2009', '5/1/2009' Union allselect '6/1/2009', '12/1/2009'insert into @Closeselect '3/1/2009' Select * from@Dates aLeft Join@Invoice bon a.MyDate = b.InvoiceDateLeft Join@Interest con case when b.DateReceived > @Datetocheck then b.DateReceivedelse a.MydateEND between c.StartDate and c.EndDateLeft Join@Close don(isnull(b.DateReceived,1) > @DateToCheck and a.MyDate = d.DateClosedand isnull(b.DateReceived,0) = isnull(b.InvoiceDate,0))or (isnull(b.DateReceived,1) <= @DateToCheck and a.Mydate = d.DateClosed )Union AllSelect DateClosed, NULL, NULL, NULL, NULL, DateClosed From @Close |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2010-03-03 : 12:55:46
|
I don't want to use a union I want the query to run as a dependent since there are numerous other factors in the production query than I mentioned.. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2010-03-03 : 13:29:24
|
I used a recursive CTE to solveDeclare @DateToCheck datetime set @DateToCheck ='5/1/2009'Declare @Dates table (MyDate datetime)Declare @Invoice table (DateReceived datetime,InvoiceDate datetime)Declare @Interest table (StartDate datetime,EndDate datetime)Declare @Close table (DateClosed datetime)insert into @Datesselect '1/1/2009' Union allselect '2/1/2009' Union allselect '3/1/2009' Union Allselect '4/1/2009' Union allselect '5/1/2009' Union allselect '6/1/2009' Union allselect '7/1/2009' Union allselect '8/1/2009' Union allselect '9/1/2009' Union allselect '10/1/2009' Union allselect '11/1/2009' Union allselect '12/1/2009' Union allselect '1/1/2010' Union allselect '2/1/2010'insert into @Invoiceselect '1/1/2009','1/1/2009' Union allselect '2/1/2009','2/1/2009' Union allselect '8/1/2009','3/1/2009' Union allselect '8/1/2009','4/1/2009' Union allselect '8/1/2009','5/1/2009' Union allselect '2/1/2010','2/1/2010'Insert into @Interest select '1/1/2009', '5/1/2009' Union allselect '6/1/2009', '12/1/2009'insert into @Closeselect '3/1/2009' ;With t (MyDate,DateReceived,InvoiceDate,StartDate,EndDate,DateClosed)as (Select Mydate,DateReceived,InvoiceDate,StartDate,EndDate,DateClosed from@Dates aLeft Join@Invoice bon a.MyDate = b.InvoiceDateLeft Join@Interest con case when b.DateReceived > @Datetocheck then b.DateReceived else a.Mydate END between c.StartDate and c.EndDateLeft Join@Close don(isnull(b.DateReceived,1) <= @DateToCheck and a.Mydate = d.DateClosed )Union AllSelect t.mydate,null,null,null,null,c.dateclosedfrom t Inner Join@Close con t.Mydate = c.DateClosedwheret.DateReceived > @Datetocheck)select * from tOPTION (MAXRECURSION 0) Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
|
|
|
|
|