gpt4 book ai didi

php - 如何在 1 个查询中合并 3 个表

转载 作者:行者123 更新时间:2023-11-29 18:40:40 25 4
gpt4 key购买 nike

我需要将 3 件事合并到 1 个查询中以在图表上表示它。

第一个查询是查找 NetAmount2017第二个问题是寻找预算(2017)第三个查询是查找 NetAmount2016

第一个查询:NetAmount2017

SELECT sum(a.netamt) as netamt2017, b.store_name 
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum
WHERE a.busidate >= '2017-01-01' AND a.busidate <='2017-04-30'
GROUP BY a.storenum

第二个查询:Budget2017

SELECT
SUM(CASE WHEN c.busidate BETWEEN '2017-01' AND '2017-04' THEN c.budget ELSE
0 END) as budget,
b.store_name
FROM site_kpimthslsbgt c JOIN site_store b ON b.storenum = c.storenum
GROUP BY c.storenum

第三个查询:NetAmount2016(与第一个查询相同,但日期不同)

SELECT sum(a.netamt) as netamt2017, b.store_name 
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum
WHERE a.busidate >= '2016-01-01' AND a.busidate <='2016-04-30'
GROUP BY a.storenum

据我所知,我只能组合 2 个查询,不知道组合 3 个查询。这是我的 2017 年预算和 2017 年 Netty 的组合。

 Select x.netamt, y.budget, y.store_name
from
(
SELECT sum(a.netamt) as netamt, b.store_name
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum
WHERE a.busidate >= '2017-01-01' AND a.busidate <='2017-04-30'
GROUP BY a.storenum
) x
inner join
(
SELECT
SUM(CASE WHEN c.busidate BETWEEN '2017-01' AND '2017-04' THEN c.budget
ELSE 0 END) as budget,
b.store_name
FROM site_kpimthslsbgt c JOIN site_store b ON b.storenum = c.storenum
GROUP BY c.storenum
) y
on x.store_name = y.store_name

我想要的结果应该显示在 1 个图表中,该图表由 3 个条形图组成,分别是 Netamount2017、Budget2017 和 Netamount2016。下图显示了结果,但是2017年的预算只显示了1个月,而不是我选择的1月到4月的所选月份。这是我迄今为止得到的组合 3 个查询的查询

SELECT
SUM(CASE WHEN a.busidate BETWEEN '2016-01-01' AND '2016-04-30' THEN a.netamt
ELSE 0 END) as netamt2016,
SUM(CASE WHEN a.busidate BETWEEN '2017-01-01' AND '2017-04-30' THEN a.netamt ELSE 0
END) as netamt,
b.store_name,
c.budget
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum
JOIN site_kpimthslsbgt c ON b.storenum = c.storenum
WHERE c.busidate = '2017-01' AND b.store_region='$store_region'
GROUP BY a.storenum

如果我执行下面的查询,如果给我错误的计算。

SELECT
SUM(CASE WHEN a.busidate BETWEEN '2016-01-01' AND '2016-04-30' THEN a.netamt
ELSE 0 END) as netamt2016,
SUM(CASE WHEN a.busidate BETWEEN '2017-01-01' AND '2017-04-30' THEN a.netamt ELSE 0
END) as netamt,
b.store_name,
c.budget
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum
JOIN site_kpimthslsbgt c ON b.storenum = c.storenum
WHERE c.busidate BETWEEN '2017-01' AND '2017-04' AND b.store_region='$store_region'
GROUP BY a.storenum

Wrong

最佳答案

感谢上帝!我找到了答案,将所有查询合并为 1 个查询。这是代码:

 SELECT x.netamt2017,z.netamt2016, y.budget, y.store_name
FROM
(
SELECT sum(a.netamt) as netamt2017, b.store_name
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum
WHERE a.busidate >= '$date1' AND a.busidate <='$date2' AND
a.storenum='$storenum'
GROUP BY a.storenum
) x

inner join
(
SELECT
SUM(CASE WHEN c.busidate BETWEEN '$date111' AND '$date211' THEN c.budget
ELSE 0 END) as budget,
b.store_name
FROM site_kpimthslsbgt c JOIN site_store b ON b.storenum = c.storenum
WHERE c.storenum='$storenum'
GROUP BY c.storenum
) y
on x.store_name = y.store_name
INNER JOIN
(
SELECT sum(a.netamt) as netamt2016, b.store_name
FROM site_sales a JOIN site_store b ON b.storenum = a.storenum
WHERE a.busidate >= '$date1234' AND a.busidate <='$date2234' AND
a.storenum='$storenum'
GROUP BY a.storenum
) z
on y.store_name = z.store_name

Here is the final result

关于php - 如何在 1 个查询中合并 3 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44967318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com