Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Consolidate 2 tables into one

Status
Not open for further replies.

MikeMV

MIS
May 15, 2006
131
US
Hello,

I have 2 tables that I need to consolidate into one, I usually do this using a do while loop, but I am wondering if there is an easier way.

All 3 tables have the following structure:

stockcode, january, february, march, etc. I have to sum each pair of months into a single row for each stockcode.

The 3 are local dbfs and are:

sales1 (source1)
sales2 (source2)
sales (destination)

I will greatly appreciate any suggestion on how to simplify this proccess.
 
If you use VFP9:
Code:
INSERT INTO Sales (field list here);
SELECT StockCode,;
       SUM(JAN) AS Jan,;
       ... 
       SUM(Dec) AS Dec;
FROM (SELECT * FROM Sales1;
      UNION ALL;
      SELECT * FROM Sales2) Tbl1;
GROUP BY StockCode

NOT TESTED!

For previous versions you should split this in 3 selects:
Code:
SELECT * FROM Sales1;
UNION ALL;
SELECT * FROM Sales2;
INTO CURSOR crsTest

SELECT StockCode,
       SUM(JAN) AS Jan,;
       ... 
       SUM(Dec) AS Dec;
FROM crsTest;
GROUP BY StockCode;
INTO CURSOR Tbl1

SELECT Sales
APPEND FROM (DBF("Tbl"))


Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Thanks for helping me optimize this process Borislav
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top