Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Displaying multiple rows into single row as columns based on id - Sql Server

Displaying multiple rows into single row as columns based on id - Sql Server

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelptutorial
7 Posts 5 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11119933
    wrote on last edited by
    #1

    Hello Friends, my requirement is, I have table with following fields EmpID,RequestNo,RequestDate,RequestType,RequestStatus. Now, I need to fetch the top 2 rows of the employee based on the employee ID with request date desc, and should display data of these 2 rows into 1 row with the fields: I have query to fetch only top 2 rows, now I should know how to display that 2 rows into single rows with columns. EmpID Current ReqNo Current ReqDate Current ReqType Current ReqStatus Previous ReqNo Previous ReqDate Previous ReqType Previous ReqStatus Kindly help, I googled and found Pivot, but I am not getting exact result. Thank you so much, Best Regards, Priya.

    CHill60C V Richard DeemingR 3 Replies Last reply
    0
    • U User 11119933

      Hello Friends, my requirement is, I have table with following fields EmpID,RequestNo,RequestDate,RequestType,RequestStatus. Now, I need to fetch the top 2 rows of the employee based on the employee ID with request date desc, and should display data of these 2 rows into 1 row with the fields: I have query to fetch only top 2 rows, now I should know how to display that 2 rows into single rows with columns. EmpID Current ReqNo Current ReqDate Current ReqType Current ReqStatus Previous ReqNo Previous ReqDate Previous ReqType Previous ReqStatus Kindly help, I googled and found Pivot, but I am not getting exact result. Thank you so much, Best Regards, Priya.

      CHill60C Offline
      CHill60C Offline
      CHill60
      wrote on last edited by
      #2

      You claim you are not getting exact result, so show us the code that you have used up to now. Some sample data and your expected results would also be useful

      1 Reply Last reply
      0
      • U User 11119933

        Hello Friends, my requirement is, I have table with following fields EmpID,RequestNo,RequestDate,RequestType,RequestStatus. Now, I need to fetch the top 2 rows of the employee based on the employee ID with request date desc, and should display data of these 2 rows into 1 row with the fields: I have query to fetch only top 2 rows, now I should know how to display that 2 rows into single rows with columns. EmpID Current ReqNo Current ReqDate Current ReqType Current ReqStatus Previous ReqNo Previous ReqDate Previous ReqType Previous ReqStatus Kindly help, I googled and found Pivot, but I am not getting exact result. Thank you so much, Best Regards, Priya.

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        You could create a temp table with the "current" & "previous" columns and insert data into it from the fetched two top records. And do it in a simple stored procedure... :)

        U 1 Reply Last reply
        0
        • U User 11119933

          Hello Friends, my requirement is, I have table with following fields EmpID,RequestNo,RequestDate,RequestType,RequestStatus. Now, I need to fetch the top 2 rows of the employee based on the employee ID with request date desc, and should display data of these 2 rows into 1 row with the fields: I have query to fetch only top 2 rows, now I should know how to display that 2 rows into single rows with columns. EmpID Current ReqNo Current ReqDate Current ReqType Current ReqStatus Previous ReqNo Previous ReqDate Previous ReqType Previous ReqStatus Kindly help, I googled and found Pivot, but I am not getting exact result. Thank you so much, Best Regards, Priya.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          For SQL Server 2012 or later:

          WITH cte As
          (
          SELECT
          ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
          EmpID,
          RequestNo As CurrentReqNo,
          RequestDate As CurrentReqDate,
          RequestType As CurrentReqType,
          RequestStatus As CurrentReqStatus,
          LEAD(RequestNo) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqNo,
          LEAD(RequestDate) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqDate,
          LEAD(RequestType) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqType,
          LEAD(RequestStatus) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqStatus
          FROM
          YourTable
          )
          SELECT
          EmpID,
          CurrentReqNo,
          CurrentReqDate,
          CurrentReqType,
          CurrentReqStatus,
          PreviousReqNo,
          PreviousReqDate,
          PreviousReqType,
          PreviousReqStatus
          FROM
          cte
          WHERE
          RN = 1
          ;

          LEAD (Transact-SQL) - SQL Server | Microsoft Docs[^] For SQL Server 2008 or 2008 R2:

          WITH cte As
          (
          SELECT
          ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
          EmpID,
          RequestNo,
          RequestDate,
          RequestType,
          RequestStatus
          FROM
          YourTable
          )
          SELECT
          C.EmpID,
          C.RequestNo As CurrentReqNo,
          C.RequestDate As CurrentReqDate,
          C.RequestType As CurrentReqType,
          C.RequestStatus As CurrentReqStatus,
          P.RequestNo As PreviousReqNo,
          P.RequestDate As PreviousReqDate,
          P.RequestType As PreviousReqType,
          P.RequestStatus As PreviousReqStatus
          FROM
          cte As C
          LEFT JOIN cte As P
          ON P.EmpID = C.EmpID
          And P.RN = C.RN + 1
          WHERE
          C.RN = 1
          ;


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          U M 2 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            For SQL Server 2012 or later:

            WITH cte As
            (
            SELECT
            ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
            EmpID,
            RequestNo As CurrentReqNo,
            RequestDate As CurrentReqDate,
            RequestType As CurrentReqType,
            RequestStatus As CurrentReqStatus,
            LEAD(RequestNo) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqNo,
            LEAD(RequestDate) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqDate,
            LEAD(RequestType) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqType,
            LEAD(RequestStatus) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqStatus
            FROM
            YourTable
            )
            SELECT
            EmpID,
            CurrentReqNo,
            CurrentReqDate,
            CurrentReqType,
            CurrentReqStatus,
            PreviousReqNo,
            PreviousReqDate,
            PreviousReqType,
            PreviousReqStatus
            FROM
            cte
            WHERE
            RN = 1
            ;

            LEAD (Transact-SQL) - SQL Server | Microsoft Docs[^] For SQL Server 2008 or 2008 R2:

            WITH cte As
            (
            SELECT
            ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
            EmpID,
            RequestNo,
            RequestDate,
            RequestType,
            RequestStatus
            FROM
            YourTable
            )
            SELECT
            C.EmpID,
            C.RequestNo As CurrentReqNo,
            C.RequestDate As CurrentReqDate,
            C.RequestType As CurrentReqType,
            C.RequestStatus As CurrentReqStatus,
            P.RequestNo As PreviousReqNo,
            P.RequestDate As PreviousReqDate,
            P.RequestType As PreviousReqType,
            P.RequestStatus As PreviousReqStatus
            FROM
            cte As C
            LEFT JOIN cte As P
            ON P.EmpID = C.EmpID
            And P.RN = C.RN + 1
            WHERE
            C.RN = 1
            ;


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            U Offline
            U Offline
            User 11119933
            wrote on last edited by
            #5

            Hello Richard, Thanks lot and lot. I was looking on this work for a day, it saved my another day. Thanks lot. Thanks for also sharing the link to learn not only just to copy paste the code shared for me. Best Regards, Priya.

            1 Reply Last reply
            0
            • V Victor Nijegorodov

              You could create a temp table with the "current" & "previous" columns and insert data into it from the fetched two top records. And do it in a simple stored procedure... :)

              U Offline
              U Offline
              User 11119933
              wrote on last edited by
              #6

              Hi Victor, Thank you

              1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                For SQL Server 2012 or later:

                WITH cte As
                (
                SELECT
                ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
                EmpID,
                RequestNo As CurrentReqNo,
                RequestDate As CurrentReqDate,
                RequestType As CurrentReqType,
                RequestStatus As CurrentReqStatus,
                LEAD(RequestNo) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqNo,
                LEAD(RequestDate) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqDate,
                LEAD(RequestType) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqType,
                LEAD(RequestStatus) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqStatus
                FROM
                YourTable
                )
                SELECT
                EmpID,
                CurrentReqNo,
                CurrentReqDate,
                CurrentReqType,
                CurrentReqStatus,
                PreviousReqNo,
                PreviousReqDate,
                PreviousReqType,
                PreviousReqStatus
                FROM
                cte
                WHERE
                RN = 1
                ;

                LEAD (Transact-SQL) - SQL Server | Microsoft Docs[^] For SQL Server 2008 or 2008 R2:

                WITH cte As
                (
                SELECT
                ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
                EmpID,
                RequestNo,
                RequestDate,
                RequestType,
                RequestStatus
                FROM
                YourTable
                )
                SELECT
                C.EmpID,
                C.RequestNo As CurrentReqNo,
                C.RequestDate As CurrentReqDate,
                C.RequestType As CurrentReqType,
                C.RequestStatus As CurrentReqStatus,
                P.RequestNo As PreviousReqNo,
                P.RequestDate As PreviousReqDate,
                P.RequestType As PreviousReqType,
                P.RequestStatus As PreviousReqStatus
                FROM
                cte As C
                LEFT JOIN cte As P
                ON P.EmpID = C.EmpID
                And P.RN = C.RN + 1
                WHERE
                C.RN = 1
                ;


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                The CTE king strikes again ;P

                Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups