473,322 Members | 1,566 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,322 software developers and data experts.

Recursive Query Quesiton - Table Function

I have a database which stores information about organisms collected
during sediment toxicology research. For each sample, organisms in
sediment are collected and identified taxonomically (Order, Family,
Genus, Species).

Taxonomy lookup information in the database is stored in a recursive
table in the form:

TSN (taxa serial number)
Rank (Order, Family, Genus, Species)
Name
Parent_TSN (related Taxa at higher taxonomic level)

When the number of a particlar organism collected is entered into the
database, the count is stored along with the lowest level TSN the
organisms were identified to.

Okay - now the problem. Depending on the type of analysis being done,
a user may want organism counts at the lowest level, or rolled up to a
higher taxonomic level (usually Family). Can I write a recursive
function which will cycle through the Taxonomy database, and provide
the name of the organism at the appropriate taxonomic level? Is this a
reasonable approach with regard to speed and efficiency?

Something Like:
SELECT sample_id, 'Get Name Function(Rank, TSN)', Sum([count]) AS
NoTaxa FROM dbo.tblbenthic

Results could then be grouped and summed on the Name, to summarise
data for each sample/taxa.

Is this a reasonable approach? Or is there a better one? Did I explain
the problem well enough?

Thanks in advance,

Tim
Jul 20 '05 #1
6 6307
I do not kow what a TSN (taxa serial number) looks like, but are there
organism that do not have (Order, Family, Genus, Species)? It would
seem be a better design to make them into columns then use NULLs for
the missing classification data.

CREATE TABLE LabNotes
(..
order INTEGER,
family INTEGER,
genus INTEGER,
species INTEGER,
CHECK (...),
org_counts INTEGER NOT NULL CHECK (org_c0tns >= 0),
..);

Okay - now the problem. Depending on the type of analysis being

done,
a user may want organism counts at the lowest level, or rolled up to a
higher taxonomic level (usually Family). <<

Now that is easy to do

SELECT .. org_count
FROM LabNotes
WHERE <level> IS NOT NULL;

Did I miss something?
Jul 20 '05 #2
Sorry, I forgot to post the constraint and test data:

CREATE TABLE LabNotes
(ord INTEGER,
family INTEGER,
genus INTEGER,
species INTEGER,
CHECK (CASE WHEN (ord + family + genus + species) IS NOT NULL THEN 1
WHEN COALESCE (ord, family, genus, species) IS NULL THEN 1
WHEN COALESCE(species, family, genus) IS NULL THEN 1
WHEN COALESCE(species, genus) IS NULL THEN 1
WHEN species IS NULL THEN 1
ELSE 0 END = 1)
);
SELECT * FROM LabNotes;

--good data
INSERT INTO Labnotes VALUES (1, 2, 3, 4);
INSERT INTO Labnotes VALUES (1, 2, 3, NULL);
INSERT INTO Labnotes VALUES (1, 2, NULL, NULL);
INSERT INTO Labnotes VALUES (1, NULL, NULL, NULL);
INSERT INTO Labnotes VALUES (NULL, NULL, NULL, NULL);
-- bad data
INSERT INTO Labnotes VALUES (1, 2, NULL, 4);
INSERT INTO Labnotes VALUES (1, NULL, 3, 4);
INSERT INTO Labnotes VALUES (1, 2, NULL, 3);
INSERT INTO Labnotes VALUES (NULL, 2, NULL, 4);
INSERT INTO Labnotes VALUES (NULL, NULL, NULL, 4);
Jul 20 '05 #3
Tim Pascoe (ti********@cciw.ca) writes:
Okay - now the problem. Depending on the type of analysis being done,
a user may want organism counts at the lowest level, or rolled up to a
higher taxonomic level (usually Family). Can I write a recursive
function which will cycle through the Taxonomy database, and provide
the name of the organism at the appropriate taxonomic level? Is this a
reasonable approach with regard to speed and efficiency?

Something Like:
SELECT sample_id, 'Get Name Function(Rank, TSN)', Sum([count]) AS
NoTaxa FROM dbo.tblbenthic

Results could then be grouped and summed on the Name, to summarise
data for each sample/taxa.

Is this a reasonable approach? Or is there a better one? Did I explain
the problem well enough?


I don't think so, I only understand bits of it. :-)

It may help if you post:

o CREATE TABLE statement for your table.
o INSERT statements with sample data.
o The desired result from that sample data.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

CELKO,

The move to a recursive table structue was to get away from the example
you suggested as an alternative :) Since organisms may only be IDed down
to one of the possible taxonomic levels, data may be entered to Order,
Family, Genus, or Species levels. If a row is used to identify this,
with a NULL for the 'missing' data, you end up needing to store 4
records for the complete teaxonomy of a single organism, and names must
be repeated hundreds of times (say the family has 10 Genus, then that is
20 repetitions of the Family name, to store Genus and Species)

E.G. Order, NULL, NULL, NULL
Order Family, NULL, NULL
Oder Family Genus, NULL etc.

It becomes a management nightmare when an organism changes from one
Family to another, or one Genus to another (it happens very often,
supprisingly).

4 records are still required for each level in the recursion, but there
are no NULL values, fewer columns, and the changes in taxonomy can be
altered with the change of a single relational value (the Parent TSN).
Also, repetition of higher categories does not occur, due to the
relaitonal nature of the links.

Thanks for the reply, however. I will need to look up the COALESCE
key-word, as I'm sure it will come in handy!

Tim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
>> Since organisms may only be IDed down to one of the possible
taxonomic levels, data may be entered to Order, Family, Genus, or
Species levels. If a row is used to identify this, with a NULL for the
'missing' data, you end up needing to store 4 records [sic] for the
complete taxonomy of a single organism, and names must be repeated
hundreds of times (say the family has 10 Genus, then that is 20
repetitions of the Family name, to store Genus and Species) <<

If you get more detailed information on an organism, then you update
the NULLs with the values you just discovered, don't you? One way or
the other, each organism is going to be modeled once in my table
design.
It becomes a management nightmare when an organism changes from one

Family to another, or one Genus to another (it happens very often,
surprisingly). <<

So that is just one update on poor old "Omosis Jones" to his new
taxonomy. If I have to switch him to another family and I don't know
any more about him yet, I just fill in (genus, species) with NULLs in
the same single update.

I think I might see what I am missing in this problem. Off to the
side of the lab work, you can keep a nested sets model of the taxonomy
apart from particular organisms. You can Google the basics on that
model (or I can bore the regulars by posting it again).
Jul 20 '05 #6
CELKO,

It is indeed a Nested Sets type of a problem. While there are more
elegant ways of storing the data, a simple heirarchy in this case is
very functional. The original question was not intended to ask if the
table structure was effective (I've goen through that exercise already),
but what the implications were for applying a user-defined function to
extract recursive data when a user requests summary counts at a level
higher than what the data was entered at (e.g. data entered at
Genus/Species, but summary counts requested for Orders).

I have built a SP which returns the appropriate taxonomic name at the
requested level, now I need to figure out a way to modify this into a
function, and use it in place of a column name in a query for the result
set.

I think I'm on the right track - thanks again for your input.

Tim
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #7

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

Similar topics

2
by: replace-this-with-my-name | last post by:
Hi. How do I return a string containing an entire menu-tree from a recursive function? Here is my current recursive function: function page_tree( $_i ){ //Call global mysql connection...
9
by: JP SIngh | last post by:
Hi All I am trying to write a recursive function to list the managers and his employees in a tree like sctructure Manager 1 Emp1 Emp1.1 Emp 1.2 Emp 2
2
by: Perttu Pulkkinen | last post by:
I need to find toplevel image categories and a) number of images directly in them and b) number of subcategories directly in those topcategories. In toplevel image categories "icat_parent_id IS...
2
by: Tim Pascoe | last post by:
I am writing a function which I hope to use as a column value in a select query. The function recursively walks a taxonomic heirarchy, extracting the name for an organism at the taxonomic level...
2
by: | last post by:
I'd want to make a custom class that will generate breadcrumb navigation for my site via a recursive query, e.g. Home > Page1 > Subpage 1. I've structured my database with hierarchical parent/child...
2
by: muzamil | last post by:
Hi Your help for the following query will be highly apprecaited. I've wasted alot of time on it. Data definition is at the bottom. Thanks -----------------------------------
3
by: Vincenzino | last post by:
Hi, I have some problem in using SQL3 recursive queries on DB2 database system (8.1 and 8.2 UDB). I need to compute the transitive closure of a (possibly) ciclic graph using SQL3 on DB2. I MUST...
2
by: RJN | last post by:
Hi I need help in writing a recursive function. My table structure is as below. InstanceId LevelId ParentId 100 1 null 101 2 ...
2
by: Jim Devenish | last post by:
I have a table named StockItems with field: StockItemID QuantityInStock I am creating assemblies of stock items and have another table named StockItemAssemblies with fields:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.