Normalise Data

  • Hi,

    I have been given a task to create a pivot table from a table of data supplied via a 3rd party.

    The table is structured like this...

    CREATE TABLE [dbo].[EntityRiskGroupKeyItem](

    [Name] [nvarchar](255) NULL,

    [Value] [nvarchar](255) NULL,

    [Type] [nvarchar](255) NULL,

    [PolNo] [nvarchar] (255) NULL,

    [RISKGROUP_Id] [numeric](20, 0) NULL

    ) ON [PRIMARY]

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type, PolNo, RISKGROUP_Id )

    VALUES('Postcode','RH19 2KN','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type, PolNo, RISKGROUP_Id )

    VALUES('Postcode','RH19 2KN','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type, PolNo, RISKGROUP_Id )

    VALUES('Postcode','RH19 2KN','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type,PolNo, RISKGROUP_Id )

    VALUES('ADD1','The Street','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type,PolNo, RISKGROUP_Id )

    VALUES('ADD1','The Road','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type,PolNo, RISKGROUP_Id )

    VALUES('ADD1','The Street','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type,PolNo, RISKGROUP_Id )

    VALUES('REGION','Surrey','Live', ab123, 1234)

    GO

    What I would like to do is to transpose the data using a pivot query or something similar but to produce a seperate row for all data.

    So that I end up with something like this;

    PolicyNumber AddLine1 PostCode Region Status

    ab123 The Street RH19 2NK Surrey Live

    ...etc etc etc

    Can this be done?

  • Bryan McGuire (1/17/2011)


    Hi,

    I have been given a task to create a pivot table from a table of data supplied via a 3rd party.

    The table is structured like this...

    CREATE TABLE [dbo].[EntityRiskGroupKeyItem](

    [Name] [nvarchar](255) NULL,

    [Value] [nvarchar](255) NULL,

    [Type] [nvarchar](255) NULL,

    [PolNo] [nvarchar] (255) NULL,

    [RISKGROUP_Id] [numeric](20, 0) NULL

    ) ON [PRIMARY]

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type, PolNo, RISKGROUP_Id )

    VALUES('Postcode','RH19 2KN','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type, PolNo, RISKGROUP_Id )

    VALUES('Postcode','RH19 2KN','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type, PolNo, RISKGROUP_Id )

    VALUES('Postcode','RH19 2KN','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type,PolNo, RISKGROUP_Id )

    VALUES('ADD1','The Street','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type,PolNo, RISKGROUP_Id )

    VALUES('ADD1','The Road','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type,PolNo, RISKGROUP_Id )

    VALUES('ADD1','The Street','Live', ab123, 1234)

    INSERT INTO EntityRiskGroupKeyItem(Name, Value, Type,PolNo, RISKGROUP_Id )

    VALUES('REGION','Surrey','Live', ab123, 1234)

    GO

    What I would like to do is to transpose the data using a pivot query or something similar but to produce a seperate row for all data.

    So that I end up with something like this;

    PolicyNumber AddLine1 PostCode Region Status

    ab123 The Street RH19 2NK Surrey Live

    ...etc etc etc

    Can this be done?

    It can be done... just not with that data because there's nothing in the data the will allow us to uniquely idenitify a "whole record". Here's an article on two different methods for "converting rows to columns" IF you have that unique record identifier...

    http://www.sqlservercentral.com/articles/T-SQL/63681/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 2 posts - 1 through 2 (of 2 total)

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