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 630603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:52:23+00:00 2026-05-13T19:52:23+00:00

I’m not sure if this is possible from with a SQL query, but I’ll

  • 0

I’m not sure if this is possible from with a SQL query, but I’ll give it a go.

I’m developing a SharePoint web part in C# that connects to a SQL database and runs a query, then databinds that result set to a gridview. It’s working fine, but I have a small snag. For the most part, my query will return exactly one result for every field. I am displaying the information vertically, so it looks roughly like this:

Customer Name           Mr. Customer
Customer Address        980 Whatever St.
Customer City           Tallahassee

Everything displays fine. However, one of the tables in the database will pretty much always return multiple results. It lists different types of materials used for different products, so the schema is different because while each customer will obviously have one name, one address, one city, etc., they will all have at least one product, and that product will have at least one material. If I were to display that information on its own, it would look something like this:

Product              Steel Type              Wood Type             Paper Type
-----------------------------------------------------------------------------
Widget               Thick                   Oak                   Bond
Whatsit              Thin                    Birch
Thingamabob                                  Oak                   Cardstock

Ideally, I suppose the end result would be something like:

Customer Name           Mr. Customer
Customer Address        980 Whatever St.
Customer City           Tallahassee
Widget Steel            Thick
Widget Wood             Oak
Widget Paper            Bond
Whatsit Steel           Thin
Whatsit Wood            Birch
Thingamabob Wood        Oak
Thingamabob Paper       Cardstock

Another acceptable result could be something like the following, adding a few columns but only returning multiple results for those fields:

Customer Name        Mr. Customer
Customer Address     980 Whatever St.
Customer City        Tallahassee
Product              Steel Type              Wood Type             Paper Type
Widget               Thick                   Oak                   Bond
Whatsit              Thin                    Birch
Thingamabob                                  Oak                             

I’m open to any suggestions. I’d like to include this in the result set without a separate query, if that’s possible. Here is the code that I am using to pull in the data:

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Specs");
                    DataSet flipped_ds = FlipDataSet(ds);
                    DataView dv = flipped_ds.Tables[0].DefaultView;
                    GridView outputGrid = new GridView();
                    outputGrid.ShowHeader = false;
                    outputGrid.DataSource = dv;
                    outputGrid.DataBind();
                    Controls.Add(outputGrid);

I would list my SQL query, but it’s enormous. I’m pulling in well over one hundred fields right now, with lots of SUBSTRING formatting and concatenation, so it would be a waste of space, but it’s really a fairly simple query where I’m selecting fields with the AS statement to get the names that I want, and using a few LEFT JOINs to pull in the data I need without several queries. The schema of the product/material table is something like this:

fldMachineID
fldProductType
fldSteel
fldWood
fldPaper

So obviously, each customer has a number of entries, one for each different fldProductType value. If I’m leaving anything out, I can add it. Thanks for everyone’s time and help!

  • 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-05-13T19:52:24+00:00Added an answer on May 13, 2026 at 7:52 pm

    try this:

    DECLARE @TableA  table (RowID int, Value1 varchar(5), Value2 varchar(5))
    DECLARE @TableB  table (RowID int, TypeOf varchar(10))
    INSERT INTO @TableA VALUES (1,'aaaaa','A')
    INSERT INTO @TableA VALUES (2,'bbbbb','B')
    INSERT INTO @TableA VALUES (3,'ccccc','C')
    INSERT INTO @TableB VALUES (1,'wood')
    INSERT INTO @TableB VALUES (2,'wood')
    INSERT INTO @TableB VALUES (2,'steel')
    INSERT INTO @TableB VALUES (2,'rock')
    INSERT INTO @TableB VALUES (3,'plastic')
    INSERT INTO @TableB VALUES (3,'paper')
    
    
    ;WITH Combined AS
    (
    SELECT
        a.RowID,a.Value1,a.Value2,b.TypeOf
        FROM @TableA                 a
            LEFT OUTER JOIN @TableB  b ON a.RowID=b.RowID
    
    )
    SELECT
        a.*,dt.CombinedValue
        FROM @TableA        a
            LEFT OUTER JOIN (SELECT
                                 c1.RowID
                                     ,STUFF(
                                                (SELECT
                                                     ', ' + TypeOf
                                                     FROM Combined  c2
                                                     WHERE c2.rowid=c1.rowid
                                                     ORDER BY c1.RowID, TypeOf
                                                     FOR XML PATH('') 
                                                )
                                                ,1,2, ''
                                           ) AS CombinedValue
                                 FROM Combined c1
                                 GROUP BY RowID
                            ) dt ON a.RowID=dt.RowID
    

    OUTPUT:

    RowID       Value1 Value2 CombinedValue
    ----------- ------ ------ ------------------
    1           aaaaa  A      wood
    2           bbbbb  B      rock, steel, wood
    3           ccccc  C      paper, plastic
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.