简体   繁体   English

多个自我联接加一个内部联接

[英]Multiple self joins plus one inner join

I have two tables: ck_startup and ck_price . 我有两个表: ck_startupck_price The price table contains the columns cu_type , prd_type , part_cd , qty , and dllrs . 价格表包含cu_typeprd_typepart_cdqtydllrs The startup table is linked to the price table through a one-to-many relationship on ck_startup.prd_type_cd = ck_price.prd_type . 启动表通过ck_startup.prd_type_cd = ck_price.prd_type上的一对多关系链接到价格表。

The price table contains multiple entries for the same product/part/qty but under different customer types. 价格表包含相同产品/零件/数量但在不同客户类型下的多个条目。 Not all customer types have the same unique combination of those three values. 并非所有客户类型都具有这三个值的相同唯一组合。 I'm trying to create a query that will do two things: 我正在尝试创建一个查询,它将做两件事:

  1. Join some columns from ck_startup onto ck_price (description, and some additional values). ck_price某些列ck_startupck_price (描述和一些其他值)。
  2. Join ck_price onto itself with a dllrs column for each customer type. ck_price与每种客户类型的dllrs列连接ck_price So in total I would only have one instance of each unique key of product/part/qty, and a value in each customer's price column if they have one. 因此,总的来说,每个产品/零件/数量的唯一键只有一个实例,如果每个客户的价格列都有一个值,那么我将只有一个实例。

I've never worked with self joining tables, and so far I can only get records to show up where both customers have the same options available. 我从未使用过自联接表,到目前为止,我只能获得记录,以显示两个客户都有相同可用选项的地方。

And because someone is going to demand I post sample code, here's the crappy query that doesn't show missing prices: 而且由于有人要我发布示例代码,因此这是不显示缺失价格的糟糕查询:

select pa.*, pac.dllrs from ck_price pa
join ck_price pac on pa.prd_type = pac.prd_type and pa.part_carbon_cd = pac.part_carbon_cd and pa.qty = pac.qty
where pa.cu_type = 'A' and pac.cu_type = 'AC';

EDIT: Here's sample data from the two tables, and how I want them to look when I'm done: 编辑:这是来自两个表的示例数据,以及完成后我希望它们如何显示:

CK_STARTUP
+-----+-----------------+-------------+
| CD  |       DSC       | PRD_TYPE_CD |
+-----+-----------------+-------------+
| 3D  | Stuff           | SKD3        |
| DC  | Different stuff | SKD         |
| DN2 | Similar stuff   | SKD         |
+-----+-----------------+-------------+

CK_PRICE
+---------+-------------+---------+-----+-------+
| CU_TYPE | PRD_TYPE_CD | PART_CD | QTY | DLLRS |
+---------+-------------+---------+-----+-------+
| A       | SKD3        |       1 | 100 |    10 |
| A       | SKD3        |       1 | 200 |    20 |
| A       | SKD3        |       1 | 300 |    30 |
| A       | SKD         |       1 | 100 |    50 |
| A       | SKD         |       1 | 200 |   100 |
| AC      | SKD3        |       1 | 300 |    30 |
| AC      | SKD         |       1 | 100 |   100 |
| AC      | SKD         |       1 | 200 |   200 |
| AC      | SKD         |       1 | 300 |   300 |
| AC      | SKD         |       1 | 400 |   400 |
+---------+-------------+---------+-----+-------+

COMBO:
+----+-----------------+---------+-----+---------+----------+
| CD |       DSC       | PART_CD | QTY | DLLRS_A | DLLRS_AC |
+----+-----------------+---------+-----+---------+----------+
| 3D | Stuff           |       1 | 100 | 10      | null     |
| 3D | Stuff           |       1 | 200 | 20      | null     |
| 3D | Stuff           |       1 | 300 | 30      | 60       |
| DC | Different stuff |       1 | 100 | 50      | 100      |
| DC | Different stuff |       1 | 200 | 100     | 200      |
| DC | Different stuff |       1 | 300 | null    | 300      |
| DC | Different stuff |       1 | 400 | null    | 400      |
+----+-----------------+---------+-----+---------+----------+

Ok, take a look at below query and at the results: 好的,看看下面的查询和结果:

SELECT *
FROM   (SELECT
          cs.cd, cs.dsc, cp.part_cd, cp.qty, cp.dllrs, cp.cu_type
        FROM ck_startup cs
          JOIN ck_price cp ON (cs.prd_type_cd = cp.prd_type_cd))
PIVOT (SUM(dllrs) AS dlllrs FOR (cu_type) IN ('A' AS a, 'AC' AS ac))
ORDER BY cd, qty
;

Output: 输出:

CD       DSC                  PART_CD     QTY   A_DLLLRS  AC_DLLLRS
-------- ----------------- ---------- ------- ---------- ----------
3D       Stuff                      1     100         10            
3D       Stuff                      1     200         20            
3D       Stuff                      1     300         30         30 
DC       Different stuff            1     100         50         50 
DC       Different stuff            1     200        100        100 
DC       Different stuff            1     300                   150 
DC       Different stuff            1     400                   200 
DN2      Similar stuff              1     100         50         50 
DN2      Similar stuff              1     200        100        100 
DN2      Similar stuff              1     300                   150 
DN2      Similar stuff              1     400                   200

It is not what you would expect, because I do not understand why you have different values in DLLRS_AC column that are in the CK_PRICE table? 这不是您所期望的,因为我不理解为什么您在CK_PRICE表的DLLRS_AC列中有不同的值? I mean, for example, why do you have 400 in last line of your output, not 200 ? 我的意思是,例如,为什么输出的最后一行有400而不是200 Why is this value doubled (as others are in DLLRS_AC column)? 为什么将此值加倍(因为其他值在DLLRS_AC列中)?

If you are using Oracle 10g, you can achieve the same result using DECODE and GROUP BY , take a look: 如果您使用的是Oracle 10g,则可以使用DECODEGROUP BY达到相同的结果,请看一下:

SELECT
        cd,
        dsc,
        part_cd,
        qty,
        SUM(DECODE(cu_type, 'A', dllrs, NULL)) AS dllrs_a,
        SUM(DECODE(cu_type, 'AC', dllrs, NULL)) AS dllrs_ac
FROM (
  SELECT
    cs.cd, cs.dsc, cp.part_cd, cp.qty, cp.dllrs, cp.cu_type
  FROM ck_startup cs
    JOIN ck_price cp ON (cs.prd_type_cd = cp.prd_type_cd)
  )
GROUP BY cd, dsc, part_cd, qty
ORDER BY cd, qty;

Result is the same. 结果是一样的。

If you want to read more about pivoting, I recommend article by Tim Hall: Pivot and Unpivot at Oracle Base 如果您想了解有关透视的更多信息,建议您阅读Tim Hall的文章: Oracle Base的透视和反透视

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

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