If-Else在多个Select语句中 请考虑下面的SQL模式和语句。
表: 声明: 在我的示例中,我得到的是PROD7之前的产品,即PROD3。不幸的是,由于ProductActive中的0,此产品未处于活动状态 问题: 我需要上一个产品,但前提是它在ProductActive列中是活动的a 1。 如果未激活,则应选择该选项之前的选项。If-Else在多个Select语句中 请考虑下面的SQL模式和语句。 ,sql,sql-server-2008,Sql,Sql Server 2008,表: 声明: 在我的示例中,我得到的是PROD7之前的产品,即PROD3。不幸的是,由于ProductActive中的0,此产品未处于活动状态 问题: 我需要上一个产品,但前提是它在ProductActive列中是活动的a 1。 如果未激活,则应选择该选项之前的选项。 是否可以在单个查询中执行此操作 如果一个给定产品只需要上一个产品,那么可以使用如下查询: select TOP 1 p.ProductID from EcomGroupProductRelation pr join Ec
是否可以在单个查询中执行此操作 如果一个给定产品只需要上一个产品,那么可以使用如下查询:
select TOP 1 p.ProductID
from EcomGroupProductRelation pr join
EcomProducts p
on p.ProductID = pr.GroupProductRelationProductID
where p.ProductActive = 1 and
pr.GroupProductRelationSorting < (select GroupProductRelationSorting
from EcomGroupProductRelation pr2
where pr2.GroupProductRelationProductID = 7
)
order by pr.GroupProductRelationSorting desc;
注意:如果没有产品,则此查询不返回任何行。如果过滤掉非活动产品,则在某种意义上,您需要重新排序排序列以确定要添加的列。这可以通过行数函数完成,如下所示:
WITH Temp AS
(
SELECT GroupProductRelationSorting
FROM EcomGroupProductRelation
WHERE GroupProductRelationProductID = 'PROD7'
)
SELECT GroupProductRelationProductID
FROM
(
SELECT
GroupProductRelationProductID,
ROW_NUMBER() OVER (PARTITION BY GroupProductRelationGroupID ORDER BY GroupProductRelationSorting DESC) AS RN
FROM
EcomGroupProductRelation
INNER JOIN
EcomProducts ON
GroupProductRelationProductID = ProductID
WHERE
ProductActive = 1 AND
GroupProductRelationSorting < (SELECT GroupProductRelationSorting FROM Temp)
) x
WHERE RN = 1
如果最后两个产品未激活,会发生什么情况?您是否应该返回,直到找到最后一个活动产品?Temp是否包含的行数不超过1行?@jarlh Yes始终为一行great@GordonLinoff!:非常感谢。这是有道理的。GroupProductRelationProductID应为varchar。但它会返回它应该返回的内容。我可以用这个,但不能完全回答问题。@Crave。在这种情况下,我不理解这个问题。好的,部分问题,但不是全部:如果它不是活动的,应该选择该问题之前的问题。是否可以在单个查询中执行此操作;你能帮我买下一个产品吗?同样的款式?我已经尝试过查询您建议的ASC,但似乎没有返回正确的产品。@Crave。如果你还有其他问题,你应该把它当作一个问题来问。然而,我预计下一个产品将只是一个逆转
select TOP 1 p.ProductID
from EcomGroupProductRelation pr join
EcomProducts p
on p.ProductID = pr.GroupProductRelationProductID
where p.ProductActive = 1 and
pr.GroupProductRelationSorting < (select GroupProductRelationSorting
from EcomGroupProductRelation pr2
where pr2.GroupProductRelationProductID = 7
)
order by pr.GroupProductRelationSorting desc;
WITH Temp AS
(
SELECT GroupProductRelationSorting
FROM EcomGroupProductRelation
WHERE GroupProductRelationProductID = 'PROD7'
)
SELECT GroupProductRelationProductID
FROM
(
SELECT
GroupProductRelationProductID,
ROW_NUMBER() OVER (PARTITION BY GroupProductRelationGroupID ORDER BY GroupProductRelationSorting DESC) AS RN
FROM
EcomGroupProductRelation
INNER JOIN
EcomProducts ON
GroupProductRelationProductID = ProductID
WHERE
ProductActive = 1 AND
GroupProductRelationSorting < (SELECT GroupProductRelationSorting FROM Temp)
) x
WHERE RN = 1
WITH Temp AS
(
SELECT GroupProductRelationSorting
FROM EcomGroupProductRelation
WHERE GroupProductRelationProductID = 'PROD7'
)
SELECT GroupProductRelationProductID
FROM
(
SELECT
GroupProductRelationProductID,
ROW_NUMBER() OVER (PARTITION BY GroupProductRelationGroupID ORDER BY GroupProductRelationSorting) AS RN
FROM
EcomGroupProductRelation
INNER JOIN
EcomProducts ON
GroupProductRelationProductID = ProductID
WHERE
ProductActive = 1 AND
GroupProductRelationSorting > (SELECT GroupProductRelationSorting FROM Temp)
) x
WHERE RN = 1