Re: Location relations
Has anyone got any SQL that I can pinch to do pull out species records for a location and it's related locations?
Information Officer
Greater Lincolnshire Nature Partnership
The technical forum of the NBN Trust for use by the Network. It includes discussions and announcements on the NBN Atlas, Recorder 6 and Indicia |
You are not logged in. Please login or register.
Forum → How Do I... & Other Questions → Location relations
Has anyone got any SQL that I can pinch to do pull out species records for a location and it's related locations?
I think the inbuilt report JNCC_J3 does something like this.
File name JNCC_L3_Location_report.xml
Mike
The following shows how to extract species records for Locations using a Common Table Expression (CTE) instead of the CURSOR and temporary table normally used. CTE's were introduced in SQL server 2005 and this SQL will not work in SQL 2000.
The query would need adapting for an XML report to pick up the key for the 'seed' location.
The final SELECT statement can be modified in many ways to to pick up the other tables different columns etc.
WITH subCTE
AS
(Select L.Location_key, 0 as lvl
from Location L
WHERE L.Location_key = 'LC00000010000001A'
UNION ALL
Select LP.Location_key, P.lvl + 1
from subCTE as P
JOIN Location LP ON
LP.Parent_key = P.Location_key
)
SELECT subcte.lvl,LN.Item_Name , ITN.ACTUAL_NAME, ITN.COMMON_NAME, ITN2.Actual_Name as Recommended_name, ITN2.Common_Name as Recommended_Common_Name FROM subCTE INNER JOIN Location_name LN
ON LN.Location_Key = subCTE.Location_Key AND LN.Preferred = 1
INNER JOIN Sample S ON S.Location_Key = LN.Location_Key
INNER JOIN Taxon_Occurrence TOCC
ON TOCC.Sample_key = S.Sample_key
INNER JOIN
Taxon_Determination TDET
ON TDET.taxon_Occurrence_key = TOCC.Taxon_Occurrence_Key
INNER JOIN
INDEX_Taxon_name ITN
ON ITN.Taxon_List_Item_Key = TDET.Taxon_List_item_key
INNER JOIN Index_taxon_Name ITN2
ON ITN2.taxon_List_Item_Key= ITN.taxon_List_Item_Key
order by subcte.lvl,Ln.Item_name, ITN2.actual_name
Mike
Charlie
I have just realised that you may be looking for a query which uses the Location relation table rather Locations which are related via the hierarchy. The Location relation tables doesn't seem to be used that often and I have just one query which works on this table. This is based on a simple structure where each Location is related to just one other. However, it would be possible to set up a very complex structure with mutiple relationship of different types and I suspect the query needed to report on a complex of structure would need to be specific to the structure in place. .
Anyway if what I have is of any use I would be pleased to provide it.
Mike
Forum → How Do I... & Other Questions → Location relations
Powered by PunBB 1.4.6, supported by Informer Technologies, Inc.