0
ID  Model   Color
1   BMW     Red
2   BMW     Green
3   BMW     Yellow
4   BMW     Black
5   Golf    Red
6   Golf    Green
7   Golf    Yellow
8   Golf    Black

I have model of the car and colors as table and i want to make it in one row as this. This will display in datagrid in c#

Model   Color1  Color2  Color3  Color4
BMW     Red     Green   Yellow  Black
Golf    Red     Green   Yellow  Black

--Edit I have a car model that has four colors. I want this to show in one row. new name columns can be anything can be ID but ID can be changed.

So it can be like...

Model   1        2      3       4
BMW     Red     Green   Yellow  Black
Golf    Red     Green   Yellow  Black
2
  • 1
    What's your question? What are you trying to do, and what have you got so far? Commented Aug 21, 2014 at 13:36
  • I want one row as model but adding four columns and make this which will be distributed in color. Commented Aug 21, 2014 at 13:49

5 Answers 5

1

I believe PIVOT will assist you:

http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

Sign up to request clarification or add additional context in comments.

Comments

0
SELECT model, group_concat(DISTINCT color)
  FROM table
 GROUP BY model

It will give 2 recodes. model name and string of color model. Then you need to process in C# to get individual colors.

Comments

0

I'd start with:

SELECT COUNT(*), Model, Color
  FROM Table
 GROUP BY Model, Color

and then use the count it returns to construct your table.

2 Comments

How would this approach help to achieve the desired result?
@jpw I think the variety of answers given suggests that no one is quite clear on what the desired result is :)
0
SELECT MODEL, [Red], [Green], [Yellow], [Black]
  FROM (
       SELECT model, color
         FROM tblCartable
        WHERE color IN ('Red', 'black', 'yellow', 'green')
       ) x
 PIVOT first(model) FOR color IN ([Red], [Green], [Yellow], [Black])) p

Comments

0
SELECT *
  FROM (SELECT Model, Color FROM TableName) as SourceT
 PIVOT (MAX(Color) FOR Color IN ([Red], [Green], [Yellow], [Black])) as Pvt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.