473,385 Members | 1,523 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,385 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 4554
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 skilled at perl, data manipulation and data...
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 clicks the cmdShowResults button on the main...
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 ClubsPersons that join the two first in a many to many...
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 the row when a user clicks anywhere on a cell...
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 another field. Ex. SalesManID ...
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 DataSet.ReadXML(). Now this dataset has various datatable,...
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 smallint) as the primary key. (there are in the range...
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" column is of datetime datatype) column is:...
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 SQL optimization, and am doing some explain plans...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.