[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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

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.
  • 2
1 Solution
Chris LuttrellSenior Database ArchitectCommented:
I just did something similar but stacked the inner values vertically using a table inside a matrix.  See http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_24331492.html 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.
tcottrillAuthor Commented:
Thank you. Your solution is very workable, and might work in a pinch.

I'd love to see your XML trick
Chris LuttrellSenior Database ArchitectCommented:
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

Open in new window


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now