基于先前的查询在一个查询中多次选择

时间:2014-06-17 14:31:04

标签: sql sql-server sql-server-2008

请考虑以下SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE EcomGroupProductRelation
(
    GroupProductRelationProductID varchar(255), 
    GroupProductRelationSorting int
);

INSERT INTO EcomGroupProductRelation
(   GroupProductRelationProductID, 
    GroupProductRelationSorting
)
VALUES
    ('OneProd', 4),
    ('Another', 3),
    ('OneMore', 2),
    ('LastProd', 1);

查询1

  SELECT 
    GroupProductRelationSorting
  FROM 
    EcomGroupProductRelation
  WHERE
    GroupProductRelationProductID = 'Another' /* returns 3*/

Results

| GROUPPRODUCTRELATIONSORTING |
|-----------------------------|
|                           3 |

问题: - 如何在同一查询中选择基于GroupProductRelationSorting的下一行?

我想输出的结果应该是:

| GROUPPRODUCTRELATIONPRODUCTID | GROUPPRODUCTRELATIONSORTING |
|-------------------------------|-----------------------------|
|                       OneProd |                           4 |

我知道我可以做这样的事情(伪代码):

 WITH Temp AS
 (
      SELECT 
           GroupProductRelationSorting
      FROM 
           EcomGroupProductRelation
      WHERE
           GroupProductRelationProductID = 'Another' /* returns 3*/
 )

 SELECT 
      * 
 FROM 
      EcomGroupProductRelation
 WHERE 
      GroupProductRelationSorting=Temp+1

(Based on the answer found here)

但我似乎无法理解如何做到这一点?

3 个答案:

答案 0 :(得分:0)

试试这个

WITH cte AS (
   SELECT
      GroupProductRelationSorting,
      GroupProductRelationProductID,
      ROW_NUMBER() OVER (ORDER BY GroupProductRelationSorting) rw
   FROM EcomGroupProductRelation
)

SELECT
  c2.*
FROM cte c1
JOIN cte c2 ON c1.rw = c2.rw-1
WHERE c1.GroupProductRelationProductID = 'Another'

小提琴:http://sqlfiddle.com/#!3/1e4fb/5/0

答案 1 :(得分:0)

你几乎写了答案。 你只需要改变你的代码

由此:

  SELECT * FROM EcomGroupProductRelation WHERE GroupProductRelationSorting=Temp+1

到此

 SELECT * FROM EcomGroupProductRelation WHERE GroupProductRelationSorting=(SELECT GroupProductRelationSorting FROM Temp)+ 1

你正在给桌子,但你需要给出标量值。

或者您可以将第一个选择值放入变量,然后对其进行过滤:

DECLARE @Temp INT = 0

SELECT @Temp = GroupProductRelationSorting
FROM EcomGroupProductRelation
WHERE GroupProductRelationProductID = 'Another' /* returns 3*/

 SELECT * FROM EcomGroupProductRelation WHERE GroupProductRelationSorting=@Temp+1

答案 2 :(得分:0)

Declare @EcomGroupProductRelation TABLE 
(
    GroupProductRelationProductID varchar(255), 
    GroupProductRelationSorting int
);

INSERT INTO @EcomGroupProductRelation
(   GroupProductRelationProductID, 
    GroupProductRelationSorting
)
VALUES
    ('OneProd', 4),
    ('Another', 3),
    ('OneMore', 2),
    ('LastProd', 1);

 SELECT TOP 1 *
FROM   (SELECT TOP 2 *
        FROM   @EcomGroupProductRelation
        WHERE  GroupProductRelationSorting >= 3
        ORDER  BY GroupProductRelationSorting) T
ORDER  BY GroupProductRelationProductID DESC  

OR

 ;WITH Temp AS
 (
      SELECT GroupProductRelationProductID,
           ROW_NUMBER()OVER(ORDER BY GroupProductRelationSorting )AS Rn
      FROM 
           @EcomGroupProductRelation

 )
 SELECT P.GroupProductRelationProductID 
 FROM Temp P
 LEFT JOIN Temp T ON P.RN = T.Rn + 1
 WHERE T.GroupProductRelationProductID = 'Another'