| Author |
Topic |
|
magikminox
Starting Member
27 Posts |
Posted - 2008-04-18 : 04:22:05
|
| Hi GuysI have a table that contains codes for commodities.Some of the codes in this table have changed and some of them have not.So now i want to design a solution that enable me to map the new codes in a different mapping table to the old ones in the other table.I also want to retain the old codes because most of the archived data used the old codes.Where there is no new code, the current code is being retained.How do i design my table and queries so that i can use the new codes as if i was using the old code.I want to select products with a certain code but using the new code and mapping to the old codes or vice versa.The structure of the data is like this.Code NameAA AAAL AluminiumALM ALMALT Aluminium in tonnesAR ARAUD Australian DollarsAUJPY AUJPYCAQ CAQCC CCCCF CCFCER Carbon Emmission ReductionThe mapping table is like this:XAA AAXAL ALXMA ALMXAL ALTXAR ARIn god we trust,everything else we test. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-18 : 04:29:55
|
The new table will contain only the modified codes along with the old codes and code id if present in current table.in queries take a left outer join with this table from current table on code or id and use it in filter condition or retrieval of valuesex:select... COALESCE(n.NewCode,c.Code)......FROM YourCurrentTable cLEFT JOIN NewCodeTable nON n.Code=c.CodeWHERE .....AND COALESCE(n.NewCode,c.Code)=@Code where @Code is parameter passed from your front end applications which passes latest code value. |
 |
|
|
magikminox
Starting Member
27 Posts |
Posted - 2008-04-18 : 04:58:22
|
| My Current query is as below.I am joining 3 tables and returning the result.But one of the table (Commodity)is the one that has some of its codes changed and i want to modify this query so that i can still get correct results.How do i include the LEFT JOIN as you said above into this query. so i can include the mapping table. SELECT TR.ClientId,TR.TradeId,TR.TransactionDate,TR.BookCode,TR.ExpiryDate,TR.TransactionCode FROM Transaction TR INNER JOIN Book BO ON BO.Code = TR.BookCode INNER JOIN Commodity CO ON CO.Code = TR.CommodityCodeThanks very much.In god we trust,everything else we test. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-18 : 05:05:45
|
| [code]SELECT TR.ClientId,TR.TradeId,TR.TransactionDate,TR.BookCode,TR.ExpiryDate,TR.TransactionCodeFROM Transaction TRINNER JOIN Book BO ON BO.Code = TR.BookCodeINNER JOIN (SELECT COALESCE(n.NewCode,c.Code) AS CodeFOM Commodity cLEFT OUTER JOIN NewCommodity nON n.Code=c.Code)COON CO.Code = TR.CommodityCode[/code]i guess you will have latest codes for all commodity in your Transaction table |
 |
|
|
magikminox
Starting Member
27 Posts |
Posted - 2008-04-18 : 05:49:42
|
| Yes the Transaction table has the new codes only.The NewCommodity table is the mapping table with 2 columns only,one for the new codes and the corresponding old codes.The NewCommodity table does not include the codes that did not change.SELECT .....FROM Transaction TRINNER JOIN Book BO ON BO.Code = TR.BookCodeINNER JOIN (SELECT COALESCE(n.NewCode,c.Code) AS Code FROM Commodity c LEFT OUTER JOIN NewCommodity n ON n.Code=c.Code )CO ON CO.Code = TR.CommodityCodeThis version of the query is giving me about 28423 results when i expect only 11700 or soAm i getting something wrong?.Thanks.In god we trust,everything else we test. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-18 : 05:52:23
|
quote: Originally posted by magikminox Yes the Transaction table has the new codes only.The NewCommodity table is the mapping table with 2 columns only,one for the new codes and the corresponding old codes.The NewCommodity table does not include the codes that did not change.SELECT .....FROM Transaction TRINNER JOIN Book BO ON BO.Code = TR.BookCodeINNER JOIN (SELECT COALESCE(n.NewCode,c.Code) AS Code FROM Commodity c LEFT OUTER JOIN NewCommodity n ON n.Code=c.Code )CO ON CO.Code = TR.CommodityCodeThis version of the query is giving me about 28423 results when i expect only 11700 or soAm i getting something wrong?.Thanks.In god we trust,everything else we test.
Do you have only unique code records in Commodity or are there duplicate records existing for same commodity code? |
 |
|
|
magikminox
Starting Member
27 Posts |
Posted - 2008-04-18 : 06:14:40
|
| All records in Commodity are unique.In god we trust,everything else we test. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-18 : 07:37:06
|
| Can you check the result to see whether you are getting duplicate values? |
 |
|
|
magikminox
Starting Member
27 Posts |
Posted - 2008-04-18 : 08:12:38
|
| Yes,loads of duplicates.Some even X 15.In god we trust,everything else we test. |
 |
|
|
|