If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > select records from one table & remaining from the other

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-05-03, 21:50
mbk mbk is offline
Registered User
 
Join Date: Sep 2003
Posts: 1
select records from one table & remaining from the other

Hi,

These are my tables :

Table-1
---------

user_id Dept Function Authority
------- ---- -------- ---------
91623 MKT Func_1 Y
91623 MKT Func_2 N
91623 MKT Func_3 Y
11122 ACC FUNC_1 N
.
.


Table-2
---------

Dept Function Authority
---- -------- ---------
MKT FUNC_1 Y
MKT FUNC_2 Y
MKT FUNC_3 Y
MKT FUNC_4 Y
MKT FUNC_5 N
.
.

MKT FUNC_17 Y
MKT FUNC_18 Y
.
.
.
ACC FUNC_1 Y
ACC FUNC_2 N




Table-2 holds all the functions and their default authority code. Table_1 holds the actual functions & authority given to an employee.

I am trying to write an SQL which will select for a user from Table_1 the user_id, Function and authority for whatever functions are in the table and for the missing functions, take them from Table_2. i.e. the result should look like :


user_id Function Authority
------- -------- ---------
-- Taken from Table_1
91623 Func_1 Y
91623 Func_2 N
91623 Func_3 Y
-- Taken from Table_2 (user_id from Table_1)
91623 FUNC_4 Y
91623 FUNC_5 N
.
.

91623 FUNC_17 Y
91623 FUNC_18 Y


Table_1 & Table_2 are linked by the department.

Could anyone help me with this query ?

I tried the following, but it does not work right :

select
a.user_id,
b.function,
min(isnull(a.authority,b.authority))
from
Table_1 a,
Table_2 b
where
a.user_id = 91623 and
a.dept = b.dept
group by
a.user_id,
b.function



Thanks !
Reply With Quote
  #2 (permalink)  
Old 09-08-03, 05:44
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: select records from one table & remaining from the other

Quote:
Originally posted by mbk
Hi,

These are my tables :

Table-1
---------

user_id Dept Function Authority
------- ---- -------- ---------
91623 MKT Func_1 Y
91623 MKT Func_2 N
91623 MKT Func_3 Y
11122 ACC FUNC_1 N
.
.


Table-2
---------

Dept Function Authority
---- -------- ---------
MKT FUNC_1 Y
MKT FUNC_2 Y
MKT FUNC_3 Y
MKT FUNC_4 Y
MKT FUNC_5 N
.
.

MKT FUNC_17 Y
MKT FUNC_18 Y
.
.
.
ACC FUNC_1 Y
ACC FUNC_2 N




Table-2 holds all the functions and their default authority code. Table_1 holds the actual functions & authority given to an employee.

I am trying to write an SQL which will select for a user from Table_1 the user_id, Function and authority for whatever functions are in the table and for the missing functions, take them from Table_2. i.e. the result should look like :


user_id Function Authority
------- -------- ---------
-- Taken from Table_1
91623 Func_1 Y
91623 Func_2 N
91623 Func_3 Y
-- Taken from Table_2 (user_id from Table_1)
91623 FUNC_4 Y
91623 FUNC_5 N
.
.

91623 FUNC_17 Y
91623 FUNC_18 Y


Table_1 & Table_2 are linked by the department.

Could anyone help me with this query ?

I tried the following, but it does not work right :

select
a.user_id,
b.function,
min(isnull(a.authority,b.authority))
from
Table_1 a,
Table_2 b
where
a.user_id = 91623 and
a.dept = b.dept
group by
a.user_id,
b.function



Thanks !
You need to use UNION:

SELECT ... FROM table1
UNION
SELECT ... FROM table2
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On