使用2个表进行JOIN条件

时间:2018-10-30 03:54:53

标签: sql sql-server

如何为JOIN条件选择2个表,

我正在尝试查询所有需要出口的客户的特价,并且每位客户在3个表中存储3种产品折扣,

我想要得到的是(价格是样品价格):

type | sku | Unit_Price | customer_group
---------------------------
item | DS875 | 50         | COMPANY X
item | DS875 | 25       |   COMPANY Y
item | DS875 | 30       |   COMPANY Z
item | DS544 | 50         | COMPANY X
item | DS544 | 25       |   COMPANY Y
item | DS544 | 30       |   COMPANY Z
item | DS855 | 50         | COMPANY X
item | DS855 | 25       |   COMPANY Y
item | DS855 | 30       |   COMPANY Z

这是我的查询

SELECT 'item' AS type, p.PRODUCT_SKU AS sku, 
    CASE 
        WHEN pp.PRICE IS NULL AND pp.PRICE > 0 
        THEN
            pp.PRICE
        WHEN cp.Price IS NOT NULL AND cp.Price > 0 
        THEN
            cp.Price
        WHEN c.MarkupDiscount <> 0 
        THEN
                p.PRODUCT_PRICE -
                (p.PRODUCT_PRICE / 100 * c.MarkupDiscount)

    END AS Unit_Price,
    c.COMPANY_NAME AS customer_group
FROM
T002_PRODUCTS p
LEFT JOIN CUSTOMERS c ON c.VISIBLE = 1 AND c.ACTIVE = 1
RIGHT JOIN PROMOTION_PRICES pp ON 
                           (c.CUSTOMER_ID = pp.CUSTOMER_ID AND 
                            p.PRODUCT_ID = pp.PRODUCT_ID) AND 
                            pp.ACTIVE = 1 AND pp.DATE_FROM <= GETDATE() AND 
                            pp.DATE_TO >= GETDATE()
RIGHT JOIN AFC cp ON (c.CUSTOMER_ID = cp.HH AND 
                      cp.VC = p.PRODUCT_ID) AND cp.SA = 1

无法弄清楚如何正确使用:)

样本数据

产品

PRODUCT_ID|PRODUCT_SKU|PRODUCT_PRICE
____________________________________
8         | DS544     | 3.99
9         | DS855     | 5
10        | DS875     | 7

客户

CUSTOMER_ID | COMPANY_NAME | MarkupDiscount | VISIBLE | ACTIVE
______________________________________________________
78         | COMPANY X  |  15          |  1     | 1
79         | COMPANY Y  |  0           |  1     | 1
80         | COMPANY Z  |  0           |  1     | 1

PROMOTION_PRICES

CUSTOMER_ID | PRODUCT_ID | DATE_FROM   | DATE_TO    | ACTIVE | PRICE
____________________________________________________________
78          |  8         | '2018-01-01'|'2019-01-01'| 1      | 50

AFC

HH | VC | SA | Price
_____________________
80 |  8 |  1 | 50

1 个答案:

答案 0 :(得分:0)

这未经测试:

SELECT
    'item'         AS type
  , p.PRODUCT_SKU  AS sku
  , CASE
        WHEN pp.PRICE IS NULL AND
            pp.PRICE > 0 THEN pp.PRICE
        WHEN cp.Price IS NOT NULL AND
            cp.Price > 0 THEN cp.Price
        WHEN c.MarkupDiscount <> 0 THEN p.PRODUCT_PRICE -
            (p.PRODUCT_PRICE / 100 * c.MarkupDiscount)
    END            AS Unit_Price
  , c.COMPANY_NAME AS customer_group
FROM AFC cp
INNER JOIN CUSTOMERS c
    ON cp.HH = c.CUSTOMER_ID AND c.VISIBLE = 1 AND c.ACTIVE = 1
INNER JOIN T002_PRODUCTS p
    ON cp.VC = p.PRODUCT_ID
INNER JOIN PROMOTION_PRICES pp
    ON c.CUSTOMER_ID = pp.CUSTOMER_ID
    AND p.PRODUCT_ID = pp.PRODUCT_ID
    AND GETDATE() BETWEEN pp.DATE_FROM AND pp.DATE_TO
WHERE cp.SA = 1

恕我直言,选择“ from table”对于使查询易于跟踪至关重要。在这里,具有客户和产品的唯一表是AFC,并且由于我们都需要完成这项工作,因此从此开始。从那时起,这种关系就很清楚了。

提示: 许多经验丰富的SQL用户从不使用“右连接”,这不是因为它是不好的还是不正确的,这纯粹是因为您总是可以将表的顺序更改为避免这种联接类型。然后,连接的“流”更容易以“从上到下”的方式遵循。一旦引入了正确的连接,您就需要朝相反的方向思考,这可能会造成混乱。因此,一旦看到自己使用了正确的联接,就暂停并重新考虑您真正想从哪个表开始。

nb:并非每个人都会同意此技巧,但还会有更多人同意。