473,811 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Output in a Single Table

8 New Member
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,Count ry,Factor1,Fact or2,Factor3,Fac tor4)
select a.AsOfDate,a.Co untry,a.FactorI nt,b.FactorInt, c.FactorInt,d.F actorInt
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 1218
iburyak
1,017 Recognized Expert Top Contributor
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
2377
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 and is supposed to provie one output. When I call it in SQL Query Analyser all runs OK and I get a column result (single result) titled "Output_Info" with a value of say 300051
1
1157
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
8293
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 row that includes the name of father, mother and the number of children. Then there would be another table with one row for each child and the rows would be linked to the first table with a foreign key scheme. Now I would like to list each family...
11
2630
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 VBA code, or automation, in the JET world. Some who have not yet worked with MS-SQL may be interested. The script is indented in reality but I have aligned it all left to make reading easier, (I hope). The Sproc takes an Account ID, an...
4
11442
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 customized C code to import a file into a bytea field.
6
2106
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 using this code: CType(objDataSet.Tables("MyTable").Rows(0).Item("MyField"), String)
3
17942
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, when a new customer is added or amended, will output the changes to a text file. For example, if Mr Smith's address is created, it will pick up his name from the Customer table and his address from the Address table (linked by Cutomer_ID) and output...
9
2048
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 generating ODBC errors (not finding my tables) when I run the script through a web browser connected to the IIS server. If I then run the unchanged script locally, from a Windows console window, the PHP script connects to the database and I can dump the
0
2782
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. The statement itself is not that complicated, it is 3 selects and union all. Explain output is pretty big, but I could not find anything unusual. I'm new to db2 and I could be missing stuff. I am posting the explain output below and I really...
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10397
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10410
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9214
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.