Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>On the basis of what is seen, a generic SQL query like this would "flatten" those rows into a single row (but the 2 PgsCompleted values need different column names).</p> <pre><code> select AutoArtId , empArtStage , MIN(ArtStageCurrStat) as ArtStageCurrStat , MIN(PgsCompleted) as PgsCompleted1 , MAX(PgsCompleted) as PgsCompleted2 from YourTable group by AutoArtId, empArtStage </code></pre> <p>Some questions emerge aboout more or less than 2 rows, this may help:</p> <pre><code> select AutoArtId , empArtStage , MIN(ArtStageCurrStat) as ArtStageCurrStat , MIN(PgsCompleted) as PgsCompleted1 , MAX(PgsCompleted) as PgsCompleted2 , count(*) as num_of from YourTable group by AutoArtId, empArtStage having count(*) &lt;&gt; 2 </code></pre> <p>Here is an approach that uses row_number() which is available in SQL 2008. It assumes you have some field(s) in the table that will allow distinction of oldest and newest (here I have used an autonumbered ID) - AND the assumption that it is the oldest and newest that are the relevant record pairs. In <strong><a href="http://sqlfiddle.com/#!3/0a1ef/1" rel="nofollow">this sqlfiddle</a></strong> I dummied up some sample data representing one group of 3 records and another of a single record.</p> <pre><code>SELECT AutoArtId , empArtStage , MIN(ArtStageCurrStat) AS ArtStageCurrStat , MIN(PgsCompleted) AS PgsCompleted1 , MAX(PgsCompleted) AS PgsCompleted2 FROM ( SELECT AutoArtId , empArtStage , ArtStageCurrStat , PgsCompleted , row_number() over (partition BY AutoArtId, empArtStage ORDER BY ID ASC) AS oldest , row_number() over (partition BY AutoArtId, empArtStage ORDER BY ID DESC) AS newest FROM YourTable ) AS derived WHERE oldest = 1 OR newest = 1 GROUP BY AutoArtId , empArtStage </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload