Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Jul 6, 2000

Multiple Entries That Meet Criteria

Question:
How can I pull just the first match to a query? We have a database of jobs in process:
jobno 12345
op# 10 shear %complete 100
op# 20 punch % complete 90
op# 30 deburr %complete 0
op# 40 timesave %complete 0
jobno 67890
op# 10 shear %complete 100
op# 20 punch % complete 100
op# 30 deburr %complete 90
op# 40 timesave %complete 0
I want my query to pull just the first occurrence of WHERE %complete < 100. Right now, my results return op#20, 30, and 40 for jobno 12345 and op#30 and 40 for jobno 67890. I want the results to be just op#20 for jobno 12345 and op#30 for jobno 67890.

Answer:
For a table called TestTable with the columns JobNo, OP, and PercentComplete and the above data, this query returns the JobNo and the maximum PercentComplete entry where the PercentComplete is less than 100:

SELECT JobNo,  MAX(PercentComplete) AS PercentComplete 
FROM TestTable
WHERE PercentComplete < 100
GROUP BY JobNo
Returns this data set:
JobNo      PercentComplete 
---------- --------------- 
12345      90
67890      90
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date