0

I have the following select that need to be done:

select top 1 a, b, c, d from Products where
CodDep = 10 or
CodDep = 11 or
CodDep = 12 or CodDep = 13 or
CodDep = 26 or
CodDep = 27 or CodDep = 32 or
CodDep = 34 or
CodDep = 248442 or
CodDep = 259741 order by LastUpdate

Is there an easy way to do this without all this repetition?

Bruno 'Shady'
  • 4,028
  • 11
  • 52
  • 73

3 Answers3

5
select top 1 
  a, b, c, d 
from 
  Products 
where
  CodDep IN (10,12,12,13,26,27,32,34,248442,259741)
order by 
  LastUpdate
Jake Feasel
  • 16,359
  • 5
  • 50
  • 65
0

Try this query:

SELECT TOP 1 a, b, c, d FROM Products WHERE
CodDep IN (10,11,12,13,26,27,32,34,248442,259741) ORDER BY LastUpdate 
Somnath Muluk
  • 51,453
  • 32
  • 215
  • 222
0

SQL has but one data structure: the table. Put the values in a table and semi join to that table e.g.

WITH Params
     AS 
     (
      SELECT * 
        FROM (
              VALUES (10), (11), (12), (13),
                     (26), (27), (32), (34), 
                     (248442), (259741)
             ) AS T (CodDep)
     )
SELECT TOP 1 a, b, c, d 
  FROM Products 
 WHERE CodDep IN (SELECT CodDep FROM Params)
 ORDER 
    BY LastUpdate;
Community
  • 1
  • 1
onedaywhen
  • 53,058
  • 12
  • 94
  • 134