Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

UNION not giving expected result 1

Status
Not open for further replies.

noddy

Computer
Dec 25, 2002
8
I need to create a .csv file grouped by Account, Ccentre with 14 columns, OBAL, JAN, FEB, etc to CBAL.

I have existing views to get the data into the individual parts, the following is for JAN:

SELECT SUBSTRING(GL06001, 1, 4) AS Account, SUBSTRING(GL06001, 5, 4) AS Ccentre, GL06004 AS JAN
FROM dbo.GL060102
WHERE (GL06014 >= CONVERT(DATETIME, '2002-01-01 00:00:00', 102)) AND (GL06014 <= CONVERT(DATETIME, '2002-01-31 00:00:00', 102))

this will give a table of entries for JAN only.
I have tried the following to ADD the views together to give a result with OBAL, JAN as columns but only get OBAL

CREATE VIEW dbo.[24HR_GL06_2002_groupby]
AS
SELECT Account, Ccentre, OBAL
FROM dbo.[24HR_GL06_2002_OB]

union

SELECT Account, Ccentre, JAN
FROM dbo.[24HR_GL06_2002_JAN]

How can I get all 14 periods?
 
Replies continue below

Recommended for you

The UNION command appends the new data as rows, not columns. So based on the SQL you have, you should get one row for OBAL, the next for JAN, then FEB, etc.

Assuming that each unique combination of Account, Ccentre has only one record in each table, then you could do something like this:

SELECT OB.Account, OB.Ccentre, OB.OBAL, JAN, FEB, ..., DEC
FROM dbo_OB OB, dbo.JAN, dbo.FEB, ..., dbo.DEC
WHERE dbo_OB.Account = dbo.JAN.Account
AND dbo_OB.Account = dbo.FEB.Account
.
.
.
AND dbo_OB.Ccentre = dbo.DEC.Ccentre
ORDER BY OB.Account, OB.Ccentre

But this will probably be a very big, time-consuming query since it has to go across 13 tables and make 26 cross-table comparisons.

Hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor