【问题标题】:Using SQL, have components of a product appear horizontally beside product使用 SQL,让产品的组件水平显示在产品旁边
【发布时间】:2022-12-08 09:24:44
【问题描述】:

我试图让属于产品一部分的所有组件彼此出现在同一行

我有两张桌子

产品

ID  PRODUCTNUMBER   DESCRIPTION                                 TYPE        STATUS  KIT
1 (PK)  121         1 Apples and 1 Oranges                      FRUIT PACK  YES     Y
2       122         2 Brocolli & 2 Carrots                      VEG PACK    NO      Y
3       123         3 Strawberries and 3 Blueberries and 1 Pear FRUIT PACK  YES     Y
4       124         2 Plums and 1 Pears                         FRUIT PACK  YES     Y
5       125         4 Grapes and 2 Cabbage                      COMBO PACK  YES     Y
6       126         Apple                                       FRUIT       YES     N
7       127         Orange                                      FRUIT       YES     N
8       128         Pear                                        FRUIT       YES     N
9       129         Onion                                       VEG         NO      N
10      130         Blueberry                                   FRUIT       YES     N
11      131         Strawberry                                  FRUIT       YES     N
12      132         Plum                                        FRUIT       YES     N

产品组件

PRODUCT QTY
5   55
6   45
7   21
8   12
9   0
10  20
11  25
12  50

我的 SQL 查询应该返回:

SKU Description                 COMPONENT1  QTY1    COMPONENT2  QTY2    COMPONENT3  QTY3
121 1 Apples and 1 Oranges              Apple       55  Orange      45      
123 3 Strawberries and 3 Blueberries and 1 Pear Strawberries    25  Blueberry   20  Pear        12
124 2 Plums and 1 Pears             Plum        50  Pear        12      

我试过了:

SELECT 
    PRODUCT.CODE, PRODUCT.DESCRIPTION,
    PRODUCTCOMPONENT.PRODUCT, PRODUCTCOMPONENT.QTY
FROM
    PRODUCT 
INNER JOIN 
    PRODUCTCOMPONENT ON PRODUCTCOMPONENT.PRODUCT = PRODUCT.ID 
WHERE 
    PRODUCT.STATUS = YES
    AND PRODUCT.KIT = Y;

任何帮助,将不胜感激

【问题讨论】:

  • 抱歉,在我发布后格式变坏了
  • 我需要更多信息来提出建议。 “在 SQL 中”的要求有多严格?这些信息是否会被引入其他应用程序(例如 Excel 或报告工具?)
  • 所以我要导出到 CSV。 WMS 将获取该文件并将其导入。
  • 好的谢谢你。下一个问题是:一个套件中最多可以包含多少个组件?
  • 我会在我的回答中解释为什么你需要预先知道。

标签: sql sql-server


【解决方案1】:

好的,这是凭记忆,但我已经用 SQL Fiddle 验证了语法。

你是对的,你需要从PRODUCTPRODUCTCOMPONENT开始。您发布的代码将为您提供所需的数据——但它不会在列中提供答案,而只是在行中提供。

所以你所拥有的就是我所说的“轮换问题”。您想要将数据“摆动”超过 90 度(可以这么说),并且在有多行的地方有多个列。

没有自动的内置方法来执行此操作。但是有间接的方法。

您需要做的是对每组要显示组件信息的列左外连接 PRODUCTCOMPONENTPRODUCT 一次。

如果您有 2 列,则需要执行两次。因为你的最大值是 5,所以你需要做 5 次。

这就是为什么我问每个项目可以有多少个组件。如果你有一个不确定的数字,你就会倒霉,因为没有简单的方法来 对于您碰巧拥有的行,自动将列向右扩展。你必须为每个额外的可能组件做一个新的左连接子句!

这是 2 列案例的示例,它应该向您展示如何执行 5 列案例:

    -- In order to join to just the records from Row 1, we need to number them! 
    -- We'll do that in a CTE (Common Table Expression).
    ; 
    WITH Components as (
      -- I don't know all the columns in PRODUCTCOMPONENT, but you presumably have a
      -- parent and child ID. Substitute the true names of the columns for the 
      -- column names I'm using
      SELECT ParentId
           , ChildId
           , Product -- I am assuming this is the product name
           , Qty
           -- The following line will assign a line number to each component within
           -- a product. If there's a particular order you want the columns to appear in,
           -- change the "Order by" part of the ROW_NUMBER() OVER expression.
           , RowNumber = ROW_NUMBER() OVER (Partition By ParentId  Order By ChildId)
      FROM   ProductComponent 
    )
    SELECT   Product.PRODUCTNUMBER as Code
           , Product.DESCRIPTION
           , Component1.Product as Component1
           , Component1.Qty as Qty1
           , Component2.Product as Component2
           , Component2.Qty as Qty2
    FROM     Product
         -- Note that since some products will have more components than others,
         -- you need to left-join to the Components CTE to make sure that rows are
         -- still returned even when they only have nulls.
         LEFT OUTER JOIN Components as Component1
           ON  Product.ID = Component1.ParentID
           AND Component1.RowNumber = 1
         -- The second clause of the JOIN means that you'll only get rows back
         -- from the CTE if the RowNumber assigned in the CTE is (in this case) 2.
         LEFT OUTER JOIN Components as Component2
           ON  Product.ID = Component2.ParentID
           AND Component2.RowNumber = 2
    WHERE   Product.STATUS = 'YES'
        AND Product.KIT = 'Y';

【讨论】:

  • 感谢 Ann 的超级描述性回答,非常感谢您抽出时间来做这件事。它就像一个魅力
猜你喜欢
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
相关资源
最近更新 更多