472,325 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

SELECT a value from various tables depending on a column value

Hi,
I'm trying to write a recordset-returning function that returns a
values from a base table, and one column from a joined table, where the
joined table varies according to a field of the base table. I'm looking
for an efficieint way to do this, and I don't think I know enough about
Postgres' capabilities to know how to do this.

I imagine fetching my base table rows in order of the table reference
column, looping over my base table, and setting a refcursor to a new
joined table when the table reference column changes. I would then
fetch from the appropriate joined table cursor to get the joined value
for each row.

So my question is a performance one: is this a sensible way to do this,
or am I missing something altogether about hierarchies of tables.

Or can I fetch a bunch of rows into memory and loop over them there,
thus avoid queries to look up individual rows over and over.

Any ideas would be much appreciated.

Many thanks,

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

Nov 23 '05 #1
1 4515
What solution to use depends how many other tables and the relative
sizes of tables, but the following option has a reasonably good chance:

Suppose you have basetable, and joined1, and joined2.
Basetable.tablename tells which of the secondary tables to join against
(contains either 'joined1' or 'joined2').

Then join ALL the tables together and use a CASE statement to pick the
column you want.

SELECT
basetable.*,
case when basetable.tablename = 'joined1' then joined1.salary else
joined2.bingo_money end
FROM basetable
LEFT JOIN joined1 USING (basetableid)
LEFT JOIN joined2 USING (basetableid)
WHERE ....

Or something like this. This will avoid writing any set-returning
functions or any user code.

Paul
Hi,
I'm trying to write a recordset-returning function that returns a
values from a base table, and one column from a joined table, where
the joined table varies according to a field of the base table. I'm
looking for an efficieint way to do this, and I don't think I know
enough about Postgres' capabilities to know how to do this.

I imagine fetching my base table rows in order of the table reference
column, looping over my base table, and setting a refcursor to a new
joined table when the table reference column changes. I would then
fetch from the appropriate joined table cursor to get the joined value
for each row.

So my question is a performance one: is this a sensible way to do
this, or am I missing something altogether about hierarchies of tables.
Or can I fetch a bunch of rows into memory and loop over them there,
thus avoid queries to look up individual rows over and over.

Any ideas would be much appreciated.

Many thanks,

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

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #2

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

Similar topics

5
by: Reestit Mutton | last post by:
Hi, I'm currently learning the ropes with CSS, PHP, MYSQL, Javascript etc... by redesigning my website as a database driven site. Okay, so I'm...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and...
14
by: Jos? | last post by:
This one droves me completely mad. I did not succeed to exploit the track given to me by Bob. I have : three tables : Clubs, Persons and...
2
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to...
4
by: Aryan | last post by:
Hi, I am having problem with DataTable.Select() method. I am using ASP.NET 2.0. I have DataSet which reads data from XML file using...
33
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a...
3
by: Jeff | last post by:
hey ..NET 2.0 I have a datatable which consist of 3 columns (int, date, value). This DataTable have 3 rows, the values of the "date" ("date"...
2
by: BD | last post by:
Hi, all. My background is more Oracle than db2. My skills at SQL tuning are quite limited. I'm running 8.2 on Windows. I'm tasked with some...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.