Showing all values in a cell in a matrix in SQL Server 2005

Posted on 2009-04-17
Last Modified: 2013-11-05
Here's my deal. I've got a pretty simple matrix set up that is pulling data from a reasonably complex query. The end result is a matrix that looks something like this

Keywords      | ranking

For the Most part, it's working great.

The problem is that the ranking field is currently only displaying the top value, while it could have multiple values. I don't want to do a sum or any kind of aggregation. Ideally, I'd just want a comma seperated list or running list of the values.

For example, if a person ranks for an term in two places in a month, I'd like it to display "1, 17" rather than just 1....

Any and all help is greatly appreciated.
Question by:tcottrill
    LVL 26

    Expert Comment

    by:Chris Luttrell
    I just did something similar but stacked the inner values vertically using a table inside a matrix.  See and see if that kind of solution  could work for you.  I have been trying to do a matrix in side a matrix to stack them horizontally but with no real success.
    The other option is to play with the query from the database and turn that column into a comma separated string.  Let me know if you need to go that route, I have an XML trick to do that.

    Author Comment

    Thank you. Your solution is very workable, and might work in a pinch.

    I'd love to see your XML trick
    LVL 26

    Accepted Solution

    As always, things are based on what you have to start with and what you need output.
    This example is basicallly concatinating the data from PetName, by type (dog, cat) into one string.
    The image shows the raw joined data and the results.
    I included a script file if you want to try it out.
    SELECT DISTINCT c.TypeName, PetNames = REPLACE((SELECT a.PetName AS [data()]
                       FROM PetName AS a
                       WHERE a.PetTypeId = c.PetTypeId
                       ORDER BY PetName
                       FOR XML PATH ('')), ' ', ',')
    FROM PetType AS c
    ORDER BY TypeName

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Steps to solve SSRS SQL 2008 R2 User Access Control (UAC) Permission Error With the introduction of SQL Server 2008 R2 and Vista (Windows 7 as well) came new enhanced security features. One of the features included was User Access Control (UAC) t…
    Introduction As you'll probably know, a data region in a SQL Server Reporting Services report can be linked to only one dataset.  This makes it troublesome when you need to display data from more than one dataset in the same data region.  SQL Serve…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now