473,791 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ 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.Na me
FROM JOB_LIST, CONTACT_LIST
WHERE (JOB_LIST.Conta ct_ID = CONTACT_LIST.Co ntact_ID)
AND (CONTACT_LIST.T ype = 'A')
UNION
SELECT JOB_LIST.Job, CONTACT_LIST.Na me
FROM JOB_LIST, CONTACT_LIST
WHERE (JOB_LIST.Conta ct_ID = CONTACT_LIST.Co ntact_ID)
AND (CONTACT_LIST.T ype = '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 3424
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.Na me
FROM JOB_LIST, CONTACT_LIST
WHERE (JOB_LIST.Conta ct_ID = CONTACT_LIST.Co ntact_ID)
AND (CONTACT_LIST.T ype = "A" Or CONTACT_LIST.Ty pe = "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
2701
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 inserted into a Binary (255) column in a SQL Server DB. The program will be complied on the unix platform using gcc. union account
2
4359
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 undefined behavior because my reference to the union is via a
73
4071
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
2
9780
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) problem can avoid these technicalities: the original table has columns A1, A2, B1, B2, C1, C2.
16
2110
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) will affect the value read from any other. i understand this is UB, but i'm
8
5664
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 reinterpret_cast for the code
10
459
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 destructor ,or other member functions. I can understand using constructors and memeber functions,but what is destructor used for? I have appealled to the forums in chinese ,but no enough usefull feedback.
30
3284
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
3847
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 table, we try separate this big table into twelve tables and create a view
9
5706
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! ---------------------------------------------------- Following C code runs on a "little endian" machine that has a 32-bit
0
9669
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
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
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
9030
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
7537
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
5435
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
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
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.