SQL Script

  • Hi All,

    Could you please help me in get the script for below scenario:

    Table Creation and Insertion:
    CREATE TABLE [dbo].[Testresult](
        [Name] [varchar](50) NULL,
        [Admin] [int] NULL,
        [Developer] [int] NULL,
        [Tester] [int] NULL,
        [Manager] [int] NULL,
        [EndUser] [int] NULL
    ) ON [PRIMARY]

    GO

    INSERT INTO [dbo].[Testresult] VALUES ('XYZ',1,0,0,0,0)
    INSERT INTO [dbo].[Testresult] VALUES('YZX',0,1,0,0,0)
    INSERT INTO [dbo].[Testresult] VALUES('ZXY',1,0,0,0,0)
    INSERT INTO [dbo].[Testresult] VALUES('ABC',0,0,1,0,0)
    INSERT INTO [dbo].[Testresult] VALUES('BCA',0,0,0,1,0)
    INSERT INTO [dbo].[Testresult] VALUES('CAB',0,0,0,0,1)

    select * from [Testresult]

    Below is the results

    NameAdminDeveloperTesterManagerEndUser
    XYZ10000
    YZX01000
    ZXY10000
    ABC00100
    BCA00010
    CAB00001

    I need results in below format. Wherever value is 1, respective name should be displayed under that columns, like shown below.

    AdminDeveloperTesterManagerEndUser
    XYZYZXABCBCACAB
    ZXY    

    Please help me to get the query.

    I tried below method but could not get much help.

    Select name as Admin into #temp1 from [Testresult] where Admin=1
    Select name as Developer into #temp2 from [Testresult] where Developer=1
    Select name as Tester into #temp3 from [Testresult] where Tester=1
    Select name as Manager into #temp4 from [Testresult] where Manager=1
    Select name as EndUser into #temp5 from [Testresult] where EndUser=1

    Select * from #temp1,#temp2,#temp3,#temp4,#temp5
    Below is the results:

    AdminDeveloperTesterManagerEndUser
    XYZYZXABCBCACAB
    ZXYYZXABCBCACAB

    Please help me here

    Thank You.

    Thank You.

    Regards,
    Raghavender Chavva

  • Raghavender Chavva - Monday, February 11, 2019 10:10 AM

    Hi All,

    Could you please help me in get the script for below scenario:

    Orginal Table structure:

    NAMECol1Col2Col3Col4
    x1000
    y0100
    z0010
    a1000
    b0001
    c0000

    I need results in below format:

    Col1Col2Col3Col4
    xyzb
    a   

    Please help me to get the query.

    Thank You.

    You've been here long enough. You should know that you need to post sample data in a consumable format for people to show some support. Also post what you have tried.
    This screams bad design all over the place. I don't understand why would you store or show data like that. You might want to consider a redesign of your tables.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares - Monday, February 11, 2019 10:34 AM

    Raghavender Chavva - Monday, February 11, 2019 10:10 AM

    Hi All,

    Could you please help me in get the script for below scenario:

    Orginal Table structure:

    NAMECol1Col2Col3Col4
    x1000
    y0100
    z0010
    a1000
    b0001
    c0000

    I need results in below format:

    Col1Col2Col3Col4
    xyzb
    a   

    Please help me to get the query.

    Thank You.

    You've been here long enough. You should know that you need to post sample data in a consumable format for people to show some support. Also post what you have tried.
    This screams bad design all over the place. I don't understand why would you store or show data like that. You might want to consider a redesign of your tables.

    I think you also might know some of the requirements come as adhoc requests and for those adhoc requirements nobody consider redesign of tables. 
    Coming to the point some times the most experience guy also requires help from others.
    Let me know consumable format means ?

    Thank You.

    Regards,
    Raghavender Chavva

  • Raghavender Chavva - Monday, February 11, 2019 10:49 AM

    Let me know consumable format means ?

    Read the articles linked in my signature on "how to post data/code on a forum to get the best help"

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares - Monday, February 11, 2019 11:13 AM

    Raghavender Chavva - Monday, February 11, 2019 10:49 AM

    Let me know consumable format means ?

    Read the articles linked in my signature on "how to post data/code on a forum to get the best help"

    Thank you. will check reframe the question.

    Thank You.

    Regards,
    Raghavender Chavva

  • You also need to specify the logic used to determine which values appear together.  NOTE: Tables represent sets, which are unordered.  The display order is not a valid option.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • /*I have tried using Row Number and Left outer join to display the data in the required format
    1. Derived column called RID will be created and used to map the columns*/

    SELECT
    ISNULL([tblAdmin].[Name],'') [Admin]
    ,ISNULL([tblDeveloper].[Name],'') Developer
    ,ISNULL([tblTester].[Name],'') Tester
    ,ISNULL([tblManager].[Name],'') Manager
    ,ISNULL([tblEndUser].[Name],'') EndUser
    FROM
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID FROM [Testresult] --Create Row ID for list of rows
    ) tblMain
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID, CASE [Admin] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [Admin] = 1
    ) [tblAdmin] ON tblMain.RID = [tblAdmin].RID
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID,CASE [Developer] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [Developer] = 1
    ) [tblDeveloper] ON tblMain.RID = [tblDeveloper].RID
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID,CASE [Tester] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [Tester] = 1
    ) [tblTester] ON tblMain.RID = [tblTester].RID
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID,CASE [Manager] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [Manager] = 1
    ) [tblManager] ON tblMain.RID = [tblManager].RID
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID,CASE [EndUser] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [EndUser] = 1
    ) [tblEndUser] ON tblMain.RID = [tblEndUser].RID
    WHERE
    [tblAdmin].RID IS NOT NULL
    OR [tblDeveloper].RID IS NOT NULL
    OR [tblTester].RID IS NOT NULL
    OR [tblManager].RID IS NOT NULL
    OR [tblEndUser].RID IS NOT NULL

  • k.karthik0889 - Wednesday, February 13, 2019 3:32 AM

    /*I have tried using Row Number and Left outer join to display the data in the required format
    1. Derived column called RID will be created and used to map the columns*/

    SELECT
    ISNULL([tblAdmin].[Name],'') [Admin]
    ,ISNULL([tblDeveloper].[Name],'') Developer
    ,ISNULL([tblTester].[Name],'') Tester
    ,ISNULL([tblManager].[Name],'') Manager
    ,ISNULL([tblEndUser].[Name],'') EndUser
    FROM
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID FROM [Testresult] --Create Row ID for list of rows
    ) tblMain
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID, CASE [Admin] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [Admin] = 1
    ) [tblAdmin] ON tblMain.RID = [tblAdmin].RID
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID,CASE [Developer] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [Developer] = 1
    ) [tblDeveloper] ON tblMain.RID = [tblDeveloper].RID
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID,CASE [Tester] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [Tester] = 1
    ) [tblTester] ON tblMain.RID = [tblTester].RID
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID,CASE [Manager] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [Manager] = 1
    ) [tblManager] ON tblMain.RID = [tblManager].RID
    LEFT OUTER JOIN
    (
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) RID,CASE [EndUser] WHEN 1 THEN [Name] ELSE NULL END [Name] FROM [Testresult] WHERE [EndUser] = 1
    ) [tblEndUser] ON tblMain.RID = [tblEndUser].RID
    WHERE
    [tblAdmin].RID IS NOT NULL
    OR [tblDeveloper].RID IS NOT NULL
    OR [tblTester].RID IS NOT NULL
    OR [tblManager].RID IS NOT NULL
    OR [tblEndUser].RID IS NOT NULL

    This is a very expensive way of doing it. Unpivoting and pivoting the data would be a lot more efficient.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • This doesn't pivot the data but is a fast (and a bit more simple than yours) way to get the subset of information back that you want.
    select t.Name,x.Type, x.Value
      from [Testresult] t
     cross apply(values('Admin',t.Admin),('Developer',t.Developer),('Tester',t.Tester),('Manager',t.Manager),('EndUser',t.EndUser)) x(Type,Value)
     where x.Value =1

    Just put it in a CTE and write a query on the CTE that does what you want.

  • Luis Cazares - Wednesday, February 13, 2019 8:32 AM

    This is a very expensive way of doing it. Unpivoting and pivoting the data would be a lot more efficient.

    I was thinking the same thing, although, you might be able to skip the unpivot if you can guarantee that exactly ONE of the columns will equal 1.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • I just noticed that there's sample data posted now. Here's a solution that will pivot the data.

    WITH Unpivoted AS(
      SELECT *, ROW_NUMBER() OVER( PARTITION BY Col ORDER BY (SELECT NULL)) rn
      FROM [Testresult]
      CROSS APPLY (VALUES('Admin', [Admin]),
            ('Developer', Developer),
            ('Tester', Tester),
            ('Manager', [Manager]),
            ('EndUser', [EndUser]))u(Col, Val)
      WHERE Val = 1
    )
    SELECT MAX( CASE WHEN Col = 'Admin' THEN [Name] END) AS [Admin],
       MAX( CASE WHEN Col = 'Developer' THEN [Name] END) AS Developer,
       MAX( CASE WHEN Col = 'Tester' THEN [Name] END) AS Tester,
       MAX( CASE WHEN Col = 'Manager' THEN [Name] END) AS Manager,
       MAX( CASE WHEN Col = 'EndUser' THEN [Name] END) AS EndUser
    FROM Unpivoted
    GROUP BY rn

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares - Wednesday, February 13, 2019 9:40 AM

    I just noticed that there's sample data posted now. Here's a solution that will pivot the data.

    WITH Unpivoted AS(
      SELECT *, ROW_NUMBER() OVER( PARTITION BY Col ORDER BY (SELECT NULL)) rn
      FROM [Testresult]
      CROSS APPLY (VALUES('Admin', [Admin]),
            ('Developer', Developer),
            ('Tester', Tester),
            ('Manager', [Manager]),
            ('EndUser', [EndUser]))u(Col, Val)
      WHERE Val = 1
    )
    SELECT MAX( CASE WHEN Col = 'Admin' THEN [Name] END) AS [Admin],
       MAX( CASE WHEN Col = 'Developer' THEN [Name] END) AS Developer,
       MAX( CASE WHEN Col = 'Tester' THEN [Name] END) AS Tester,
       MAX( CASE WHEN Col = 'Manager' THEN [Name] END) AS Manager,
       MAX( CASE WHEN Col = 'EndUser' THEN [Name] END) AS EndUser
    FROM Unpivoted
    GROUP BY rn

    Thanks.

  • Luis Cazares - Wednesday, February 13, 2019 9:40 AM

    I just noticed that there's sample data posted now. Here's a solution that will pivot the data.

    WITH Unpivoted AS(
      SELECT *, ROW_NUMBER() OVER( PARTITION BY Col ORDER BY (SELECT NULL)) rn
      FROM [Testresult]
      CROSS APPLY (VALUES('Admin', [Admin]),
            ('Developer', Developer),
            ('Tester', Tester),
            ('Manager', [Manager]),
            ('EndUser', [EndUser]))u(Col, Val)
      WHERE Val = 1
    )
    SELECT MAX( CASE WHEN Col = 'Admin' THEN [Name] END) AS [Admin],
       MAX( CASE WHEN Col = 'Developer' THEN [Name] END) AS Developer,
       MAX( CASE WHEN Col = 'Tester' THEN [Name] END) AS Tester,
       MAX( CASE WHEN Col = 'Manager' THEN [Name] END) AS Manager,
       MAX( CASE WHEN Col = 'EndUser' THEN [Name] END) AS EndUser
    FROM Unpivoted
    GROUP BY rn

    Thank You Luis. It helped me a lot.

    Thank You.

    Regards,
    Raghavender Chavva

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

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