带join的mysql count case

i34xakig  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(378)

我试图创建一个简单的select语句,但似乎无法正确执行。我有三张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

我不知道发生了什么,为什么总数相差这么远。但我对质疑写作还很陌生。感谢您的帮助,对于格式错误的模式和查询,我们深表歉意。

wnavrhmk

wnavrhmk1#

对于每个经销商,您都将今年的数据与去年的每个经销商的数据合并在一起,这样您就可以交叉连接;您需要在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列表
left join返回null表示去年没有销售

相关问题