473,391 Members | 1,454 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,391 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 6309
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.