?
Solved

help needed with Microsoft SQL Server syntax

Posted on 2011-03-14
4
Medium Priority
?
206 Views
Last Modified: 2012-05-11
select 5 records from customers table for each city, so i guess something like select top 5 customerid from tablename group by city?  i would hope to return:

NY, CUSTOMERID1
NY, CUSTOMERID2
NY, CUSTOMERID3
NY, CUSTOMERID4
NY, CUSTOMERID5
LON, CUSTOMERID1
LON, CUSTOMERID2
LON, CUSTOMERID3
LON, CUSTOMERID4
LON, CUSTOMERID5

0
Comment
Question by:Jimmy_inc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 19

Assisted Solution

by:Bhavesh Shah
Bhavesh Shah earned 664 total points
ID: 35135453
Hi,

Hope this is not homework. :-)


SELECT * FROM
(
SELECT Country, CustomerName, ROW_NUMBER() OVER(PARTITION BY Country ORDER BY Country DESC) AS 'Row'
from table1
)
WHERE Row <= 5

0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 668 total points
ID: 35135471
try this


 select customerid , City from Customers
where (
   select count(*) from Customers as f
   where f.City  = Customers.City
) <= 5
0
 

Author Comment

by:Jimmy_inc
ID: 35135518
Ha ha - thanks for the humour Brichsoft

I will try these
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 668 total points
ID: 35139357
See attached.
query.txt
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 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