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!