Solved

help needed with Microsoft SQL Server syntax

Posted on 2011-03-14
4
202 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Syntax 17 43
sql server query 12 25
Query for timesheet application 3 17
Need return values from a stored procedure 8 19
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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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