HOME | FERGUSON Digital Blog

COALESCE in SQL to compile results

ColdFusion , MS SQL2008 Add comments

Our customers have a process where the lowest level of the organization submits weekly activity statuses up the chain. Each higher level may have 1 or more reporting sub levels and would require them to compile the results before submitting to their superiors. They have, for the longest time, been using email as their means of submission. The reports would come in as either messages in the email itself or Word documents attached to the email. There was no standardization among them. They would take hours cutting and pasting the comments before submitting up the ladder.

So, they came to us asking if their was a better way. We created a web application that displays a standard entry form with categories and text fields for each level to fill out. Once they submit the form, the data is collected in the tables. As the next level logs into the web application, the data from the tables would be compiled and then displayed to the screen.

In order to accomplish this, we use a Scalar-valued Function. The function receives attributes passed from the application and returns the compiled string to display. Here is how it works:

Table
UnitID   Date      CategoryID   CategoryContent   
1   6/13/2011   1      Unit 1 Cat 1
2   6/13/2011   1      Unit 2 Cat 1
3   6/13/2011   1      Unit 3 Cat 1
4   6/13/2011   1      Unit 4 Cat 1
1   6/13/2011   2      Unit 1 Cat 2
2   6/13/2011   2      Unit 2 Cat 2
3   6/13/2011   2      Unit 3 Cat 2
4   6/13/2011   2      Unit 4 Cat 2

Inside your cfquery tag you would include the function call like this

<cfquery name="CompileComments" datasource"YourDataSource">
SELECT DISTINCT
   UnitID, dbo.fnCompileComments('6/13/2011', '1', CategoryID) AS CompiledComments
FROM   TableName
WHERE   Date = '6/13/2011' AND UnitID = 1
</cfquery>

Function name in SQL is dbo.fnCompileComments. List the variables you are passing it and then declare the return Variable with AS.

The SQL function looks like this:

USE [DatabaseName]
GO
/****** Object: UserDefinedFunction [dbo].[fnCompileComments] Script Date: 06/13/2011 13:12:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[fnCompileComments]
(
   @MYDate DATE,
   @MYUnitID INT,
   @MYCategoryID INT
)
RETURNS VARCHAR(MAX)
AS
BEGIN
   DECLARE @Comments VARCHAR(MAX)

SELECT @Comments = COALESCE(@Comments + char(10) + char(13) + '|', + '') + CategoryContent
FROM   TableName
WHERE Dater = @MyDate AND
   UnitID = @MyUnitID AND
CategoryID = @MyCategoryID

RETURN @Comments

END

The expected result:
Unit 1 Cat 1
|Unit 2 Cat 1
|Unit 3 Cat 1
|Unit 4 Cat 1

Unit 1 Cat 2
|Unit 2 Cat 2
|Unit 3 Cat 2
|Unit 4 Cat 2

The user defined function uses the COALESCE function to compile the comments together. I use a char(10) and a char(13) to put a return line and carriage return between each comment. I also include a unique character "|" that I use for a specfic function elsewhere in the application.

26 responses to “COALESCE in SQL to compile results”

  1. Anthony on Hi,
    I was hoping you could help me. I need to create a view that will return a list of partNo, desc, rrp along side a coalesce'd line of the compatible handset names for each partno.
    Thanks
    Anhony
  2. Dustin Brisson on I will help where I can.

    First you will have to create a query to return the desired results of the compatible handset names for each partNo, desc, rrp. This will be the query within the Scalar-valued Function.

    Second, create your Scalar-valued Function.

    Lastly, write your stored procedure calling the Scalar-valued Function passing it the arguements of partNo, desc, rrp.

    Something like this:
    SQL query:

    SELECT handset
    FROM TableName
    WHERE partNo = value, desc = value, rrp = value


    SQL Scalar-valued Function:

    USE [DatabaseName]
    GO

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    CREATE FUNCTION [dbo].[fnGetHandsets]
    (
    @MyPartNo TYPE,
    @MyDesc INT,
    @MyRRP INT
    )
    RETURNS VARCHAR(MAX)
    AS
    BEGIN
    DECLARE @CompatibleHandsets VARCHAR(MAX)

    SELECT @CompatibleHandsets = COALESCE(@CompatibleHandsets + char(10) + char(13) + '|', + '') + TableName.Handset
    FROM TableName
    WHERE partNo = @MyPartNo AND
    desc = @MyDesc AND
    rrp = @MyRRP

    RETURN @CompatibleHandsets

    END


    Stored Procedure with arguements:

    <query name="GetHandsets" datasource"YourDataSource">
    SELECT DISTINCT
    partNo, desc, rrp, dbo.fnGetHandsets('partNo.value', 'desc.value', 'rrp.value') AS CompatibleHandsets
    FROM TableName
    WHERE partNo = 'value' AND desc = 'value' AND rrp = 'value'
    </query>
  3. Christian Louboutin Boots on Wow, I feel what you talked about earlier could be very true. Shows what I??m experiencing. Correctly since I??m already here, surprise should you can be form ample to trade hyperlinks with my site. I will possible be inserting your link inside the blogroll section, and that i hope that our hyperlink commerce can assist us make our blogs better. Hope youre capable of fulfill my humble request.
  4. Air Jordan 8 on Im still learning from you, as Im trying to achieve my goals. I certainly liked reading everything that is written on your blog.Keep the stories coming. I loved it!
  5. monsterheadphones on [url=http://www.salescheapbeatsbydre.com/][b]cheap beats by dre[/b][/url] This is the interpretation of the article is very good, [url=http://www.salescheapbeatsbydre.com/][b]monster headphones[/b][/url] pass this article I read you can have different harvest, [url=http://www.salescheapbeatsbydre.com/beats-by-dre-dre-lady-gaga-headphones-c-1_23.html][b]Dre Lady Gaga Headphones[/b][/url] good article introduced to share the happiness, [url=http://www.salescheapbeatsbydre.com/beats-by-dre-dre-new-style-beats-by-dre-c-1_52.html][b]Dre New style Beats By Dre[/b][/url] come in and see not cost you too much time but can let you learn a lot[url=http://www.salescheapbeatsbydre.com/beats-by-dre-dre-beats-by-dre-black-c-1_12.html][b]Dre Beats By Dre black[/b][/url]
    http://www.salescheapbeatsbydre.com/
  6. Cheap Christian Louboutin on Very nice pattern and excellent articles.
  7. northface on <p><a href="http://www.northfacehonsale.org"><strong>north face shop</strong></a> the application of good, <a href="http://www.northfacehonsale.org"><strong>north face outlet</strong></a> diction properly make people who read up very comfortable,<strong> <a href="http://www.northfacehonsale.org/womens-north-face-apex-bionic-c-77.html">the north face apex</a></strong> plus beautiful sentences,<strong> <a href="http://www.northfacehonsale.org/womens-north-face-down-c-79.html">northface down</a></strong> can reflect the value of more articles <strong><a href="http://www.northfacehonsale.org/mens-north-face-down-c-71.html">down jacket</a></strong>.<strong> </strong></p>
    http://www.northfacehonsale.org/
  8. Daysia on You got to push it-this eesnsital info that is!
  9. vnphcqa on 7VTz7O <a href="http://cnjigsmkqipf.com/">cnjigsmkqipf</a>
  10. fasajwjohu on D1rMiC , [url=http://ipcgnriejgbo.com/]ipcgnriejgbo[/url], [link=http://uylxncgaarxw.com/]uylxncgaarxw[/link], http://eaxhdniatxon.com/
  11. gknucyneo on MRkA5R , [url=http://ntropaztpyqb.com/]ntropaztpyqb[/url], [link=http://imdfzljiymqp.com/]imdfzljiymqp[/link], http://gxmenolqdcup.com/
  12. amgvpxj on 5eYAgq , [url=http://qxvrgdlsrkmo.com/]qxvrgdlsrkmo[/url], [link=http://hstpnrxtqqcl.com/]hstpnrxtqqcl[/link], http://dkrdwkgesvdf.com/
  13. msrdjslm on mi1UEx , [url=http://txhdffedpkda.com/]txhdffedpkda[/url], [link=http://nexlbxwxmoan.com/]nexlbxwxmoan[/link], http://hnlfexadhxws.com/
  14. cuoqipsjqnh on gFIznT , [url=http://nitxyqvdinhr.com/]nitxyqvdinhr[/url], [link=http://ixrfuxnkjzxj.com/]ixrfuxnkjzxj[/link], http://yeexasljpzzk.com/
  15. jinwwrimf on 7zAegu , [url=http://fmuaugejqwwb.com/]fmuaugejqwwb[/url], [link=http://iotcgygggnxl.com/]iotcgygggnxl[/link], http://xhptskcizmok.com/
  16. Cheap Jordan Shoes on I will possible be inserting your link inside the blogroll section, and that i hope that our hyperlink commerce can assist us make our blogs better. Hope youre capable of fulfill my humble request.
  17. Authentic Jordan Shoes on Very nice pattern and excellent articles, practically nothing else we want .
  18. Authentic Jordan Shoes on Hope youre capable of fulfill my humble request.
  19. north face denali on Groupon price <a href="http://www.northface-denali.net/"><strong>north face denali</strong></a> camping, hiking or backpacking, the <a href="http://www.northface-denali.net/"><strong>north face fleece jackets</strong></a> from The North Face remains one of the most popular outerwear designs in the outdoor industry.
  20. north face on Discount <a href="http://www.cheapnorthfaces.net/"><strong>cheap north face jackets</strong></a> online is a good change to get cheap <a href="http://www.cheapnorthfaces.net/"><strong>north face fleece</strong></a>. High quality <a href="http://www.cheapnorthfaces.net/"><strong>north face</strong></a> offer womens, mens and kids jackets. Buy cheap north face now.
  21. Coach Outlet Store Online on It's really a good post. I have learnt a lot from it. I will recommend it to my firends. Hope they ill like it too.
  22. matt ryan jerseys on How happy to read your post, I hope I can write the article the same wonderful as you.
  23. Coach Outlet Store Online on Good post for learning. I have recommend this post to my friends. There will be more people reading your posts. Thank you!
  24. Ford ids on Obd2 Products like Obd-2 scantools, obdii scanners, obd2 cables and J1962 connector 100Z
  25. Nike SB on Today for everybody who is interested in an merchandise with the Tiffany & co Jewelery and Business to line of jewelry, I suppose that what you desire is really a true top grade Tiffany's merchandise.
  26. Beats Pro Wholesale on The quality of the <a href="http://www.beatsbydrdrer.com/"><strong>Cheap Beats By Dre</strong></a> seems good to me, never had a problem with it. I really like the flat cable, staying true to <a href="http://www.beatsbydrdrer.com/beats-dr-dre-detox-c-7.html"><strong>Beats By Dre Cheapest Price</strong></a>'s word, they don't tangle ever. I use <a href="http://www.beatsbydrdrer.com/beats-dr-dre-detox-c-7.html"><strong>Cheap Beats By DreSolo Hd</strong></a> everyday and even when out biking or I'm at the gym and they are still fine, not a problem build-wise.

Leave a Reply




© Copyright 1997-2012, All Rights Reserved Coldfusion and MS SQL2008
Powered by Mango Blog.   Design by FERGUSON Digital
o:Event name="beforeHtmlBodyEnd" />