all groups > sql server programming > february 2004 >
You're in the

sql server programming

group:

dynamic numbering of results


dynamic numbering of results Jarrad
2/29/2004 10:01:08 PM
sql server programming:
am trying to get an sql query that will auto number rankings for me
The problem is as follows

Through a number of queries I can load a temporay table with data e.

Site_ID,Total_Sales,Sales_Dept,Sales_Chip
I need to end up with
Site_ID,Total_Sales,Ranking_TotalSales,Sales_Dept,Ranking_Sales_Dept,Sales_Chips,Ranking_Sales_Chip
for a number of sites ( note that these aren't the actual fields used)
not sure where to go for this on
have had a look at the knowledge base article 186133 and that didn't help me
Jarra
Re: dynamic numbering of results Jarrad
2/29/2004 11:11:06 PM
note that this is a simplification of the problem

DD
Table_On
Site_ID int
Total_Sales int
Sales_Dept2 int
Sales_Chips int
Chips_PT_AllSales floa
Dept2_PT floa
.
.
Re: dynamic numbering of results Uri Dimant
3/1/2004 8:22:48 AM
Jarrad
I am not sure following you.
CREATE TABLE #Temp
(
col INT NOT NULL PRIMARY KEY,
col1 CHAR(1) NOT NULL
)
INSERT INTO #Temp VALUES (1212,'A')
INSERT INTO #Temp VALUES (454,'B')
INSERT INTO #Temp VALUES (1412,'C')
INSERT INTO #Temp VALUES (142,'D')

SELECT col,col1,(SELECT COUNT(*) FROM #Temp T
WHERE T.col<=#Temp.col) AS Rank
FROM #Temp



[quoted text, click to view]
Site_ID,Total_Sales,Ranking_TotalSales,Sales_Dept,Ranking_Sales_Dept,Sales_C
hips,Ranking_Sales_Chips
[quoted text, click to view]

Re: dynamic numbering of results Roji. P. Thomas
3/1/2004 11:52:57 AM
PLz post DDL, sample data and desired results.

http://www.catb.org/~esr/faqs/smart-questions.html

--
Roji. P. Thomas
SQL Server Programmer
[quoted text, click to view]
Site_ID,Total_Sales,Ranking_TotalSales,Sales_Dept,Ranking_Sales_Dept,Sales_C
hips,Ranking_Sales_Chips
[quoted text, click to view]

Re: dynamic numbering of results Roji. P. Thomas
3/1/2004 1:03:38 PM
when someone says what is your DDL? It is the code used to create and
define your tables and correlating objects.


--
Roji. P. Thomas
SQL Server Programmer
[quoted text, click to view]

Re: dynamic numbering of results Roji. P. Thomas
3/1/2004 1:19:08 PM
Jarrad,
Try this


CREATE TABLE #Sales(Site_ID int,Total_Sales int,Sales_Dept2 int,
Sales_Chips int,Chips_PT_AllSales float,Dept2_PT float)

INSERT INTO #Sales VALUES(1,100,20,.20,40,.40)
INSERT INTO #Sales VALUES(2,100,30,.35,45,.45)
INSERT INTO #Sales VALUES(3,100,13,.13,50,.50)
INSERT INTO #Sales VALUES(4,100,15,.15,25,.25)
INSERT INTO #Sales VALUES(5,100 ,40,.40,30,.30)

SELECT Site_ID,
Total_Sales,
Sales_Dept2,
Sales_Chips,
Chips_PT_AllSales,
(SELECT COUNT(*) FROM #Sales B
WHERE B.Chips_PT_AllSales >= A.Chips_PT_AllSales) As Rank,
Dept2_PT
FROM #Sales A
ORDER BY Site_ID

DROP TABLE #Sales



--
Roji. P. Thomas
SQL Server Programmer
[quoted text, click to view]

AddThis Social Bookmark Button