473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

selecting top 10 entries from multiple groups

Hi,
I'm trying to select the top 10 entries from a table per a variable. At present the below query brings up only 10 entries but I want 10 entries per segmentcode.



SELECT top 10 * FROM t_security WHERE segmentcode IN(

'AIM','AIM3','AIMI','AMSM','ASQ1','ASQ2','ASX1','A SX2','ASXN','CIBB','CNVE','CRNR','CRTR','CSEQ','CS ET','CSQX',
'CWNR','CWNU','CWTR','CWTU','EQS','ETCS','ETF2','E TFS','GILT','INSD','IOB','IOBU','IRSQ','IRSU','ITB B','ITBU',
'ITR','LVSD','MIBB','MISC','MISL','MSEQ','MSET','M SQX','MSTM','NSTS','ODTT','PSNR','PSTR','RSET','RS TM','SET1',
'SET2','SET3','SSMM','SSMU','SSQ3','SSX3','SSX4',' STBS','STMM','TC1E','TC1M','TC1O','TCAE','TCAM','T CAO','TEST',
'XIBB','XSEQ','XSET','XSTM','ZA01','ZA02','ZA03',' ZA04','ZA11','ZA21','ZA22','ZA24'
)
Sep 13 '07 #1
1 1574
almaz
168 Expert 100+
Hi,
I'm trying to select the top 10 entries from a table per a variable. At present the below query brings up only 10 entries but I want 10 entries per segmentcode.
SQL Server 2005 solution (don't forget to replace YOUR_FIELDS_HERE with correct field list, ORDER_FIELD with correct field name):
Expand|Select|Wrap|Line Numbers
  1. ;with Ordered(YOUR_FIELDS_HERE, Position) as
  2. (
  3.     select YOUR_FIELDS_HERE, Position = row_number() over(partition by segmentcode order by ORDER_FIELD)
  4.     from t_security
  5.     where segmentcode in(
  6.         'AIM','AIM3','AIMI','AMSM','ASQ1','ASQ2','ASX1','A  SX2','ASXN','CIBB','CNVE','CRNR','CRTR','CSEQ','CS  ET','CSQX',
  7.         'CWNR','CWNU','CWTR','CWTU','EQS','ETCS','ETF2','E  TFS','GILT','INSD','IOB','IOBU','IRSQ','IRSU','ITB  B','ITBU',
  8.         'ITR','LVSD','MIBB','MISC','MISL','MSEQ','MSET','M  SQX','MSTM','NSTS','ODTT','PSNR','PSTR','RSET','RS  TM','SET1',
  9.         'SET2','SET3','SSMM','SSMU','SSQ3','SSX3','SSX4','  STBS','STMM','TC1E','TC1M','TC1O','TCAE','TCAM','T  CAO','TEST',
  10.         'XIBB','XSEQ','XSET','XSTM','ZA01','ZA02','ZA03','  ZA04','ZA11','ZA21','ZA22','ZA24'
  11.     )
  12. )
  13. select YOUR_FIELDS_HERE 
  14. from Ordered
  15. where Position<=10
Sep 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Nathan Sokalski | last post by:
When testing a form of mine which uses RequiredFieldValidators that have the Display property set to "Dynamic" the ErrorMessage property is automatically removed when an entry is completely typed...
1
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been...
7
by: Jed Parsons | last post by:
Hi, I'm using the logging module for the first time. I'm using it from within Zope Extensions. My problem is that, for every event logged, the logger is producing multiple identical entries...
3
by: Bernard Lebel | last post by:
Hello, Is there an option or a way to allow the selection of multiple entries in the Listbox widget? I could not find any, and would like to allow the end user to select multiple entries. ...
2
by: Henrik Goldman | last post by:
Hi there, I'm new to sql and thus I'm having problems with a specific query which I hope you guys can help me with. Basicly I have a few tables which I'm trying to do a query on: Table...
1
by: michael.martinek | last post by:
Greetings! I've recently been trying to do something, which apparently looks like it may be a little odd.. I'm not finding anything in the manuals or anywhere on the web where something similiar...
9
by: Generic Usenet Account | last post by:
I had a need to randomly select an element from an STL collection. It does not appear that this functionality is provided out-of-the-box with STL. Here is my crude implementation. I am using...
1
by: c35tar1 | last post by:
I have a ListView within a dialog. In code the groups are created and items added. Display is 100% correct. The problem is with using Shift to select multiple items in the list. - If you...
2
maxamis4
by: maxamis4 | last post by:
Hello folks need some ideas here for a report that I am getting together. Here is the scenerio, currently we have an SLA that states that an external group transferring a problem to the internal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.