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
Solved

How to Select groups of rows by 60,000 from table

Posted on 2010-09-14
20
447 Views
Last Modified: 2012-06-21
I have a table with 170,597 rows in it.
I need to Select the first 60,000 then the second 60,000, etc..
But I have no Identity column I can use for a count.
How else can I do this?  thanks   SQL Server 2005

SELECT Customer.Email, Customer.LName ......
FROM  Customer
where ???
order by Customer.LName

0
Comment
Question by:MikeMCSD
  • 9
  • 5
  • 4
  • +1
20 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 200 total points
ID: 33676205
One possibility is using EXCEPT

declare @endrow int
declare @startrow int
set @endrow = 120000
set @startrow = 60001

select * from (select top(@endrow) * from Customer order by LName) t1
except
select * from (select top@(startrow) * from Customer order by LName) t2

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 33676213
oops, typo there
declare @endrow int
declare @startrow int
set @endrow = 120000
set @startrow = 60001

select * from (select top(@endrow) * from Customer order by LName) t1
except
select * from (select top(@startrow) * from Customer order by LName) t2

Open in new window

0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 200 total points
ID: 33676245
no problem:

select * from (
SELECT rowno = (row_number() over(order by Customer.Email)%60000) + 1, Customer.Email, Customer.LName ......
FROM  Customer
)
where rowno = 1
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 41

Expert Comment

by:ralmada
ID: 33676250
The above is my preferred methond, but there are more alternatives like the ones in the question I've participated.
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_24294001.html 
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 100 total points
ID: 33676317
Use row_number like chapmandew's, but don't use %, use / in the same place. Rowno 1 gives first 60k, 2 is next etc
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33676350
yep, thats why I did it.  could partition it into "pages" so you don't have to go through and do the math each time for it.  
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33676355
1 = first 60K pages
2 = 60001 - 120000

etc
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33676380
Im a goof...you're right.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33676402
Article I wrote on the subject...years ago

http://articles.techrepublic.com.com/5100-10878_11-6141785.html#
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 33676508
thanks guys, ..  not sure how to apply it,
got an error when I tried this:

select * from (
SELECT rowno = (row_number() over(order by Customer.Email)%60000) + 1, Customer.Email, Customer.LName as 'Last Name', Customer.FName as 'First Name',
Customer.Address1 as 'Address', Customer.City,
Customer.Region, Customer.PostalCode, Customer.Country, Product.Name as 'Product Name', Orders.OrderID
FROM Customer INNER JOIN
Orders ON Customer.CustomerID = Orders.CustomerID INNER JOIN
OrderDetail ON Orders.OrderID = OrderDetail.OrderID INNER JOIN
Product ON OrderDetail.ProductID = Product.ProductId
where CustomerID between 1088 and 61088
order by Customer.LName
)
where rowno = 1
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33676534
select * from (
SELECT rowno = (row_number() over(order by Customer.LName)/60000) + 1, Customer.Email, Customer.LName as 'Last Name', Customer.FName as 'First Name',
Customer.Address1 as 'Address', Customer.City,
Customer.Region, Customer.PostalCode, Customer.Country, Product.Name as 'Product Name', Orders.OrderID
FROM Customer INNER JOIN
Orders ON Customer.CustomerID = Orders.CustomerID INNER JOIN
OrderDetail ON Orders.OrderID = OrderDetail.OrderID INNER JOIN
Product ON OrderDetail.ProductID = Product.ProductId
where CustomerID between 1088 and 61088

) a
where rowno = 1
order by Customer.LName
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33676537
select * from (
SELECT rowno = (row_number() over(order by Customer.LName)/60000) + 1, Customer.Email, Customer.LName as 'Last Name', Customer.FName as 'First Name',
Customer.Address1 as 'Address', Customer.City,
Customer.Region, Customer.PostalCode, Customer.Country, Product.Name as 'Product Name', Orders.OrderID
FROM Customer INNER JOIN
Orders ON Customer.CustomerID = Orders.CustomerID INNER JOIN
OrderDetail ON Orders.OrderID = OrderDetail.OrderID INNER JOIN
Product ON OrderDetail.ProductID = Product.ProductId
where CustomerID between 1088 and 61088

) a
where rowno = 1
order by a.LName
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33676544
crap:

select * from (
SELECT rowno = (row_number() over(order by Customer.LName)/60000) + 1, Customer.Email, Customer.LName as 'LastName', Customer.FName as 'First Name',
Customer.Address1 as 'Address', Customer.City,
Customer.Region, Customer.PostalCode, Customer.Country, Product.Name as 'Product Name', Orders.OrderID
FROM Customer INNER JOIN
Orders ON Customer.CustomerID = Orders.CustomerID INNER JOIN
OrderDetail ON Orders.OrderID = OrderDetail.OrderID INNER JOIN
Product ON OrderDetail.ProductID = Product.ProductId
where CustomerID between 1088 and 61088

) a
where rowno = 1
order by a.LastName
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 33676625
chap, I got :
Msg 209, Level 16, State 1, Line 9
Ambiguous column name 'CustomerID'.
Msg 209, Level 16, State 1, Line 9
Ambiguous column name 'CustomerID'.
for the last one you posted
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33676637
select * from (
SELECT rowno = (row_number() over(order by Customer.LName)/60000) + 1, Customer.Email, Customer.LName as 'LastName', Customer.FName as 'First Name',
Customer.Address1 as 'Address', Customer.City,
Customer.Region, Customer.PostalCode, Customer.Country, Product.Name as 'Product Name', Orders.OrderID
FROM Customer INNER JOIN
Orders ON Customer.CustomerID = Orders.CustomerID INNER JOIN
OrderDetail ON Orders.OrderID = OrderDetail.OrderID INNER JOIN
Product ON OrderDetail.ProductID = Product.ProductId
--where CustomerID between 1088 and 61088

) a
where rowno = 1
order by a.LastName
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 33676665
I added :
where Customer.CustomerID between 1088 and 61088

Actually the above line should not have been there when I first posted
and tried the query, . . I was just trying something else at the time . .
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 33676676
worked, thanks . . how do I get the next 2 60,000 batches?
0
 
LVL 41

Expert Comment

by:ralmada
ID: 33676701
They already told you
where rowno = 2 will get second batch
If you are still interested you can also take a look at my approach and compare the speed.
 
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33676733
Hi all,

Just quick correction.. again :)
Added a -1 so that the first batch returns 60000, not 59999
select * from (
SELECT rowno = (row_number() over(order by Customer.LName)-1)/60000 + 1, Customer.Email, Customer.LName as 'LastName', Customer.FName as 'First Name',
Customer.Address1 as 'Address', Customer.City,
Customer.Region, Customer.PostalCode, Customer.Country, Product.Name as 'Product Name', Orders.OrderID
FROM Customer INNER JOIN
Orders ON Customer.CustomerID = Orders.CustomerID INNER JOIN
OrderDetail ON Orders.OrderID = OrderDetail.OrderID INNER JOIN
Product ON OrderDetail.ProductID = Product.ProductId
--where CustomerID between 1088 and 61088

) a
where rowno = 1
order by a.LastName

Open in new window

0
 
LVL 16

Author Closing Comment

by:MikeMCSD
ID: 33677001
thanks all
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

829 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