1

Re: My arm for GROUP_CONCAT

I've been toiling with this all day...

What I want to do is run an XML report on all the surveys on the database, pulling out various bits of information such as, name, number of records in survey etc.

It's working great except I can't get my head around pulling out the taxon groups that make up the records in the survey.

I want something like:

name         | run by             |number of records | taxon groups
===============================================================================
River Survey | environment agency | 1200             | odonata, freshwater fish

With the taxon groups concatenated in one field. Normally I would use a group_concat, but M$ obviously thought that wouldn't be useful!!

I'm sure Recorder must do this in various places (Recorder names?), is there any functions in there I can use that would do this?

Charlie Barnes
Information Officer
Greater Lincolnshire Nature Partnership

2 (edited by MikeWeideli 02-12-2010 21:19:51)

Re: My arm for GROUP_CONCAT

Charlie

Concatenation is done using User Defined Functions. The following pulls out the Taxon Groups as a semi colon
separated string given the Survey_key as a parameter. Once you have installed the UDF you should  be able to work it into your query.

Mike 
 
USE [NBNData]
GO

/****** Object:  UserDefinedFunction [dbo].[LCFormatTaxonGroup]    Script Date: 12/02/2010 20:55:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[LCFormatTaxonGroup]
(@Survey_key char(16))
RETURNS varchar(8000)
--
--    DESCRIPTION
--    Function to return a semi-colon separated string of taxon groups within a survey
--   
--
--    PARAMETERS
--    NAME                DESCRIPTION
--    @Survey_Key            Survey Key.
--
--
--    AUTHOR:    Mike Weideli
--    CREATED: 02/12/2010
--

AS
BEGIN

--****************************************************************************************************
DECLARE @ReturnString varchar(8000)
DECLARE @Taxon_Groups  varchar(8000)

SET @Taxon_Groups    =''
SELECT @Taxon_Groups  = @Taxon_Groups   + D.Taxon_Group_Name  +  '; '
FROM
(SELECT  TAXON_GROUP_NAME FROM
Taxon_group TG
INNER JOIN
TAXON_VERSION TV ON TV.OUTPUT_GROUP_KEY = TG.TAXON_GROUP_KEY
INNER JOIN
TAXON_LIST_ITEM TLI ON TLI.TAXON_VERSION_KEY = TV.TAXON_VERSION_KEY
INNER JOIN TAXON_DETERMINATION TDET
ON TDET.TAXON_LIST_ITEM_KEY = TLI.TAXON_LIST_ITEM_KEY
INNER JOIN TAXON_OCCURRENCE TOCC
ON TOCC.TAXON_OCCURRENCE_KEY = TDET.TAXON_OCCURRENCE_KEY
INNER JOIN SAMPLE S
ON S.SAMPLE_KEY = TOCC.SAMPLE_KEY
INNER JOIN SURVEY_EVENT SE
ON SE.SURVEY_EVENT_KEY = S.SURVEY_EVENT_KEY
WHERE SE.SURVEY_KEY = @Survey_key GROUP BY TAXON_GROUP_NAME ) D



if len(@Taxon_Groups) > 0
BEGIN
  set  @RETURNSTRING  = left(@Taxon_groups,len(@Taxon_groups)-1)
END

--****************************************************************************************************
RETURN @ReturnString
END


GO

GRANT EXECUTE ON  [dbo].[LCFormatTaxonGroup] TO PUBLIC

Mike Weideli

3

Re: My arm for GROUP_CONCAT

That's a useful one, thanks Mike

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

4

Re: My arm for GROUP_CONCAT

MikeWeideli wrote:

Concatenation is done using User Defined Functions.

Thank-you Mike!

Charlie Barnes
Information Officer
Greater Lincolnshire Nature Partnership

5

Re: My arm for GROUP_CONCAT

Hi Mike,

Working great, but the output is being truncated to 255 characters. Is this a limit of Recorder/Excel 2.1?

Charlie Barnes
Information Officer
Greater Lincolnshire Nature Partnership

6

Re: My arm for GROUP_CONCAT

The UDF is returning 8000 characters. Is the data in the R6 output truncated or just it does happen when it is exported to Excel ?

Mike Weideli

7

Re: My arm for GROUP_CONCAT

Just tried an export to TXT, and the fields seem intact. I guess it's a limitation of Excel?

Charlie Barnes
Information Officer
Greater Lincolnshire Nature Partnership