473,473 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Union question

Hi,

This isn't really an access question, but as I'm really new to SQL I
don't know where I would post this. Any suggestions on that would
also be helpful. Now on to the question...

I am trying to extract data from a Timberline database through an OBDC
driver to MS Excel. I'm using Microsoft Query and a crude knowledge
of SQL to build my query.

What I'm having trouble doing is getting a list of jobs (or rather,
job numbers) and the appropriate contact for those jobs.

I'd like to get a list of jobs and the primary contact for that job.
That is, if a job has a contact of type A, I want the result set to
show that contact next to the job, if not, I want a contact of type B,
if finally not that, I want a contact of type C.

I was hoping that a union statement could help me with that.
Something along the lines of:

SELECT JOB_LIST.Job, CONTACT_LIST.Name
FROM JOB_LIST, CONTACT_LIST
WHERE (JOB_LIST.Contact_ID = CONTACT_LIST.Contact_ID)
AND (CONTACT_LIST.Type = 'A')
UNION
SELECT JOB_LIST.Job, CONTACT_LIST.Name
FROM JOB_LIST, CONTACT_LIST
WHERE (JOB_LIST.Contact_ID = CONTACT_LIST.Contact_ID)
AND (CONTACT_LIST.Type = 'B')

However, this gets a "non unique table reference" error. The UNION
statements I've seen generally form a union between select statements
from two different databases, not between select statements from the
same database. However, maybe the SQL code here looks good, and the
problem lies within Microsoft query and I should ask this somewhere
else; I really don't know.

Is there a way to do what I want? Does it have nothing to with UNION
statements?

Any help would be greatly appreciated,
Chip

[Please reply to message board, not e-mail]
Nov 13 '05 #1
3 3396
First, you need to confirm that you having a valid ODBC connection. I
would create an ODBC link in Access. Matter of fact, I would pull the
data into Excel from the Access link.

Open up an Access mdb, go to the Tables tab and right-click anywhere in
the db window (in the Tables tab) that is clear (anywhere on the white
background). On the context menu click on "Link Tables". In the dialog
box that appears, go to the lower left corner of the Dialog window to
the
"Files of Type:" dropdown box. Scroll down to the bottom of that list
and select "ODBC Databases". Then, in the "Select Data Source" dialog
window that appears, go to the "Machine Data Source" Tab. Select the
DSN to your Timberline Database. Note: if you don't see the DSN or
know which DSN it is, then that is your problem. But if you do know the
DSN, then select it and click OK. If this DSN has a password, make sure
to click on the "remember password" checkbox in the Dialog window (lower
right corner) and then click OK. Select the index column in the next
window and click OK (it doesn't really matter which column you select if
you don't know which one is the index column, usually a column on the
left side).

Now you have a definite connection to your external data table. Open
the table to make sure you can see data. If you don't need to automate
the process you can create a query in Access and query for the fields
you need. Highlight the rows from the query and copy and paste them
into Excel.

As for the queries, instead of a Union query, just use the "OR"
operator.

SELECT JOB_LIST.Job, CONTACT_LIST.Name
FROM JOB_LIST, CONTACT_LIST
WHERE (JOB_LIST.Contact_ID = CONTACT_LIST.Contact_ID)
AND (CONTACT_LIST.Type = "A" Or CONTACT_LIST.Type = "B")

In the Access query window, you don't write any Sql statements. Just
select fields and enter criteria in the criteria fields.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #2
Rich,

Thank you for your help.

However, I did as you suggested and imported the data from Timberline
into Access. I was fairly sure that I had a valid OBDC connection,
and I did.

But I still don't have the query correct. Sorry that I didn't explain
myself better, but I did not want the fields if either the type of the
contact was 'A' or 'B'. I wanted the fields corresponding to 'A' and
only 'A', even if a 'B' existed, and if 'A' did not exist, then I
wanted 'B' and only 'B'. An example might show this better.

Given the data:

Job Name Type
3000 Chris A
3001 Doug B
3002 Esteban C
3003 Frank A
3003 Gerald B
3004 Hank B
3004 Igor C
Your query would result in:

If (A or B)...

3000 Chris A
3001 Doug B
3003 Frank A
3003 Gerald B
3004 Hank B

However, I only want:

If A... Else If B...

3000 Chris A
3001 Doug B
3003 Frank A
3004 Hank B

That is, despite having two people for job 3003, I only want type A,
despite there also being a type B. A UNION statement seemed like it
would do the job, because it would do the query first for the 'A's,
then for the 'B's, and I could use a distinct statement or something
to eliminate the 'B's that already had 'A's. However, I still
receieve the "non distinct table reference" error.

Anyway, thanks for your help.

-Chip
Nov 13 '05 #3
OK. try this in the Access Query builder - just copy and paste it
straight in the sql window - Note: to get to the sql window go to the
Query design view, go to the View menu, 2nd menu from the top <b>SQL</b>
Sql view. This puts you in the Sql window. Just overwrite whatever
text is in that window with the text below. To get back to design view
go back to the View menu and click on the first selection Design View.

SELECT * FROM Table1 WHERE Table1.type = (SELECT MIN(type) FROM Table1
T2 " WHERE T2.Job = Table1.Job) AND Table1.type in ("a","b")

Or

SELECT job, name, type FROM Table1 WHERE table1.type = (SELECT MIN(type)
FROM Table1 T2 WHERE T2.Job = Table1.Job) AND Table1.type in ("a","b")
Here I'm using a subquery and a few agregate functions that come with
sql. The first query will list all the columns in your table that meet
your desired criteria. But inside the query builder you will only see
one column for Type with the subquery listed in the criteria section of
that column. In the 2nd query you get 3 columns with the subquery
listed in the criteria section of the Type column.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4

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

Similar topics

4
by: James DeClerk | last post by:
Hi everyone. I'm new to C++ and I've got a seemingly tough problem to tackle. I have a union. This union needs to be converted into hexadecimal format. The hexadecimal number is then...
2
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
2
by: marco | last post by:
Dear List, as it seems, MS SQL as used in Access does not allow a select INTO within a UNION query. Also, it seems that a UNION query can not be used as a subquery. Maybe my (simplified)...
16
by: tedu | last post by:
does anyone know of a platform/compiler which will place union elements to not overlap? as in union u { int a; long b; size_t c; }; in my limited experience, writing to any of (a, b, or c)...
8
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to...
10
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
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...
9
by: dspfun | last post by:
Hi! I have stumbled on the following C question and I am wondering wether the answer is implementation specific or if the answer is always valid, especially question b). BRs! ...
0
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
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,...
0
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...
0
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...
0
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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 ...

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.