473,503 Members | 9,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Union and Join in a single view

I have 3 tables I want to use in a view. Table A has field 1,2,3,4,5
and table B has field 1,2,3,4,5. I want to do a union on these. (I have
done so successfully if I stop here) I also want to join table C which
has field 1,6,7,8,9. I would like to join on field 1 and bring in the
other fields. I can join table C to A or B. I can union table A and B
but I do not know how to both union A and B then join C. Can someone
please help me? Thanks in advance.

Aug 8 '06 #1
7 15036
On 08.08.2006 16:31, ja************@gmail.com wrote:
I have 3 tables I want to use in a view. Table A has field 1,2,3,4,5
and table B has field 1,2,3,4,5. I want to do a union on these. (I have
done so successfully if I stop here) I also want to join table C which
has field 1,6,7,8,9. I would like to join on field 1 and bring in the
other fields. I can join table C to A or B. I can union table A and B
but I do not know how to both union A and B then join C. Can someone
please help me? Thanks in advance.
What stops you from joining twice? Can't you just do

select 1,2,3,4,5,6,7,8,9
from tab_a, tab_c
where tab_a.1 = tab_c.1
union all
select 1,2,3,4,5
from tab_b, tab_c
where tab_b.1 = tab_c.1

Alternatively

select 1,2,3,4,5,6,7,8,9
from (
select 1,2,3,4,5
from tab_a
union all
select 1,2,3,4,5
from tab_b
) unioned, tab_c
where unioned.1 = tab_c.1
....

Of course you can also create a view for the union and then another one
for the join - this might be more easier to manage and handle. My guess
would be that the first approach is more efficient but this depends of
course on your data.

HTH

robert
Aug 8 '06 #2

Robert Klemme wrote:
On 08.08.2006 16:31, ja************@gmail.com wrote:
I have 3 tables I want to use in a view. Table A has field 1,2,3,4,5
and table B has field 1,2,3,4,5. I want to do a union on these. (I have
done so successfully if I stop here) I also want to join table C which
has field 1,6,7,8,9. I would like to join on field 1 and bring in the
other fields. I can join table C to A or B. I can union table A and B
but I do not know how to both union A and B then join C. Can someone
please help me? Thanks in advance.

What stops you from joining twice? Can't you just do

select 1,2,3,4,5,6,7,8,9
from tab_a, tab_c
where tab_a.1 = tab_c.1
union all
select 1,2,3,4,5
from tab_b, tab_c
where tab_b.1 = tab_c.1

Alternatively

select 1,2,3,4,5,6,7,8,9
from (
select 1,2,3,4,5
from tab_a
union all
select 1,2,3,4,5
from tab_b
) unioned, tab_c
where unioned.1 = tab_c.1
...

Of course you can also create a view for the union and then another one
for the join - this might be more easier to manage and handle. My guess
would be that the first approach is more efficient but this depends of
course on your data.

HTH

robert
Thanks for your help. I have rewritten my query but I get an error. It
might be a basic error but I am a Crystal report designer who suddenly
has to create his own views. Please let me know what you think :)

ERROR:
Server: Msg 8156, Level 16, State 1, Procedure historic_sales_2, Line 2
The column 'SOPNUMBE' was specified multiple times for
'historic_sales_2'.
SQL VIEW:
CREATE VIEW [historic_sales_2] as
SELECT
SOP30200.SOPNUMBE,SOP10200.SOPNUMBE,SOP30200.DOCDA TE,SOP30200.MSTRNUMB,SOP30200.CUSTNMBR,SOP30200.CU STNAME,SOP30200.DOCAMNT,SOP30200.LOCNCODE,SOP30200 .VOIDSTTS,SOP30200.SOPTYPE,SOP10200.ITEMNMBR,SOP10 200.QUANTITY,SOP10200.UNITPRCE,SOP10200.XTNDPRCE,S OP10200.SLPRSNID,SOP10200.SALSTERR

FROM cvi.dbo.SOP30200,cvi.dbo.SOP10200
WHERE SOP30200.SOPNUMBE = SOP10200.SOPNUMBE
UNION ALL
SELECT

SOP10100.SOPNUMBE,SOP10100.DOCDATE,SOP10100.MSTRNU MB,SOP10100.CUSTNMBR,SOP10100.CUSTNAME,SOP10100.DO CAMNT,

SOP10100.LOCNCODE,SOP10100.VOIDSTTS,SOP10100.SOPTY PE
FROM cvi.dbo.SOP10100,cvi.dbo.SOP10200
WHERE SOP10100.SOPNUMBE = SOP10200.SOPNUMBE

Aug 8 '06 #3
ja************@gmail.com (ja************@gmail.com) writes:
Thanks for your help. I have rewritten my query but I get an error. It
might be a basic error but I am a Crystal report designer who suddenly
has to create his own views. Please let me know what you think :)

ERROR:
Server: Msg 8156, Level 16, State 1, Procedure historic_sales_2, Line 2
The column 'SOPNUMBE' was specified multiple times for
'historic_sales_2'.
Did you actually try to read the error message? :-) Yes, I realise that
you are not an SQL Server guy, but an error message cannot be much clearer
than this. The column names in a view must be unique, and you have two
columns called SOPNUMBE. Since you join over these column, they obviously
have the same contents, so why include them twice.

By the way, it looks to me that you have a different number of columns in
the two SELECT. That is not going to work out well.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 8 '06 #4
Yes I did read the error message and it's as clear to me as it is to
you. What is not clear to me is how to remedy it. Unfortunately, the
same data is not contained in these two columns either. I don't know if
anyone here knows anything about Microsoft Dynamics GP, which is where
these tables are coming from, but all three tables contain the same
SOPNUMBE column but the rows in each table are unique. One his a
historical table the other two are god knows what. Since on of these
tables does not have all the fields I want to select, I can't do a
simple union; which would be ideal for me. Maybe my entire approach is
wrong. Have you got any ideas?
Erland Sommarskog wrote:
ja************@gmail.com (ja************@gmail.com) writes:
Thanks for your help. I have rewritten my query but I get an error. It
might be a basic error but I am a Crystal report designer who suddenly
has to create his own views. Please let me know what you think :)

ERROR:
Server: Msg 8156, Level 16, State 1, Procedure historic_sales_2, Line 2
The column 'SOPNUMBE' was specified multiple times for
'historic_sales_2'.

Did you actually try to read the error message? :-) Yes, I realise that
you are not an SQL Server guy, but an error message cannot be much clearer
than this. The column names in a view must be unique, and you have two
columns called SOPNUMBE. Since you join over these column, they obviously
have the same contents, so why include them twice.

By the way, it looks to me that you have a different number of columns in
the two SELECT. That is not going to work out well.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 9 '06 #5
SQL lets you alias your field name, so that's the way to avoid
duplicates. If you have to pull SOPNUMBE from two different places,
just be sure to clearly define which is which, and give each one a
different alias. So if the field was in two tables called TABLE1 and
TABLE2, you could have something like this in your SELECT statement:

SELECT TABLE1.SOPNUMBE AS SOPNUMA, TABLE2.SOPNUMBE AS SOPNUMB ...

--Richard

ja************@gmail.com wrote:
Yes I did read the error message and it's as clear to me as it is to
you. What is not clear to me is how to remedy it. Unfortunately, the
same data is not contained in these two columns either. I don't know if
anyone here knows anything about Microsoft Dynamics GP, which is where
these tables are coming from, but all three tables contain the same
SOPNUMBE column but the rows in each table are unique. One his a
historical table the other two are god knows what. Since on of these
tables does not have all the fields I want to select, I can't do a
simple union; which would be ideal for me. Maybe my entire approach is
wrong. Have you got any ideas?
Erland Sommarskog wrote:
ja************@gmail.com (ja************@gmail.com) writes:
Thanks for your help. I have rewritten my query but I get an error. It
might be a basic error but I am a Crystal report designer who suddenly
has to create his own views. Please let me know what you think :)
>
ERROR:
Server: Msg 8156, Level 16, State 1, Procedure historic_sales_2, Line 2
The column 'SOPNUMBE' was specified multiple times for
'historic_sales_2'.
Did you actually try to read the error message? :-) Yes, I realise that
you are not an SQL Server guy, but an error message cannot be much clearer
than this. The column names in a view must be unique, and you have two
columns called SOPNUMBE. Since you join over these column, they obviously
have the same contents, so why include them twice.

By the way, it looks to me that you have a different number of columns in
the two SELECT. That is not going to work out well.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 9 '06 #6
On 9 Aug 2006 05:00:20 -0700, "ja************@gmail.com"
<ja************@gmail.comwrote:
>all three tables contain the same
SOPNUMBE column but the rows in each table are unique.
But the SOPNUMBE values are the same, so you should only include
one. (If they're not the same, then you can't join the tables
that way.)
>Since on of these
tables does not have all the fields I want to select, I can't do a
simple union; which would be ideal for me.
Try the following:

CREATE VIEW [historic_sales_2] as
SELECT
SOP30200.SOPNUMBE,
SOP30200.DOCDATE,
SOP30200.MSTRNUMB,
SOP30200.CUSTNMBR,
SOP30200.CUSTNAME,
SOP30200.DOCAMNT,
SOP30200.LOCNCODE,
SOP30200.VOIDSTTS,
SOP30200.SOPTYPE,
SOP10200.ITEMNMBR,
SOP10200.QUANTITY,
SOP10200.UNITPRCE,
SOP10200.XTNDPRCE,
SOP10200.SLPRSNID,
SOP10200.SALSTERR
FROM cvi.dbo.SOP30200,cvi.dbo.SOP10200
WHERE SOP30200.SOPNUMBE = SOP10200.SOPNUMBE
UNION ALL
SELECT
SOP10100.SOPNUMBE,
SOP10100.DOCDATE,
SOP10100.MSTRNUMB,
SOP10100.CUSTNMBR,
SOP10100.CUSTNAME,
SOP10100.DOCAMNT,
SOP10100.LOCNCODE,
SOP10100.VOIDSTTS,
SOP10100.SOPTYPE,
NULL AS ITEMNMBR,
NULL AS QUANTITY,
NULL AS UNITPRCE,
NULL AS XTNDPRCE,
NULL AS SLPRSNID,
NULL AS SALSTERR
FROM cvi.dbo.SOP10100,cvi.dbo.SOP10200
WHERE SOP10100.SOPNUMBE = SOP10200.SOPNUMBE
Aug 9 '06 #7
ja************@gmail.com (ja************@gmail.com) writes:
Yes I did read the error message and it's as clear to me as it is to
you. What is not clear to me is how to remedy it. Unfortunately, the
same data is not contained in these two columns either.
Since you join over them they are. Or they are not, and you don't get
any rows back at all.
I don't know if anyone here knows anything about Microsoft Dynamics GP,
which is where these tables are coming from, but all three tables
contain the same SOPNUMBE column but the rows in each table are unique.
One his a historical table the other two are god knows what. Since on of
these tables does not have all the fields I want to select, I can't do a
simple union; which would be ideal for me. Maybe my entire approach is
wrong. Have you got any ideas?
I don't know Microsoft Dynamics GP, I don't know what your specifications,
so my prospects to say whether you query will give you want you want are
utterly bleak.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2415
by: Tomas | last post by:
I would like to know if and how it is possible to join the *result* of a union with another table (without first doing individual joins to each part of the result that then will be combined into a...
9
5454
by: (Pete Cresswell) | last post by:
I've got some SQL that works as far as returning a recordset from a series of UNION statements. viz: SELECT whatever UNION this UNION that UNION other
1
3321
by: jtwright | last post by:
I've got a view that creates a parent child relationship, this view is used in Analysis Services to create a dimension in a datastore. This query tends to deadlock after about 10 days of running...
10
5586
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
5
1689
by: Arvin Portlock | last post by:
I can't come up with a query that works. Can anyone help? Conceptually the relationships are easy to describe. I have a table for books (Entries), a table for authors (Authors), and a linking...
3
2450
by: Mike Fellows | last post by:
Help, im trying to use a union join from an SQL database and a Access database - the program works fine from my own machine as i have the odbc DSN connections setup from access to link to the...
4
11639
by: janko.klemensek | last post by:
Can I Group by the Union in one query or must I write two queries for this (one for union and second for group by)?
5
3815
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
0
1130
by: ticktack | last post by:
Hi there, I am trying to do a UNION with slightly different queries, it appears to work but it now bring in another problem. The second part of the UNION need to have a an extra field value...
0
7207
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,...
0
7294
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,...
1
7015
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...
0
5602
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,...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
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 ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.