gpt4 book ai didi

Mysql 使用 join 统计案例

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

我正在尝试创建一个简单的选择语句,但似乎无法正确执行。我有三张 table 。这是架构。

CREATE TABLE IF NOT EXISTS `SalesRepDealer` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`DealerId` varchar(20) NOT NULL,
`DealerName` varchar(50) NOT NULL,
`SalesRepId` varchar(30) NOT NULL,
`SalesRepName` varchar(30) NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=629 ;

CREATE TABLE IF NOT EXISTS `PriorYearBoatOrders` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`DealerId` varchar(20) NOT NULL,
`PriorYearSalesTotal` int(11) NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2234 ;

CREATE TABLE IF NOT EXISTS `BoatOrdersCurrentYear` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`DealerId` varchar(40) COLLATE utf8_bin NOT NULL,
`DealerName` varchar(50) COLLATE utf8_bin NOT NULL,
`OrderNum` varchar(10) COLLATE utf8_bin NOT NULL,
`BoatDescription` varchar(50) COLLATE utf8_bin NOT NULL,
`SerialNum` varchar(50) COLLATE utf8_bin NOT NULL,
`CompleteDate` varchar(15) COLLATE utf8_bin DEFAULT NULL,
`ShipDate` varchar(15) COLLATE utf8_bin DEFAULT NULL,
`ShipBy` varchar(15) COLLATE utf8_bin DEFAULT NULL,
`Invoice` varchar(15) COLLATE utf8_bin DEFAULT NULL,
`DocUnitPrice` int(11) DEFAULT NULL,
`Customer` varchar(50) COLLATE utf8_bin DEFAULT NULL,
`Po` varchar(30) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=8192 ;

这是我的初始查询,用于计算未结订单和已发货订单的数量,并按销售代表 ID 筛选列表。这工作得很好,并给了我这个结果。

SELECT DealerId, DealerName, sum(case when ShipDate = '' then 1 else 0 end)  AS NumOpenOrders, sum(case when ShipDate != '' then 1 else 0 end)  AS NumShipped FROM BoatOrdersCurrentYear WHERE DealerId IN (SELECT DealerID FROM SalesRepDealer WHERE SalesRepId = 'bba') Group By DealerName

DealerId DealerName NumOpenOrders NumShipped
BACA082017 Antler's Resort & Marina 0 5
CDGWAZ001 Got Water Marine 7 31
CDORHI014 HILLYERS SANTIAM MOTOR SPORTS 10 9
CDCAJM317 JOHNSON MARINE SUPPLIES INC 4 5
CDCABA0117 KDME, INC 4 0
CDIAMM015 Mark's Marine, Inc. 0 16
CDCANE001 Newport Pontoons, Inc. 0 1
CDIDPR043 Prestige Water Sports 15 4

现在我只想添加上一年的总数。我尝试了连接和其他选择组合,但计数搞砸了并开始变得不稳定。我只需要从上面的查询中获取经销商 ID 并将其与 PriorYearBoatOrders 进行匹配并返回 PriorYearSalesTotal。但这是我一直在尝试的查询和结果。

SELECT BoatOrdersCurrentYear.DealerId, BoatOrdersCurrentYear.DealerName, sum(case when BoatOrdersCurrentYear.ShipDate = '' then 1 else 0 end)  AS NumOpenOrders, sum(case when BoatOrdersCurrentYear.ShipDate != '' then 1 else 0 end)  AS NumShipped, PriorYearBoatOrders.PriorYearSalesTotal FROM BoatOrdersCurrentYear, PriorYearBoatOrders  WHERE BoatOrdersCurrentYear.DealerId IN (SELECT DealerId FROM SalesRepDealer WHERE SalesRepId = 'bba')Group By DealerName

DealerId DealerName NumOpenOrders NumShipped PriorYearSalesTotal
BACA082017 Antler's Resort & Marina 0 11165 5
CDGWAZ001 Got Water Marine 15631 69223 5
CDORHI014 HILLYERS SANTIAM MOTOR SPORTS 22330 20097 5
CDCAJM317 JOHNSON MARINE SUPPLIES INC 8932 11165 5
CDCABA0117 KDME, INC 8932 0 5
CDIAMM015 Mark's Marine, Inc. 0 35728 5
CDCANE001 Newport Pontoons, Inc. 0 2233 5
CDIDPR043 Prestige Water Sports 33495 8932 5

我不确定发生了什么以及为什么总数相差如此之远。但我对查询写作还很陌生。感谢任何帮助,对于格式不正确的架构和查询表示抱歉。

最佳答案

对于每个经销商,您都将今年的数据与去年的每个经销商的数据连接起来 - 为您提供交叉连接;您需要添加与 DealerId 匹配的连接条件。

但是您还需要从 DealeId 列表开始,否则即使有 DealerId 上的条件,您也会交叉连接订单。

SELECT
d.DealerId,
cy.DealerName,
sum(case when cy.ShipDate = '' then 1 else 0 end) AS NumOpenOrders,
sum(case when cy.ShipDate != '' then 1 else 0 end) AS NumShipped,
py.PriorYearSalesTotal
FROM (SELECT distinct DealerId FROM SalesRepDealer WHERE SalesRepId = 'bba') d
JOIN BoatOrdersCurrentYear cy on cy.DealerId = d.DealerId
LEFT JOIN PriorYearBoatOrder py on py.DealerId = d.DealerId
group by 1,2

注意使用:

  • 使用别名来提高可读性
  • “现代”(自 1992 年起)连接语法
  • 用于生成 DealerId 列表的子查询
  • 如果去年没有销售,则左连接返回 null

关于Mysql 使用 join 统计案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50236784/

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