Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2004-10-27 : 02:32:09
|
| I have this data...ID - NAME - POSITION----------------------------------------1 - A - aa2 - B - bb3 - C - cc4 - D - dd5 - E - ee6 - F - ff7 - F - ff8 - F - ff9 - B - bb10 - B - bb11 - B - bb12 - A -aaIs there a way to querry the top 1 multiple by 3 of records?jUST an example:SYNTAX: TOPMULTIPLEOF(multiplenumber,top)EX. 1SELECT TOPMULTIPLEOF(3,1) * from tblAida ORDER BY IDResult:ID NAME POSITION----------------------------------------1 A aa2 B bb3 C cc***************************************************************EX. 2 SELECT TOPMULTIPLEOF(3,2) * from tblAida ORDER BY IDResult:ID NAME POSITION----------------------------------------4 D dd5 E ee6 F ff***************************************************************EX. 3 SELECT TOPMULTIPLEOF(5,1) * from tblAida ORDER BY IDResult:ID NAME POSITION----------------------------------------1 A aa2 B bb3 C cc4 D dd5 E ee***************************************************************EX. 4 SELECT TOPMULTIPLEOF(5,2) * from tblAida ORDER BY IDResult:ID NAME POSITION----------------------------------------6 F ff7 F ff8 F ff9 B bb10 B bb***************************************************************Want Philippines to become 1st World COuntry? Go for World War 3... |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-10-27 : 04:03:15
|
| [code]set nocount onset rowcount 0create table #t(ID int not null, NAME char(1) not null, POSITION char(2) not null)insert #t(ID,NAME,POSITION)select 1,'A','aa' union allselect 2,'B','bb' union allselect 3,'C','cc' union allselect 4,'D','dd' union allselect 5,'E','ee' union allselect 6,'F','ff' union allselect 7,'F','ff' union allselect 8,'F','ff' union allselect 9,'B','bb' union allselect 10,'B','bb' union allselect 11,'B','bb' union allselect 12,'A','aa'-- declare variables as of-- pseudocode function "TOPMULTIPLEOF(multiplenumber,top)"declare @multiplenumber int, @top intselect @multiplenumber = 5, @top = 2/***************** One Way ******************/declare @ID int, @rowcount intset @rowcount = ( @multiplenumber * ( @top - 1 ) + 1 )set rowcount @rowcountselect @ID = ID from #t order by IDset rowcount @multiplenumberselect ID,NAME,POSITION from #t where ID >= @id order by ID/***************** Another Way ******************/set rowcount 0declare @sql nvarchar(512)set @sql = 'select a.ID,a.NAME,a.POSITION from( select top ' + ltrim(@multiplenumber*@top) + ' ID,NAME,POSITION from #t order by id ) aleft join( select top ' + ltrim(@multiplenumber*(@top-1)) + ' ID from #t order by id ) bon a.ID = b.IDwhere b.ID is null'-- print @sqlexec sp_executesql @sqldrop table #t[/code]rockmoose |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2004-10-27 : 05:17:01
|
thanks rockmoose...Your first way is much easy to understand because its short. The shorter the qeurry the better.But as i try to execute the function TOPMULTIPLEOF(5,2)? OK the result is satisfy.But as i want to querry and show all the record by "SELECT * FROM tblAida"? it givesme the result of the function TOPMULTIPLEOF I recently executed and that confuses us. lolIt was later that i found out that you forgot to reset the rowcount to zero on the last part of the querry. By the way thanks. Do mo Arigato Guisaimasu.Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
|
|
|
|
|