The effective date I am looking for is the one that applies to the transaction. In the above example, the 6/2003 transaction would need the contract that is effective 5/2003 not the 3/2003 effective date which was valid only for transactions in 3/2003 and 4/2003.
My question is simply is there a way to write a SELECT statement joining the transaction with the appropriate entry in any of the Contract tables and not return a row for each effective date prior to the transaction date?
Okay, I think (finally, god forbid) I understand. Going back to the example data above;
Table A (Transaction Table)
Cont # Year/Month
1215 200306
Table B (Contract Table)
Cont # Eff Year/Month
1215 200303
1215 200305
Breaking down your description to hopefully understand...
- Looking for the date that applies to the Transaction (200306).
- We need the Contract that is effective for this Transaction, which is my understanding that it would be the Contract date closest to the Transaction date (200305 in this case, or 200303 if 200305 did not exist)
- Only return ONE Contract for the Transaction, and that ONE Contract is as described above.
If this is incorrect, please correct me. Assuming this is correct, the select statement would simply be;
SELECT TOP 1 Transaction.SomeColumn, Contract.SomeColumn FROM Transaction JOIN Contract (Transaction.ContractNumber = Contract.ContractNumber AND Transaction.Date >= Contract.Date) ORDER BY Contract.Date DESC
Please let me know if my explanation was correct, but the SELECT statement did not perform correctly. If it didnt perform correctly then please tell me what the output was, so I can attempt to fix it.
As with others who seem to get frustred, I always recommend going to the news group microsoft.public.sqlserver.programming as another source of help (I do all the time). Transact SQL professionals frequently answer questions on there. This is of course assuming that youre using SQL Server or MSDE as your database.
A word about the date (month & year only) in a string field, not a datetime field. When I started this project in January I put considerable thought in deciding how to store dates. I started using the datetime fields and it was akward. I switched to using a string and it has been much easier. I have a dll that will do any operation on a string date that you can do with a date from a datetime field. This dll works with either a 6 char date (yyyymm) or a 8 char date (yyyymmdd). I will never use a datetime field in any project.
It may be easy now, but it wont be in the future if you lose database integrity. You can insert anything into that varchar field of yours, and thats just bad. DateTime field insures a date in some fasion or another will come out of it. Also using functions to constantly parse dates from string format is some nice added overhead for yourself. Ew.
There is nothing hard about using a DateTime column in SQL. I strongly suggest you look into using it again, especially since there are techniques that you can use with SQL code to manipulate and parse DateTime columns to get full control of it.