title: SQL Server: Merge two rows of same table
body: <p>I'm trying to merge the rows from a table in below manner.</p>
  
  <p>The table contains logs about the Request, Response, RequestTime and ResponseTime made to an API.</p>
  
  <p><a href="https://i.stack.imgur.com/Qn8Lh.jpg" rel="nofollow noreferrer">Please find the attached image here.</a></p>
  
  <p>Both Request and Response columns in the desired output are both populated from Message column of base table.</p>
  
  <p>Similarly, the RequestTime and ResponseTime columns in the desired output are both populated from Date column of base table.</p>
  
  <p>I have tried the below code and it is getting me desired output but I don't think this is the right way to do it.</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[GetPackageRequestResponseDetails]
      @PACKAGEID VARCHAR(20)  
  AS
  BEGIN
      SET NOCOUNT ON;
  
      DECLARE @ApiLogDump TABLE
      (
          Date DATETIME,
          Thread VARCHAR(255),
          Logger VARCHAR(255),
          Message VARCHAR(4000)
      )
  
      INSERT INTO @ApiLogDump
      (
          Date,
          Thread,
          Logger,
          Message
      )
      SELECT
          Date,
          Thread,
          Logger,
          Message
      FROM APILog
      WHERE Logger LIKE '%GetPackageDetails%'
      AND Message LIKE '%'+@PACKAGEID+'%'
  
      DECLARE  @ApiLogMerge TABLE
      (
          Thread VARCHAR(255),
          Request VARCHAR(4000),
          Response VARCHAR(4000),
          RequestTime DATETIME,
          ResponseTime DATETIME
      )
  
      INSERT INTO @ApiLogMerge
      (   
          Thread,
          Request,
          Response,
          RequestTime,
          ResponseTime
      )
      SELECT 
          Thread,
          Message,
          Null,
          Date,
          Null
      FROM @ApiLogDump
      WHERE Message LIKE '%Request:'+@PACKAGEID+'%'
  
  
      UPDATE @ApiLogMerge
          SET Response = A.Message,
              ResponseTime = A.Date
      FROM @ApiLogMerge AP JOIN @ApiLogDump A
         ON AP.Thread = A.Thread
      WHERE Message LIKE '%Response:{"PackageID":"'+@PACKAGEID+'"%'
  
      SELECT * FROM @ApiLogMerge ORDER BY RequestTime DESC
  
  END
  </code></pre>
  
  <p>Any help would be highly appreciated.</p>