1

我有一个提供移动应用程序的网络服务。出于多种原因,我通过服务将数据发送到应用程序,并通过查询将产品、类别产品和类别连接到产品所在的每个类别的单个记录中:

 SELECT * FROM
    (SELECT ROW_NUMBER() OVER(ORDER BY Products.ItemID, Products.ManufacturerID) AS RowNum,
         Products.ProductID, Products.ItemID, Products.ManufacturerID, Categories.CategoryID, Categories.CategoryName, CategoryProducts.CategoryProductID,  Products.ItemName, Products.Description, Products.Notes, 
                      Products.Dimensions, Products.BasePrice, Products.OrderMinimumQuantity, 
                      ContainerMinimumQuantity = 
                        CASE  COALESCE(Products.ContainerMinQty, 0)
                            WHEN 0 THEN Products.OrderMinimumQuantity
                            ELSE Products.ContainerMinQty
                        END 
                      , Products.OrderMultipleQuantity, Products.OnHandQuantity, 
                      Products.Category, Products.IntroDate, Products.BackOrderDate, Products.UPC, Products.PriceLevel1, Products.PriceLevel2, Products.PriceLevel3, 
                      Products.PriceLevel4, Products.PriceLevel5, Products.PriceLevel6, Products.PriceLevel7, Products.PriceLevel8, Products.PriceLevel9, Products.PieceBox, 
                      Products.Cubes, Products.UnitOfMeasure, Products.UDF1, Products.UDF2, Products.UDF3, Products.UDF4, Products.UDF5, Products.AdditionalImageCount, 
                     PhotoName= LOWER(Products.PhotoName),  Products.Discontinued, Products.ModifiedOn, 
                        CategoryProducts.IsDeleted, Products.PriceLevel10, Products.PriceLevel11, Products.PriceLevel12, Products.PriceLevel13, 
                      Products.PriceLevel14, Products.PriceLevel15, Products.PriceLevel16, Products.PriceLevel17, Products.PriceLevel18, Products.PriceLevel19, Products.PriceLevel20, 
                      Products.Weight, Products.DimensionsMetric, Products.Source, Products.InventoryStatus, Products.CatalogCode, Products.CatalogName, 
                       CategoryProducts.SortOrder
 JOIN
                Categories INNER JOIN
                      CategoryProducts ON Categories.CategoryID = CategoryProducts.CategoryID RIGHT OUTER JOIN
                      Products ON CategoryProducts.ManufacturerID = Products.ManufacturerID AND CategoryProducts.ItemID = Products.ItemID
WHERE     (Products.ManufacturerID = @ManufacturerID)
            AND 
            (Products.ModifiedOn > @tStamp OR  CategoryProducts.ModifiedOn > @tStamp)
) AS Products
    WHERE RowNum >= @StartRow AND RowNum <= @EndRow

这让我的所有产品都变得扁平化,包括不属于任何类别的产品,这正是我想要的。这样做的问题是,如果产品已在某个类别中并且该类别产品已被删除,则 IsDeleted 标志设置在归结为设备的单个展平记录上。这是一个问题,因为产品本身并没有被删除,而是产品类别,但我的设备只看到一条记录,其中设置了 IsDeleted 标志以将其删除。因此,在这种情况下,我既需要包含已删除信息的展平产品的行,也需要该产品的行,就好像它没有被删除一样,以便用户仍然可以在设备上搜索该产品。

希望我清楚地解释了这一点,我已经为此苦苦挣扎了一个星期。

4

1 回答 1

0

If you need to see two rows, then I would expect to see a UNION ALL in the subquery. I think you need to include all products as well as all non-deleted product categories.

于 2012-04-13T20:38:15.427 回答