如何为此SQL查询添加行号

时间:2015-10-01 19:02:14

标签: sql sql-server rows

我有这个SQL Server 2008 R2查询

SELECT TOP(10)
    *
FROM 
    [T1].dbo.CPU_Benchmarks 
JOIN 
    [T1].dbo.CPU_Slugs ON CPU_Benchmarks.Id = [T1].dbo.CPU_Slugs.BenchmarkId 
                       AND [Approved] = 'true')
ORDER BY 
     [T1].dbo.[CPU_Benchmarks].Overal_Score DESC

2 个答案:

答案 0 :(得分:2)

SELECT TOP (10)  
    row_number() over (order by [T1].dbo.[CPU_Benchmarks].Overal_Score DESC) as rn, *
FROM [T1].dbo.CPU_Benchmarks
JOIN [T1].dbo.CPU_Slugs 
   ON CPU_Benchmarks.Id = [T1].dbo.CPU_Slugs.BenchmarkId
  AND [Approved] = 'true'
ORDER BY 
     [T1].dbo.[CPU_Benchmarks].Overal_Score DESC

答案 1 :(得分:1)

#include <string>
#include <iostream>
 using std::cout;
 using std::endl;

void reverse(char* target) //Requirements specify to have this argument
{
cout << "Before :" << target << endl; // Print out the word to be reversed
if (strlen(target) > 1) // Check incase no word or 1 letter word is placed
{
    char* firstChar = &target[0]; // First Char of char array
    char* lastChar = &target[strlen(target) - 1]; //Last Char of char array
    char temp; // Temp char to swap
    while (firstChar < lastChar) // File the first char position is below the last char position
    {
        temp = *firstChar; // Temp gets the firstChar
        *firstChar = *lastChar; // firstChar now gets lastChar
        *lastChar = temp; // lastChar now gets temp (firstChar)
        firstChar++; // Move position of firstChar up one
        lastChar--; // Move position of lastChar down one and repeat loop
    }
}
cout << "After :" << target << endl; // Print out end result.
}




 int main()
{
std::string userInput;
std::cin >> userInput;
char str[] = userInput; // <- This is the Key Part of my Question.
reverse(str);
 }

您可以使用SELECT row_number() over(ORDER BY [T1].dbo.[CPU_Benchmarks].Overal_Score DESC) as rn, * FROM [T1].dbo.CPU_Benchmarks JOIN [T1].dbo.CPU_Slugs ON CPU_Benchmarks.Id=[T1].dbo.CPU_Slugs.BenchmarkId AND [Approved]='true' 功能。