Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7868277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:57:02+00:00 2026-06-03T00:57:02+00:00

I’ve reviewed many other posts on here and have become pretty familiar with the

  • 0

I’ve reviewed many other posts on here and have become pretty familiar with the Coalesce function, but I haven’t been able to figure out how to do this specific task.

So, I have a Commissions table and a Categories table. I’ve created a gist here so you can see the exact data structure with some example data. Basically, the Commission table has a SalesRepID, LocationID, CategoryID, SurgeonID, and CommissionPercent column.

Using a Coalesce function, I’ve been able to get something like this by passing in the SalesRepID, LocationID, and SurgeonID:

.05 (Shirts), .05 (Shoes), .05 (Dresses), .10 (Hats), .15 (Pants)

However, I’m trying to get it to look like:

.05 (Shirts, Shoes, Dresses), .10 (Hats), .15 (Pants)

I did try it a few times with STUFF, but I never got the result that I’m looking for.

Which leads me to ask if this is even possible in MsSQL 2008 R2? If it is, any help in getting the result I’m looking for would be greatly appreciated.

Thank you very much for your time & energy,

Andrew

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:57:03+00:00Added an answer on June 3, 2026 at 12:57 am

    Thank you for the gist! So much better than pulling teeth to get schema and data. 🙂 If you plug this in to your gist query you should see the results you’re after (well, very close – see below).

    DECLARE @SalesRepID int = 2, 
            @SurgeonID  int = 1, 
            @LocationID int = 1;
    
    ;WITH x AS 
    (
      SELECT CommissionPercent, Categories = STUFF((SELECT N', ' 
          + tCat.Category FROM #tCategories AS tCat 
          INNER JOIN #tCommissions AS tCom 
          ON tCat.CategoryID = tCom.CategoryID
          WHERE tCom.CommissionPercent = com.CommissionPercent
          FOR XML PATH, 
          TYPE).value(N'./text()[1]', N'nvarchar(max)'), 1, 2, N'') 
     FROM #tCommissions AS com
     WHERE SalesRepID = @SalesRepID
       AND SurgeonID  = @SurgeonID
       AND LocationID = @LocationID
    ),
    y AS
    (
      SELECT s = RTRIM(CommissionPercent) + N' (' + Categories + N')' 
      FROM x GROUP BY CommissionPercent, Categories
    )
    SELECT Result = STUFF((SELECT N', ' + s FROM y 
      FOR XML PATH, 
      TYPE).value(N'./text()[1]', N'nvarchar(max)'), 1, 2, N'');
    

    The result is slightly different than you asked for, but you could fix that by applying string formatting when pulling CommissionPercent.

    Result
    --------------------------------------------------------
    0.05 (Shirts, Shoes, Dresses), 0.10 (Hats), 0.15 (Pants)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I want to construct a data frame in an Rcpp function, but when I
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.