MikeMCSD
asked on
How to Select groups of rows by 60,000 from table
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The above is my preferred methond, but there are more alternatives like the ones in the question I've participated.
https://www.experts-exchange.com/questions/24294001/Select-10-to-25-Percent.html
https://www.experts-exchange.com/questions/24294001/Select-10-to-25-Percent.html
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
1 = first 60K pages
2 = 60001 - 120000
etc
2 = 60001 - 120000
etc
Im a goof...you're right.
Article I wrote on the subject...years ago
http://articles.techrepublic.com.com/5100-10878_11-6141785.html#
http://articles.techrepublic.com.com/5100-10878_11-6141785.html#
ASKER
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
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
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
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
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
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
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
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
ASKER
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
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
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
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
ASKER
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 . .
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 . .
ASKER
worked, thanks . . how do I get the next 2 60,000 batches?
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.
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.
Hi all,
Just quick correction.. again :)
Added a -1 so that the first batch returns 60000, not 59999
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
ASKER
thanks all
Open in new window