title: Return only first match with Inner Join
body: <p>I have this query below and it works. However, it takes over 30 mins to run. Essentially, the sku field is unique, but the tables it is joining with have multiple values that correspond to the entity_id. I only want to return the first value it finds.  I understand that the way I am doing it now works, but it is not efficient at all.  Any tips would be greatly appreciated.</p>
  
  <p>Sorry for the weird code sample. Couldn't get it to format under the code option.</p>
  
  <p>Thanks!</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>SELECT
  FROM
  (
   SELECT DISTINCT substring(sku, 1, 
   (
   SELECT CASE
    WHEN charindex('-', sku)-1 = -1
    THEN 0
    ELSE charindex('-', sku)-1
   END
   )) AS sku, 
   CAST(cpet.value AS varchar(max)) AS 'DESCRIPTION', cpev.value AS 'URL_KEY', CONCAT('https://azureedge.net/catalog/product', cpemg.value) AS 'image', ROW_NUMBER() OVER(PARTITION BY sku ORDER BY sku) AS RowNumber
   FROM imp_mg.CATALOG_Product_Entity cpe
   INNER JOIN 
   imp_mg.CATALOG_Product_Entity_Text cpet
   ON cpe.entity_id = cpet.entity_id
   INNER JOIN 
   imp_mg.CATALOG_Product_Entity_Varchar cpev
   ON cpe.entity_id = cpev.entity_id
   INNER JOIN 
   imp_mg.CATALOG_Product_Entity_Media_Gallery cpemg
   ON cpe.entity_id = cpemg.entity_id
  ) AS a
  WHERE a.RowNumber = 1</code></pre>
  </div>
  </div>
  </p>