SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


RANK ISSUE


RANK ISSUE

Author
Message
Snargables
Snargables
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1896 Visits: 839
NOTICE THE DESIRED OUTPUT ON THE BOTTOM.


SELECT 'table1' TABLENAME
,'col1' COLNAME
INTO #temp
UNION ALL
SELECT 'table1'
,'col2'
UNION ALL
SELECT 'table1'
,'col3'
UNION ALL
SELECT 'table2'
,'col1'
UNION ALL
SELECT 'table2'
,'col2'
UNION ALL
SELECT 'table3'
,'col1'


SELECT *
FROM #temp

--DESIRED OUTPUT ID COL:

--ID TABLENAME COLNAME
--1 table1 col1
--2 table1 col2
--3 table1 col3
--1 table2 col1
--2 table2 col2
--1 table3 col1
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90608 Visits: 38945
Snargables (3/7/2013)
NOTICE THE DESIRED OUTPUT ON THE BOTTOM.


SELECT 'table1' TABLENAME
,'col1' COLNAME
INTO #temp
UNION ALL
SELECT 'table1'
,'col2'
UNION ALL
SELECT 'table1'
,'col3'
UNION ALL
SELECT 'table2'
,'col1'
UNION ALL
SELECT 'table2'
,'col2'
UNION ALL
SELECT 'table3'
,'col1'


SELECT *
FROM #temp

--DESIRED OUTPUT ID COL:

--ID TABLENAME COLNAME
--1 table1 col1
--2 table1 col2
--3 table1 col3
--1 table2 col1
--2 table2 col2
--1 table3 col1


Okay, so what is the problem?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Snargables
Snargables
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1896 Visits: 839
I need to generate the id col values. I don’t know how to make a rank or rownumber reset.

notice it goes back to one. When it gets to a new table. I 've been playing w dense rank but don’t see one where it goes back to 1.

am I making any sense? for some reason i'm having a really hard time explaining it
Snargables
Snargables
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1896 Visits: 839
run the insert and select. Notice there is no id col being retruned. It's in the desired output. i need to find a way to generate that id patter in my select
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90608 Visits: 38945


select
ID = row_number() over (partition by TABLENAME order by COLNAME),
TABLENAME,
COLNAME
from
#temp;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Snargables
Snargables
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1896 Visits: 839
ah. row number w partition. nice. i was about to cross apply it to itself to get the darn id to reset. thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search