April 10, 2012 at 9:42 am
Hi,
I have an table called type.
SeqAttributeTypeTypeid
15 10
21 10
33 10
15 11
24 11
15 12
16 13
13 14
26 14
How to get the Seq Column value like above table structure.
If typeid field having the same value 1 time then seq field value should be 1
If typeid field having the same value above 1 time then seq field value should be 1+1...
Can you help me on this., how to get this in single query.
Thanks,
Antony
April 10, 2012 at 9:50 am
tonyarp05 61903 (4/10/2012)
Hi,I have an table called type.
SeqAttributeTypeTypeid
15 10
21 10
33 10
15 11
24 11
15 12
16 13
13 14
26 14
How to get the Seq Column value like above table structure.
If typeid field having the same value 1 time then seq field value should be 1
If typeid field having the same value above 1 time then seq field value should be 1+1...
Can you help me on this., how to get this in single query.
Thanks,
Antony
SELECT
ROW_NUMBER() OVER (PARTITION BY Typeid ORDER BY (SELECT NULL)) -
DENSE_RANK() OVER (PARTITION BY Typeid ORDER BY (SELECT NULL))+1 AS Seq,
AttributeType, Typeid
FROM #yourTable;
April 10, 2012 at 10:53 am
Note: while cadavre's code will generate a sequence number within the query run, it's not a deterministic sequence number. As a result, rows might get different sequence numbers within the typeID on different runs of the query.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
April 10, 2012 at 3:53 pm
Matt Miller (#4) (4/10/2012)
Note: while cadavre's code will generate a sequence number within the query run, it's not a deterministic sequence number. As a result, rows might get different sequence numbers within the typeID on different runs of the query.
Yep. I wrote the code above on my phone whilst walking to my car on my way home otherwise I would've stressed this point.
There needs to be a way of ordering the results to determine your sequence numbers. I've not included an order so the results are not guaranteed to be the same each time as SQL Server doesn't guarantee table ordering without an order by clause.
Take a look at this article for more information on SQL ordering.
April 10, 2012 at 4:25 pm
Cadavre (4/10/2012)
Matt Miller (#4) (4/10/2012)
Note: while cadavre's code will generate a sequence number within the query run, it's not a deterministic sequence number. As a result, rows might get different sequence numbers within the typeID on different runs of the query.Yep. I wrote the code above on my phone whilst walking to my car on my way home otherwise I would've stressed this point.
There needs to be a way of ordering the results to determine your sequence numbers. I've not included an order so the results are not guaranteed to be the same each time as SQL Server doesn't guarantee table ordering without an order by clause.
Take a look at this article for more information on SQL ordering.
Whoa!!! Good find on that link! Thanks!
--Jeff Moden
Change is inevitable... Change for the better is not.
April 10, 2012 at 5:58 pm
Jeff Moden (4/10/2012)
Cadavre (4/10/2012)
Matt Miller (#4) (4/10/2012)
Note: while cadavre's code will generate a sequence number within the query run, it's not a deterministic sequence number. As a result, rows might get different sequence numbers within the typeID on different runs of the query.Yep. I wrote the code above on my phone whilst walking to my car on my way home otherwise I would've stressed this point.
There needs to be a way of ordering the results to determine your sequence numbers. I've not included an order so the results are not guaranteed to be the same each time as SQL Server doesn't guarantee table ordering without an order by clause.
Take a look at this article for more information on SQL ordering.
Whoa!!! Good find on that link! Thanks!
It came from Gail. I asked for evidence to help me to explain ordering in a different thread earlier on and she produced that gem.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply