SQL - 加入 2 个表以及 return 两行之间的值差
SQL - Join 2 tables and also return difference in value between 2 rows
我正在尝试 return 具有各种条件(搜索词、日期等)的产品列表,我有以下示例 SQL。
SELECT *
FROM products
JOIN ( SELECT date, price, product_id
FROM pricing
WHERE id IN ( (SELECT MAX(id)
FROM pricing
GROUP BY product_id ) )
AND date = '2021-09-08' ) as current_price on products.id=price.product_id
WHERE 1
AND sku like '%%'
OR title like '%%'
ORDER BY id ASC
它从 2 个表中提取数据:产品和定价。
产品
+----+----------------+--------+
| id | title | sku |
+----+----------------+--------+
| 1 | Product Name 1 | 654987 |
| 2 | Product Name 2 | 548879 |
| 3 | Product Name 3 | 541685 |
+----+----------------+--------+
定价
+----+------------+------------+-------+
| id | product_id | date | price |
+----+------------+------------+-------+
| 1 | 1 | 06-09-2021 | 1.99 |
| 2 | 2 | 06-09-2021 | 1.99 |
| 3 | 1 | 07-09-2021 | 3.99 |
| 4 | 3 | 06-09-2021 | 5.99 |
| 5 | 3 | 08-09-2021 | 6.99 |
| 6 | 1 | 08-09-2021 | 6.99 |
+----+------------+------------+-------+
期望的输出
+----+----------------+--------+---------------+-------------------+------------+
| id | title | sku | current_price | last_price_update | difference |
+----+----------------+--------+---------------+-------------------+------------+
| 1 | Product Name 1 | 654987 | 6.99 | 08-09-2021 | 2.5 |
| 2 | Product Name 2 | 548879 | 1.99 | 06-09-2021 | 0 |
| 3 | Product Name 3 | 541685 | 6.99 | 08-09-2021 | -1 |
+----+----------------+--------+---------------+-------------------+------------+
我的问题是我也想要一个差异列 returned,它显示最新价格与之前价格之间的差异。
我尝试了十亿种东西,但 SQL 不是我的菜,我要么 return 完整的价格清单,要么什么都不做。
使用 MariaDB 版本:10.3.30
谢谢,
阿什莉
您可以使用row_number()
枚举价格,然后条件聚合得到两个价格:
select p.*, pr.current_price, pr.previous_price,
(pr.current_price - pr.previous_price) as diff
from products p join
(select product_id,
max(case when seqnum = 1 then price end) as current_price,
max(case when seqnum = 2 then price end) as previous_price
from (select pr.*,
row_number() over (partition by product_id order by date desc) as seqnum
from pricing pr
) pr
where seqnum <= 2
group by product_id
) pr
on pr.product_id = p.id;
我正在尝试 return 具有各种条件(搜索词、日期等)的产品列表,我有以下示例 SQL。
SELECT *
FROM products
JOIN ( SELECT date, price, product_id
FROM pricing
WHERE id IN ( (SELECT MAX(id)
FROM pricing
GROUP BY product_id ) )
AND date = '2021-09-08' ) as current_price on products.id=price.product_id
WHERE 1
AND sku like '%%'
OR title like '%%'
ORDER BY id ASC
它从 2 个表中提取数据:产品和定价。
产品
+----+----------------+--------+
| id | title | sku |
+----+----------------+--------+
| 1 | Product Name 1 | 654987 |
| 2 | Product Name 2 | 548879 |
| 3 | Product Name 3 | 541685 |
+----+----------------+--------+
定价
+----+------------+------------+-------+
| id | product_id | date | price |
+----+------------+------------+-------+
| 1 | 1 | 06-09-2021 | 1.99 |
| 2 | 2 | 06-09-2021 | 1.99 |
| 3 | 1 | 07-09-2021 | 3.99 |
| 4 | 3 | 06-09-2021 | 5.99 |
| 5 | 3 | 08-09-2021 | 6.99 |
| 6 | 1 | 08-09-2021 | 6.99 |
+----+------------+------------+-------+
期望的输出
+----+----------------+--------+---------------+-------------------+------------+
| id | title | sku | current_price | last_price_update | difference |
+----+----------------+--------+---------------+-------------------+------------+
| 1 | Product Name 1 | 654987 | 6.99 | 08-09-2021 | 2.5 |
| 2 | Product Name 2 | 548879 | 1.99 | 06-09-2021 | 0 |
| 3 | Product Name 3 | 541685 | 6.99 | 08-09-2021 | -1 |
+----+----------------+--------+---------------+-------------------+------------+
我的问题是我也想要一个差异列 returned,它显示最新价格与之前价格之间的差异。
我尝试了十亿种东西,但 SQL 不是我的菜,我要么 return 完整的价格清单,要么什么都不做。
使用 MariaDB 版本:10.3.30
谢谢, 阿什莉
您可以使用row_number()
枚举价格,然后条件聚合得到两个价格:
select p.*, pr.current_price, pr.previous_price,
(pr.current_price - pr.previous_price) as diff
from products p join
(select product_id,
max(case when seqnum = 1 then price end) as current_price,
max(case when seqnum = 2 then price end) as previous_price
from (select pr.*,
row_number() over (partition by product_id order by date desc) as seqnum
from pricing pr
) pr
where seqnum <= 2
group by product_id
) pr
on pr.product_id = p.id;