简体   繁体   English

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

[英]Multiple selects in one query, based on previous query

Please consider the following SQL Fiddle 请考虑以下SQL Fiddle

MS SQL Server 2008 Schema Setup : 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);

Query 1 : 查询1

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

Results : 结果

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

QUESTION: - How can I select the next row - based on GroupProductRelationSorting , in the same query? 问题: -如何在同一查询中基于GroupProductRelationSorting选择下一行?

The result I would like to output, should be: 我想输出的结果应该是:

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

I know I can do something like this (pseudocode-ish): 我知道我可以做这样的事情(pseudocode-ish):

 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) (根据此处找到的答案)

But I can't seem to get my head around how to do it? 但是我似乎无法理解该怎么做?

try this 尝试这个

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'

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

You almost wrote the answer. 您差点写了答案。 You just need to change a bit your code 您只需要更改一点代码

From this : 由此 :

  SELECT * FROM EcomGroupProductRelation WHERE GroupProductRelationSorting=Temp+1

To this 对此

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

You was giving table, but you need to give scalar value. 您正在提供表格,但需要提供标量值。

Or you can put first select value to variable and then filter it: 或者,您可以先将选择值放入变量,然后对其进行过滤:

DECLARE @Temp INT = 0

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

 SELECT * FROM EcomGroupProductRelation WHERE GroupProductRelationSorting=@Temp+1
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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM