473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Output in a Single Table

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:

TBLMASTER
AsOfDate Country Factor1 Factor2 Factor3 Factor4
------------------------------------------------------------------------------------------------------------
10/29/2007 Brazil 5 4 2 3
10/30/2007 Brazil 2 2 6 2
10/31/2007 Brazil 6 8 9 0
10/29/2007 Canada 8 9 8 8
10/30/2007 Canada 1 5 3 4
10/31/2007 Canada 3 1 5 7
10/29/2007 USA 9 3 4 6
10/30/2007 USA 3 6 0 2
10/31/2007 USA 7 3 1 1


Each 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.Fac torInt,d.FactorInt
from tbl1 a
inner join tbl2 b
on a.AsOfDate = b.AsOfDate
and a.Country = b.Country
inner join tbl3 c
on b.AsOfDate = c.AsOfDate
and b.Country = c.Country
inner join tbl4 d
on c.AsOfDate = d.AsOfDate
and c.Country = d.Country



================================================== ================================================

TBL1
AsOfDate Country FactorInt
--------------------------------------------------------------
10/31/2007 Canada 3
10/30/2007 Brazil 2
10/29/2007 USA 9
10/30/2007 Canada 1
10/29/2007 Brazil 5
10/31/2007 USA 7
10/29/2007 Canada 8
10/31/2007 Brazil 6
10/30/2007 USA 3

TBL2
AsOfDate Country FactorInt
--------------------------------------------------------------
10/31/2007 Canada 1
10/30/2007 Brazil 2
10/29/2007 USA 3
10/30/2007 Canada 5
10/29/2007 Brazil 4
10/31/2007 USA 3
10/29/2007 Canada 9
10/31/2007 Brazil 8
10/30/2007 USA 6

TBL3
AsOfDate Country FactorInt
--------------------------------------------------------------
10/31/2007 Canada 5
10/30/2007 Brazil 6
10/29/2007 USA 4
10/30/2007 Canada 3
10/29/2007 Brazil 2
10/31/2007 USA 1
10/29/2007 Canada 8
10/31/2007 Brazil 9
10/30/2007 USA 0

TBL4
AsOfDate Country FactorInt
--------------------------------------------------------------
10/31/2007 Canada 7
10/30/2007 Brazil 2
10/29/2007 USA 6
10/30/2007 Canada 4
10/29/2007 Brazil 3
10/31/2007 USA 1
10/29/2007 Canada 8
10/31/2007 Brazil 0
10/30/2007 USA 2
Nov 5 '07 #1
1 1203
iburyak
1,017 Expert 512MB
Try something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT AsOfDate, Country, max(Factor1) Factor1, max(Factor2) Factor2, max( Factor3) Factor3, max( Factor4) Factor4
  2. FROM(
  3. Select AsOfDate, Country, FactorInt as Factor1, 0 as Factor2, 0 as Factor3, 0 as Factor4
  4. From TBL1
  5. Union All
  6. Select AsOfDate, Country, 0 as Factor1, FactorInt as Factor2, 0 as Factor3, 0 as Factor4
  7. From TBL2
  8. Union All
  9. Select AsOfDate, Country, 0 as Factor1, 0 as Factor2, FactorInt as Factor3, 0 as Factor4
  10. From TBL3
  11. Union All
  12. Select AsOfDate, Country, 0 as Factor1, 0 as Factor2, 0 as Factor3, FactorInt as Factor4
  13. From TBL4) a
  14. GROUP BY AsOfDate, Country
It shouldn't be treated like final code. I didn't test it but this is a main idea on how I think it should be done.
Good Luck.
Nov 5 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Craig Hoskin | last post by:
Hi all Ive got a problem I was hoping someone may be able to help with. Im calling an extended stored procedure provided by a third party (master..xp_OrderHeader). This xp requires 3 inputs...
1
by: Asteras | last post by:
Hi All!!! Let us consider two tables in a MySQL database: 1) C_T is a table of school classes and the teachers that teach in them: select * from C_T; +-------+---------+
1
by: R. P. | last post by:
Anybody knows a way to produce output from a SELECT statement on a single line, instead of each hit on a separate line? Here is what I mean: let's say I have a table listing families, one per...
11
by: Lyle Fairfield | last post by:
The stored procedure script below is an example of how looping, case statements and output parameters can be used in MS-SQL stored procedures to accomplish things for which we may have had to use...
4
by: Jerry LeVan | last post by:
Hi, I am adding image and large object support in my Cocoa postgresql browser. Are there going to be any enhanced bytea support functions coming along? It seems sorta silly to have to write...
6
by: Ben | last post by:
Hi We have a Dataset that has been populated from the output parameter of a Stored Procedure (@Output). I understand that I can extract a single item when the dataset is populated by a table...
3
by: pr33tz | last post by:
Hi, I've been using SQL Server 2000 for a short while now and want to know if the following is possible. I've got two tables, Customer table and an Address table. I want to create a trigger that,...
9
by: google | last post by:
This may be covered already, but I've yet to hit upon the search terms to find it... I'm running PHP v4.4.0 on a Windows 2000 Server (SP4) running IIS v5. A simple script I'm using is...
0
by: LanaR | last post by:
Hello, one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.