473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Question Join/Subselect

Hi,

I have some problems with creating a query that will replace values in
one table from another one.

Table 1:
userName : refCode1 : refCode2
------------------------------
alex : 12 : 24

Table 2:
refCode : ActualCode
---------------------
12 AAAAAA
24 BBBBBB
Result Desired
userName : refCode1 : refCode2
------------------------------
alex : AAAAAA : BBBBBB
I need to crete a view that returns me Table1 but replaces refCode1,
refCode2 with the ActualCode.

I did not have any success with Joins or Subselects so far.

Thanks for any advise

Alex

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 11 '05 #1
7 4543
Andrew,
thanks for the help. The query actually works. However if I try to
create a view then the sever complains

Create Tabe: attribute "actualcode" duplicated;

Any Ideas on how to get around that ?

Alex

Andrew L. Gould wrote:
On Wednesday 27 August 2003 10:28 pm, Alex wrote:

Hi,

I have some problems with creating a query that will replace values in
one table from another one.

Table 1:
userName : refCode1 : refCode2
------------------------------
alex : 12 : 24

Table 2:
refCode : ActualCode
---------------------
12 AAAAAA
24 BBBBBB
Result Desired
userName : refCode1 : refCode2
------------------------------
alex : AAAAAA : BBBBBB
I need to crete a view that returns me Table1 but replaces refCode1,
refCode2 with the ActualCode.

I did not have any success with Joins or Subselects so far.

Thanks for any advise

Alex


I'm not sure how to handle the space in the table names. If there weren't any
spaces in table names, the following should work:

select Table1.userName, Table2.ActualCode, Table3.ActualCode
from Table1, Table2, Table2 as Table3
where Table1.refCode1 = Table2.refCode and Table1.refCode2 = Table3.refCode;

I hope this helps,

Andrew Gould
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 11 '05 #2
On Thursday 28 August 2003 02:20 am, Alex wrote:
Andrew L. Gould wrote:
On Wednesday 27 August 2003 10:28 pm, Alex wrote:
Hi,

I have some problems with creating a query that will replace values in
one table from another one.

Table 1:
userName : refCode1 : refCode2
------------------------------
alex : 12 : 24

Table 2:
refCode : ActualCode
---------------------
12 AAAAAA
24 BBBBBB
Result Desired
userName : refCode1 : refCode2
------------------------------
alex : AAAAAA : BBBBBB
I need to crete a view that returns me Table1 but replaces refCode1,
refCode2 with the ActualCode.

I did not have any success with Joins or Subselects so far.

Thanks for any advise

Alex


I'm not sure how to handle the space in the table names. If there weren't
any spaces in table names, the following should work:

select Table1.userName, Table2.ActualCode, Table3.ActualCode
from Table1, Table2, Table2 as Table3
where Table1.refCode1 = Table2.refCode and Table1.refCode2 =
Table3.refCode;

I hope this helps,

Andrew Gould


Andrew,
thanks for the help. The query actually works. However if I try to
create a view then the sever complains

Create Tabe: attribute "actualcode" duplicated;

Any Ideas on how to get around that ?

Alex


Alex,

Have you tried renaming the column being duplicated?

select Table1.userName, Table2.ActualCode as ActualCode1, Table3.ActualCode as
ActualCode2
from Table1, Table2, Table2 as Table3
where Table1.refCode1 = Table2.refCode and Table1.refCode2 =
Table3.refCode;

Best of luck,

Andrew Gould

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 11 '05 #3
On Thursday 28 August 2003 08:20, Alex wrote:
Andrew,
thanks for the help. The query actually works. However if I try to
create a view then the sever complains

Create Tabe: attribute "actualcode" duplicated;
Alias the output names:
.... Table2.ActualCode as actual1, Table3.ActualCode as actual2 ...
Any Ideas on how to get around that ?

Alex

Andrew L. Gould wrote:
On Wednesday 27 August 2003 10:28 pm, Alex wrote:

I'm not sure how to handle the space in the table names. If there weren't
any spaces in table names, the following should work:

select Table1.userName, Table2.ActualCode, Table3.ActualCode
from Table1, Table2, Table2 as Table3
where Table1.refCode1 = Table2.refCode and Table1.refCode2 =
Table3.refCode;


--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 11 '05 #4
Thanks for the help.
Alex

Richard Huxton wrote:
On Thursday 28 August 2003 08:20, Alex wrote:

Andrew,
thanks for the help. The query actually works. However if I try to
create a view then the sever complains

Create Tabe: attribute "actualcode" duplicated;


Alias the output names:
... Table2.ActualCode as actual1, Table3.ActualCode as actual2 ...
Any Ideas on how to get around that ?

Alex

Andrew L. Gould wrote:

On Wednesday 27 August 2003 10:28 pm, Alex wrote:

I'm not sure how to handle the space in the table names. If there weren't
any spaces in table names, the following should work:

select Table1.userName, Table2.ActualCode, Table3.ActualCode
from Table1, Table2, Table2 as Table3


where Table1.refCode1 = Table2.refCode and Table1.refCode2 =
Table3.refCode;



---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #5
Hi,

I have a query where I want to filter out records from table_a if a
field in table_a matches in table table_b. Basically table_b defines the
filter.

If table_b however is empty i dont get any results

SELECT A.value_one FROM table_a AS A, table_b AS B WHERE A.value_two <>
B.value_two;
or
SELECT A.value_one FROM table_a AS A, table_b AS B WHERE A.value_two <>
B.value_two AND B.value_two NOTNULL;

Only work if the there is a value in table_b.
Could anyone tell me if there is a way to do that ?
Thanks a lot
Alex

PS: I'd like to thank here persons who reply rather than sending the
message per mail.


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #6

On Mon, 1 Sep 2003, Alex wrote:
Hi,

I have a query where I want to filter out records from table_a if a
field in table_a matches in table table_b. Basically table_b defines the
filter.


Well something like one of the following should work depending
on how you want to treat nulls and such (and performance varies in
postgresql by version for each of the options):

SELECT a.value_one FROM table_a AS A where NOT EXISTS
(select 1 from table_b AS B WHERE A.value_two=B.value_two);
SELECT a.value_one FROM table_a AS A where A.value_two NOT IN
(select value_two from table_b);
SELECT a.value_one FROM table_a AS A LEFT OUTER JOIN
table_b AS B ON (a.value_two=B.value_two) WHERE B.value_two IS NULL;
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #7
Ron,
the idea is to provide a table where users can define filters. But it
this table may be as well empty.

Alex

Ron Johnson wrote:
On Sun, 2003-08-31 at 11:56, Alex wrote:

Hi,

I have a query where I want to filter out records from table_a if a
field in table_a matches in table table_b. Basically table_b defines the
filter.

If table_b however is empty i dont get any results

SELECT A.value_one FROM table_a AS A, table_b AS B WHERE A.value_two <>
B.value_two;
or
SELECT A.value_one FROM table_a AS A, table_b AS B WHERE A.value_two <>
B.value_two AND B.value_two NOTNULL;

Only work if the there is a value in table_b.
Could anyone tell me if there is a way to do that ?


If the filter is empty, how can you filter anything?
PS: I'd like to thank here persons who reply rather than sending the
message per mail.


What does that mean?


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 11 '05 #8

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

Similar topics

0
by: John Larsen | last post by:
Does anybody know if there is a way to aggregate a table then join it to another table? I assume this would be possible with a subquery as in SELECT * FROM t1 WHERE t1.a=(SELECT sum(t2.b) FROM t2...
6
by: Dave | last post by:
To build a grid, all the distinct rows from T1 are required, and only those from T2 which fall btn 2003-09-11 and 2003-09-18. In the following example: A is included because it is within the date...
1
by: Magnus | last post by:
Hi, I have previously worked mostly with Sql Server and wonder how to write a subselect query in mysql 4.0. What i would like to do is something like: select a.name, b.isbn from author a...
2
by: Karsten Hilbert | last post by:
Dear all, for some reason I just cannot get my brain wrapped around the required syntax for the following. I think I need to either use a join or subselect(s): Situation: ---------- I have...
3
by: Alex | last post by:
Hi, I need to form a query where i can add some columns based on the result. Table A ColA, ColB ---------- 1 A 2 B
5
by: Todd | last post by:
Data related to the query I'm working on is structured such that TableA and TableB are 1-many(optional). If an item on TableA has children on TableB, I need to use the Max(tstamp) from Table B in...
2
by: Morten K. Poulsen | last post by:
(re-post) Dear list, Please let me know if this is not the list to ask this kind of question. I am trying to optimize a query that joins two relatively large (750000 rows in each) tables....
6
by: Bob Stearns | last post by:
How do I (syntax please) write a query which returns the invoice header data and the amount due which is the SUM(qty*each) from the details rows with the same invoice number for all invoices in the...
4
by: johnfaulkner | last post by:
Hi, I am trying to perform a single select of data from 2 tables, table A and table B. Table B may have none, one or many corresponding rows. If table B has no corresponding rows then table B...
2
by: frederikengelen | last post by:
Hello all, We are seeing strange behaviour for queries on a table we need to convert data from. We try to find out whether table A(B_CONV_ID) contains data that does not exists in table...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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
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,...
0
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...

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.