Skip to main content
Inspiring
January 22, 2009
Question

ValueList question

  • January 22, 2009
  • 3 replies
  • 349 views
I need to join table A to table B via docNo. In table A, docNo is a unique key but it is not in table B.
Table A contains docNo...something else. Table B contains DocNo, Descr...something else. So the first line of table B is docNo 12345, descr D1, next line is docNo 12345, descr D2, next line docNo 9999, descr D9...etc.
When I do the join with docNo 12345, I would get two lines with the cfoutput, one for descr D1 and the other for descr D2. I tried to use valueList to combine them, hopoing to get one line only, docNo 12345, descr D1,D2.
But instead, it is giving me docNo 12345, descr D1,D2,D9...etc, all the descr in the same valuelist.

What do I need to do to get the desired output, with a comma as the separator (only if there is more than on descr) .

    This topic has been closed for replies.

    3 replies

    Inspiring
    January 22, 2009
    If the end result you want is an actual recordset rather than HTML formatted data for output, Azadi's suggestion of using a database function is the way to go. Unfortunately if you are a MS SQL Server user you don't have access to the Group_Concat() function - but that doesn't mean you can't roll your own:

    http://sqlforums.windowsitpro.com/web/forum/messageview.aspx?catid=80&threadid=28451&highlight_key=y
    Inspiring
    January 22, 2009
    The ValueList() function's job is to serialize the data for every row in a given query column as a delimeted list - not really what you're trying to do.

    I would recommed looking into the <cfquery> tag using the GROUP attribute - this should allow you to accomplish your goal.

    Something like this:

    <cfoutput query="myQuery" group="docNo">
    #docNo# -
    <cfset sDescList = "">
    <cfoutput>
    <cfset sDescList = ListAppend(sDescList, Desc)>
    </cfoutput>
    #Desc#
    </cfoutput>

    obviously, you'd have to either format your result to meet your requirements or construct a new query to contain your data.

    Hope that helps
    Inspiring
    January 22, 2009
    if you are using MySQL as your db, check out the GROUP_CONCAT()
    aggregate function:
    http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/