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 |
|
sqldba20
Posting Yak Master
183 Posts |
Posted - 2007-11-05 : 12:25:57
|
| Folks:I need help with a script. I have 4 tables with AsOfDate, Country and FactorInt columns. In each table AsOfdate and Country are the same for that date but the FACTORINT column varies. I want the output in a single table (master table) and it should be in this format and look like this:TBLMASTERAsOfDate Country Factor1 Factor2 Factor3 Factor4------------------------------------------------------------------------------------------2007-10-29 Brazil 5 4 2 32007-10-30 Brazil 2 2 6 22007-10-31 Brazil 6 8 9 02007-10-29 Canada 8 9 8 82007-10-30 Canada 1 5 3 42007-10-31 Canada 3 1 5 72007-10-29 USA 9 3 4 62007-10-30 USA 3 6 0 22007-10-31 USA 7 3 1 1Each child table has data for 3 days for each country with different FactorInt, with total of 9 rows in each table. By clubbing it in a single table I should have only 9 rows. I ran the below script to get it into single table but for some reason it doesn't work and happen to give around 23 rows and some weird data.insert into TBLMASTER (AsOfDate,Country,Factor1,Factor2,Factor3,Factor4)select a.AsOfDate,a.Country,a.FactorInt,b.FactorInt,c.FactorInt,d.FactorIntfrom tbl1 ainner join tbl2 bon a.AsOfDate = b.AsOfDateand a.Country = b.Countryinner join tbl3 con b.AsOfDate = c.AsOfDateand b.Country = c.Countryinner join tbl4 don c.AsOfDate = d.AsOfDateand c.Country = d.Country==================================================================================================TBL1AsOfDate Country FactorInt-------------------------------------------10/31/2007 Canada 310/30/2007 Brazil 210/29/2007 USA 910/30/2007 Canada 110/29/2007 Brazil 510/31/2007 USA 710/29/2007 Canada 810/31/2007 Brazil 610/30/2007 USA 3TBL2AsOfDate Country FactorInt-------------------------------------------10/31/2007 Canada 110/30/2007 Brazil 210/29/2007 USA 310/30/2007 Canada 510/29/2007 Brazil 410/31/2007 USA 310/29/2007 Canada 910/31/2007 Brazil 810/30/2007 USA 6TBL3AsOfDate Country FactorInt-------------------------------------------10/31/2007 Canada 510/30/2007 Brazil 610/29/2007 USA 410/30/2007 Canada 310/29/2007 Brazil 210/31/2007 USA 110/29/2007 Canada 810/31/2007 Brazil 910/30/2007 USA 0TBL4AsOfDate Country FactorInt-------------------------------------------10/31/2007 Canada 710/30/2007 Brazil 210/29/2007 USA 610/30/2007 Canada 410/29/2007 Brazil 310/31/2007 USA 110/29/2007 Canada 810/31/2007 Brazil 010/30/2007 USA 2All these output should get stored in a single table (tblmaster) with the following format and the output should like this==================================================================================================================TBLMASTERAsOfDate Country Factor1 Factor2 Factor3 Factor4------------------------------------------------------------------------------------------2007-10-29 Brazil 5 4 2 32007-10-30 Brazil 2 2 6 22007-10-31 Brazil 6 8 9 02007-10-29 Canada 8 9 8 82007-10-30 Canada 1 5 3 42007-10-31 Canada 3 1 5 72007-10-29 USA 9 3 4 62007-10-30 USA 3 6 0 22007-10-31 USA 7 3 1 1 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-11-05 : 12:30:57
|
| perhaps you should normalize the data first. That would make it MUCH easier[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
sqldba20
Posting Yak Master
183 Posts |
Posted - 2007-11-05 : 12:32:14
|
| Well, I have the data in that format now. Is there a way I could do it with a script or stored procedure? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-05 : 13:13:48
|
You get duplicate rows because there is same dateOf in several tables.Try DISTINCT. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|