Solved

Limit SQL Query results

Posted on 2010-11-09
2
357 Views
Last Modified: 2012-08-13
This SQL query returns 5 records:

select FULLNAME,L_LAXON
from sde.HILLSBOROUGH_POLYLINE
where fullname like '% VARN%' or FULLNAME like 'VARN%'
GROUP BY FULLNAME,L_LAXON

I also want the ObjectID field returned but when I include it in the Select and Group By it increases the number of records returned to 22 and now I have duplicates of the FULLNAME and L_LAXON data.
How can I query for this information and get want I want, which is the 5 records including the OjbectID field.  Thanks!
0
Comment
Question by:MEINMEL
2 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 34096858
two options

select FULLNAME,L_LAXON, max(ObjectID)
from sde.HILLSBOROUGH_POLYLINE
where fullname like '% VARN%' or FULLNAME like 'VARN%'
GROUP BY FULLNAME,L_LAXON

or this other one

select FULLNAME,L_LAXON, ObjectID
from (
 select FULLNAME,L_LAXON, ObjectID, row_number() over (partition by FullName, L_LAXON order by FULLNAME, L_LAXON) rn
 from sde.HILLSBOROUGH_POLYLINE
 where fullname like '% VARN%' or FULLNAME like 'VARN%'
) a
where rn = 1
 
0
 

Author Closing Comment

by:MEINMEL
ID: 34096902
Thank you so much, especially for the fast and accurate response!
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question