Tricky Query Needed

  • declare @t table

    (

    name nvarchar(10),

    English int,

    Social int

    )

    insert into @t

    select 'John' , 100, 90

    union

    select 'Mary' , 35, 87

    select * from @t

    --Output Needed

    name value

    john_english 100

    john_social 90

    mary_english 35

    mary_social 87

    In Name column concatenation of text and column name is required

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • select name + '_english', English as Value from @t

    union all

    select name + '_social', Social as Value from @t

  • Thanks Steveb but it means if i have 100 columns i need to do 100 union.That would not be a nice approach

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Bhuvnesh (2/22/2011)


    Thanks Steveb but it means if i have 100 columns i need to do 100 union.That would not be a nice approach

    The you would need to normalise your design. I'm not sure what the 'English' and 'Social' columns refer to in your design, however I would suggest that you split them out into another table (if your project allows this). Then you could try the approach below. Obviously you can add foreign key constraints and indexes, rename tables and columns as you wish, however it is just to illustrate the point.

    DECLARE @t table(

    name nvarchar(10)

    typeid int

    score int)

    DECLARE @type table(

    typeid int

    typedescription nvarchar(10)

    )

    INSERT INTO @type

    values(1, 'English');

    INSERT INTO @type

    values(2, 'Social');

    INSERT INTO @t

    values('John',1,100)

    INSERT INTO @t

    values('John',2,90)

    INSERT INTO @t

    values('Mary',1,35)

    INSERT INTO @t

    values('Mary',2,87)

    SELECT

    t.name + '_' + ty.typedescription, score

    FROM @t t INNER JOIN @type ty

    ON t.typeid = ty.typeid

  • Use below T-Sql help you to get the result . If you have more than 10 column use dynamic sql

    SELECT name + '_' + column_name as [Name],

    [Value]

    FROM (SELECT [name],

    CONVERT(VARCHAR(50), RTRIM(English )) AS English,

    CONVERT(VARCHAR(50), Social) AS Social

    FROM @t) MyTable UNPIVOT ([Value] FOR column_name IN (English, Social))AS MyUnPivot

  • Thanks Srikant . i saw your reply today only but i had the same approach yesterday to fulfill the objective.

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Another requirement

    declare @t table

    (

    name nvarchar(10),

    English int,

    Social int

    )

    insert into @t

    select 'John' , 100, 90

    union

    select 'Mary' , 35, 87

    select * from @t

    --Output Needed

    NOw there column will be 4 columns as below

    john_english,john_social,mary_english ,mary_social

    100 90 35 87

    it means that concatenation of name column's value with column's name to have new columns name

    No if new columns = existing column * values in name column ( here 2*2 = 4)

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Have a look at the CrossTab and DynamicCrossTab articles referenced in my signature.

    as a side note: Pivoting and the like usually should be done at the presentation layer, not at the database layer....



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • i got solution for this but this is not nice one 🙁

    select * from

    (

    select

    english as [john_english],

    social as [john_social]

    from @t where name = 'john') a

    ,

    (select english as [Mary_english],

    social as [Mary_social]

    from @t where name = 'Mary') b

    So "name" column has definite and constant values.

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • You can use below sql and its completely upto you ...

    drop table ##Z

    SELECT name + '_' + column_name as [Name],

    [Value]

    into ##Z

    FROM (SELECT [name],

    CONVERT(VARCHAR(50), RTRIM(English )) AS English,

    CONVERT(VARCHAR(50), Social) AS Social

    FROM @t) MyTable UNPIVOT ([Value] FOR column_name IN (English, Social))AS MyUnPivot

    declare @Column_name VARCHAR(8000),

    @cmd VARCHAR(8000)

    set @Column_name=''

    set @cmd=''

    SELECT @Column_name = @Column_name + '[' + name + ']' + ','

    FROM ##Z

    SET @Column_name=left(@Column_name, len(@Column_name) - 1)

    set @cmd ='SELECT *

    FROM ##Z ' + 'PIVOT

    (

    max(value)

    FOR [name] IN (' + @Column_name + ')

    )

    AS p'

    exec( @cmd)

  • Bhuvnesh (2/24/2011)


    i got solution for this but this is not nice one 🙁

    select * from

    (

    select

    english as [john_english],

    social as [john_social]

    from @t where name = 'john') a

    ,

    (select english as [Mary_english],

    social as [Mary_social]

    from @t where name = 'Mary') b

    So "name" column has definite and constant values.

    Did you read the CrossTab article I mentioned?

    Based on that article you could decide if you'd nee to move on to the DynamicCrossTab concept or not.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Thanks LutzM , i have read that articles and got the solution .

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply