0

SQLServer2008のテーブルに次の列がありますt_list
firstid, firstpoints, secondid, secondpoints

SQLクエリでこれらの列をすべて選択したいのですが、特定の方法で並べ替えます。

これらの列の上位400を選択し、列のみ、および1つのクエリの列firstpointsのみで並べ替えます。次に例を示します。firstidsecondpointssecondid

SELECT TOP 400 firstid, firstpoints 
FROM t_list 
ORDER BY firstpoints desc 

select top 400 secondid, secondpoints 
FROM t_list 
ORDER BY secondpoints desc

上記の2つのクエリを1つのクエリに適合させ、次のような出力を返すにはどうすればよいですか。

firstid, firstpoints, secondid, secondpoints

1. firstidresult, firstpointresult, secondidresult, secondpointsresult
2. etc...
4

4 に答える 4

4

これはMS SQL Serverで動作します

Select t1.FirstID, t1.FirstPoints, t2.SecondID, t2.SecondPoints
From (Select Top 400 row_number() Over (Order By FirstPoints desc) As r, FirstID, FirstPoints From t_list) As t1
    Join (Select Top 400 row_number() Over (Order By SecondPoints desc) As r, SecondID, SecondPoints From t_list) as t2 on t1.r = t2.r

あなたの問題を正しく理解できたことを願っています。

ここに画像の説明を入力

于 2012-04-04T00:55:52.650 に答える
0
    SELECT *
FROM (
    SELECT TOP 400 FirstId
        ,FirstPoints
    FROM t_list
    ORDER BY FirstPoints
    ) f

UNION

SELECT *
FROM (
    SELECT TOP 400 SecondId
        ,SecondPoints
    FROM t_list
    ORDER BY SecondPoints
    ) s
于 2012-04-03T23:58:45.610 に答える
0
WITH CTE1 AS 
(SELECT 1 as [Order],
    firstid, 
    firstpoints,
    Row_Number() OVER (ORDER BY firstpoints desc ) as RowNum
FROM t_list 
),
CTE2 AS 
(SELECT 
    2,
    secondid, 
    secondpoints,
    Row_Number() OVER (ORDER BY secondpoints) as RowNum
FROM t_list 
)
SELECT [Order],
    firstid as Id, 
    firstpoints  as Points
FROM CTE1
WHERE RowNum <= 400
UNION 
SELECT [Order],
     secondid, 
     secondpoints 
FROM CTE2
WHERE RowNum <= 400
ORDER BY [Order], RowNum 
于 2012-04-03T23:59:04.847 に答える
-1

「UNION ALL」を使用して、2 つのフィルターを 1 つのテーブルに結合するだけです。

(上位 400 の firstid、firstpoints FROM t_list
ORDER BY firstpoints desc を選択)

ユニオンオール

(上位 400 の secondid、secondpoints
FROM t_list
ORDER BY secondpoints desc を選択)

于 2012-04-04T05:39:57.213 に答える