January 28, 2015 at 3:29 am
A simple Query to select alternate rows from a table ?
January 28, 2015 at 3:46 am
satishkmr538 (1/28/2015)
A simple Query to select alternate rows from a table ?
Use ROW_NUMBER() and select either even or odd numbers.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 28, 2015 at 3:50 am
Check this sample query without using % or mod
USE [AdventureWorks2008]
GO
SELECT P.*
FROM
(SELECT *,ROW_NUMBER() OVER(Order by BusinessEntityId) AS RID FROM Person.Person )P
INNER JOIN (SELECT ROW_NUMBER() OVER(Order by BusinessEntityId) * 2 As Rid from Person.Person) P2
ON P.RID = P2.RId
January 28, 2015 at 4:03 am
Bhushan Kulkarni (1/28/2015)
Check this sample query without using % or mod
USE [AdventureWorks2008]
GO
SELECT P.*
FROM
(SELECT *,ROW_NUMBER() OVER(Order by BusinessEntityId) AS RID FROM Person.Person )P
INNER JOIN (SELECT ROW_NUMBER() OVER(Order by BusinessEntityId) * 2 As Rid from Person.Person) P2
ON P.RID = P2.RId
You may wish to consider warning the OP if you are posting a deliberately expensive and convoluted query "just for laughs", it isn't always as obvious as it may seem. It's good, though. I think it would be tricky to make it any more expensive without adding considerably to the code.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 28, 2015 at 4:17 am
Optimized one..
USE [AdventureWorks2008]
GO
SELECT P.*
FROM
Person.Person P
WHERE
P.BusinessEntityId & 1 = 0
January 28, 2015 at 4:23 am
Bhushan Kulkarni (1/28/2015)
Optimized one..
USE [AdventureWorks2008]
GO
SELECT P.*
FROM
Person.Person P
WHERE
P.BusinessEntityId & 1 = 0
Almost...
SELECT n, n%1, n%2
FROM (VALUES (1),(2),(3),(4),(5)) d (n)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 28, 2015 at 4:51 am
Bhushan Kulkarni (1/28/2015)
Optimized one..
USE [AdventureWorks2008]
GO
SELECT P.*
FROM
Person.Person P
WHERE
P.BusinessEntityId & 1 = 0
That doesn't return alternate rows, it returns rows with odd even values of BusinessEntityID. There's a fairly large difference.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 28, 2015 at 4:56 am
GilaMonster (1/28/2015)
Bhushan Kulkarni (1/28/2015)
Optimized one..
USE [AdventureWorks2008]
GO
SELECT P.*
FROM
Person.Person P
WHERE
P.BusinessEntityId & 1 = 0
That doesn't return alternate rows, it returns rows with odd values of BusinessEntityID. There's a fairly large difference.
It returns every row:
SELECT
n,
[AllZero] = n%1,
[(n-1)%2] = (n-1)%2,
[(n-1)%3] = (n-1)%3,
[(n-1)/2] = (n-1)/2,
[(n-1)/3] = (n-1)/3
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8)) d (n)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 28, 2015 at 4:59 am
ChrisM@Work (1/28/2015)
GilaMonster (1/28/2015)
Bhushan Kulkarni (1/28/2015)
Optimized one..
USE [AdventureWorks2008]
GO
SELECT P.*
FROM
Person.Person P
WHERE
P.BusinessEntityId & 1 = 0
That doesn't return alternate rows, it returns rows with odd values of BusinessEntityID. There's a fairly large difference.
It returns every row:
SELECT
n,
[AllZero] = n%1,
[(n-1)%2] = (n-1)%2,
[(n-1)%3] = (n-1)%3,
[(n-1)/2] = (n-1)/2,
[(n-1)/3] = (n-1)/3
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8)) d (n)
It's a bitwise-and not modulo
SELECT
n,
[BitwiseAnd] = n&1, --***
[AllZero] = n%1,
[(n-1)%2] = (n-1)%2,
[(n-1)%3] = (n-1)%3,
[(n-1)/2] = (n-1)/2,
[(n-1)/3] = (n-1)/3
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8)) d (n)
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537January 28, 2015 at 5:03 am
Mark Cowne (1/28/2015)
ChrisM@Work (1/28/2015)
GilaMonster (1/28/2015)
Bhushan Kulkarni (1/28/2015)
Optimized one..
USE [AdventureWorks2008]
GO
SELECT P.*
FROM
Person.Person P
WHERE
P.BusinessEntityId & 1 = 0
That doesn't return alternate rows, it returns rows with odd values of BusinessEntityID. There's a fairly large difference.
It returns every row:
SELECT
n,
[AllZero] = n%1,
[(n-1)%2] = (n-1)%2,
[(n-1)%3] = (n-1)%3,
[(n-1)/2] = (n-1)/2,
[(n-1)/3] = (n-1)/3
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8)) d (n)
It's a bitwise-and not modulo
SELECT
n,
[BitwiseAnd] = n&1, --***
[AllZero] = n%1,
[(n-1)%2] = (n-1)%2,
[(n-1)%3] = (n-1)%3,
[(n-1)/2] = (n-1)/2,
[(n-1)/3] = (n-1)/3
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8)) d (n)
D'oh! Thanks Mark.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 28, 2015 at 5:06 am
ChrisM@Work (1/28/2015)
It returns every row:
SELECT
n,
[AllZero] = n%1,
[(n-1)%2] = (n-1)%2,
[(n-1)%3] = (n-1)%3,
[(n-1)/2] = (n-1)/2,
[(n-1)/3] = (n-1)/3
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8)) d (n)
No, it doesn't. It's a Bitwise AND, true only when the ID is even (not odd as I previously said)
SELECT
n,
n & 1
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8)) d (n)
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 29, 2015 at 5:15 pm
Bhushan Kulkarni (1/28/2015)
Optimized one..
USE [AdventureWorks2008]
GO
SELECT P.*
FROM
Person.Person P
WHERE
P.BusinessEntityId & 1 = 0
Interesting but it is not guaranteed to return alternate rows if there are gaps in BusinessEntityID.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply