Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

RANK ISSUE Expand / Collapse
Author
Message
Posted Thursday, March 7, 2013 12:57 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:25 AM
Points: 152, Visits: 375
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
Post #1428215
Posted Thursday, March 7, 2013 1:01 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 2:48 PM
Points: 23,397, Visits: 32,244
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?



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)
Post #1428219
Posted Thursday, March 7, 2013 1:06 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:25 AM
Points: 152, Visits: 375
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
Post #1428224
Posted Thursday, March 7, 2013 1:08 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:25 AM
Points: 152, Visits: 375
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
Post #1428227
Posted Thursday, March 7, 2013 1:13 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 2:48 PM
Points: 23,397, Visits: 32,244

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





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)
Post #1428236
Posted Thursday, March 7, 2013 1:18 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:25 AM
Points: 152, Visits: 375
ah. row number w partition. nice. i was about to cross apply it to itself to get the darn id to reset. thanks
Post #1428239
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse