如果在多选语句中有其他

时间:2015-08-27 11:38:39

标签: sql sql-server-2008

请考虑以下SQL Schema和语句。 http://sqlfiddle.com/#!3/06c46/1/0

表格

CREATE TABLE EcomGroupProductRelation 
(
    GroupProductRelationGroupID varchar(10), 
    GroupProductRelationProductID varchar(10), 
    GroupProductRelationSorting int
);

INSERT INTO EcomGroupProductRelation
    (
        GroupProductRelationGroupID,
        GroupProductRelationProductID,
        GroupProductRelationSorting
    )
VALUES
    ('GROUP1', 'PROD3', 0),
    ('GROUP1', 'PROD7', 1),
    ('GROUP1', 'PROD2', 2),
    ('GROUP1', 'PROD4', 3),
    ('GROUP1', 'PROD6', 5),
    ('GROUP1', 'PROD8', 6),
    ('GROUP1', 'PROD10', 7);

CREATE TABLE EcomProducts 
(
    ProductID varchar(10), 
    ProductName varchar(255), 
    ProductActive int,
    ProductAutoID int Primary Key
);

INSERT INTO EcomProducts
(
    ProductID,
    ProductName,
    ProductActive,
    ProductAutoID
)
VALUES
    ('PROD2', 'My 2. prod', 1, 2),
    ('PROD3', 'My 3. prod', 0, 3),
    ('PROD4', 'My 4. prod', 1, 4),
    ('PROD5', 'My 5. prod', 1, 5),
    ('PROD6', 'My 6. prod', 1, 6),
    ('PROD7', 'My 7. prod', 1, 7),
    ('PROD8', 'My 8. prod', 1, 8),
    ('PROD9', 'My 9. prod', 1, 9),
    ('PROD10', 'My 10. prod', 1, 10);

声明:

    WITH Temp AS
    (
            SELECT 
                    GroupProductRelationSorting 
            FROM 
                    EcomGroupProductRelation 
            WHERE 
                    GroupProductRelationProductID = 'PROD7'
    ) 
    SELECT 
            GroupProductRelationProductID 
    FROM 
            EcomGroupProductRelation 
    WHERE 
            GroupProductRelationSorting=
            (
                    SELECT 
                            GroupProductRelationSorting 
                    FROM 
                            Temp
            )-1

在我的示例中,我在PROD7之前获得了产品,即PROD3。不幸的是,由于0中的ProductActive,此产品无效。

问题:
 我需要以前的产品,但前提是它是有效的(ProductActive列中的1) 如果它不活动,则应选择该那个之前的那个。 是否可以在单个查询中执行此操作?

2 个答案:

答案 0 :(得分:0)

如果您只需要一个给定产品的上一个产品,那么您可以使用如下查询:

    viewPager.addOnPageChangeListener(new SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {

            if (position == POSITION_OF_PAGE_TO_RESET + 1 ||
                    position == POSITION_OF_PAGE_TO_RESET - 1) {
                FragmentPagerAdapter adapter = (FragmentPagerAdapter) viewPager.getAdapter();
                ResettableFragment fragment = (ResettableFragment) adapter.getItem(position);
                fragment.reset();
            }
        }
    });

注意:如果没有产品,则此查询不返回任何行。

答案 1 :(得分:0)

如果您过滤掉非活动产品,那么在某种意义上,您需要重新排序“排序”列以确定要添加的列。这可以通过ROW_NUMBER()函数完成,如下所示:

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 

为“下一个产品”执行此操作将涉及更改以下几个条件:撤消RN字段的排序顺序,并更改WHERE子句以仅显示大于{{的记录1}}值。

Temp