Solved

help needed with Microsoft SQL Server syntax

Posted on 2011-03-14
4
204 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 166 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 167 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 167 total points
ID: 35139357
See attached.
query.txt
0

Featured Post

Independent Software Vendors: 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…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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