0

I have table like this below,

Type             Model       Year    
121232323        Test1       2000
121232323        Test2       2001

I want output like below, how to write query for that.?

121232323       Test1      Test2
                2000       2001
3
  • 4
    Where did Originalnr come from? Commented May 25, 2016 at 6:44
  • And whence is the value 2124? Commented May 25, 2016 at 6:50
  • I want sql select query without passing values.
    – user123
    Commented May 25, 2016 at 6:54

3 Answers 3

2

Using Simple Pivot we can achieve

DECLARE @Table1 TABLE 
    ( Type  int,  Model  varchar(5),  Year  int)
;

INSERT INTO @Table1
    ( Type ,  Model ,  Year )
VALUES
    (121232323, 'Test1', 2000),
    (121232323, 'Test2', 2001)
;


select Type,[Test1],[Test2] from @Table1
PIVOT (MAX(YEAR) FOR MODEL IN ([Test1],[Test2]))PVT
1
  • I want output like below, how to write query for that.? I want to sql select query to select dynamically with headers. Type Model Year 121232323 Test1 2000 Test2 2001 Test1 2000 121232330 Test2 2001 121232340 Test1 2000 121232350 Test2 2001
    – user123
    Commented May 25, 2016 at 12:30
2

I don't know about Originalnr , but you can do this with conditional aggregation if you have a limited amount of tests :

SELECT t.Type,
       MAX(CASE WHEN t.Model = 'Test1' THEN t.Year END) as Test1,
       MAX(CASE WHEN t.Model = 'Test2' THEN t.Year END) as Test2
FROM YourTable t
GROUP BY t.Type
2
  • I want sql select query without passing values.
    – user123
    Commented May 25, 2016 at 6:55
  • I want output like below, how to write query for that.? I want to sql select query to select dynamically with headers. Type Model Year 121232323 Test1 2000 Test2 2001 Test1 2000 121232330 Test2 2001 121232340 Test1 2000 121232350 Test2 2001
    – user123
    Commented May 25, 2016 at 12:31
2

Another method other than PIVOT is to use conditional aggregation:

SELECT
    Type,
    Test1 = MAX(CASE WHEN Model = 'Test1' THEN [year] END),
    Test2 = MAX(CASE WHEN Model = 'Test2' THEN [year] END)
FROM tbl
GROUP BY Type

If you have unlimited number of Models you can do it using dynamic crosstab:

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql =
'SELECT
    Type' + CHAR(10) +
(SELECT DISTINCT
'   , MAX(CASE WHEN Model =''' + Model +  ''' THEN [year] END) AS ' + QUOTENAME(Model) + CHAR(10)
FROM tbl
FOR XML PATH('')
) +
'FROM tbl
GROUP BY Type;';

PRINT(@sql);
EXEC(@sql);

ONLINE DEMO

5
  • I want sql select query without passing values.
    – user123
    Commented May 25, 2016 at 6:55
  • @user123 Then yuo need to do it dynamically. Commented May 25, 2016 at 6:56
  • how.? i need column name?
    – user123
    Commented May 25, 2016 at 6:59
  • See the second part of my answer. It's done using dynamic sql. Commented May 25, 2016 at 6:59
  • I want output like below, how to write query for that.? I want to sql select query to select dynamically with headers. Type Model Year 121232323 Test1 2000 Test2 2001 Test1 2000 121232330 Test2 2001 121232340 Test1 2000 121232350 Test2 2001
    – user123
    Commented May 25, 2016 at 12:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.