2012-04-03 13 views
0

私は、SQL Server 2008のテーブルt_listで、次の列があります。
firstid, firstpoints, secondid, secondpointsこのようなクエリをどのように実行するのですか?

私は特定の方法でSQLクエリが、順番にそれらの列をすべて選択します。

私だけ firstid列に firstpointsにより発注、それらの列の最上部400を選択したい

、とだけ例えば1つのクエリでsecondid列、ためsecondpoints

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

これは、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 

希望に取り組んでいます。

enter image description here

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 
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 
-1

あなたは単に 'UNION ALL' を使用して一つのテーブルにあなたの2つのフィルタを組み合わせる必要が

(firstpointsのDESC BY
t_list ORDER FROM TOP 400 firstid、firstpoints を選択)

UNION ALL

(選択トップ400 secondid、secondpoints
FROM t_list
ORDER BY secondpoints desc

関連する問題