带连接的Mysql计数案例

时间:2018-05-08 15:05:49

标签: mysql

我试图创建一个简单的选择语句,似乎无法做到正确。我有三张桌子。这是架构。

.md-button {
    background-color: #fff!important;
}

这是我的初始查询,用于计算未结订单和已发货订单,并按销售代表ID过滤列表。这很有效,并且给了我这个结果。

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 ;

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

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

我不确定发生了什么,以及为什么总数太远了。但我对查询的写作非常陌生。感谢任何帮助,对于格式不良的架构和查询感到抱歉。

1 个答案:

答案 0 :(得分:0)

对于每个经销商,您将加入今年的数据与去年的每个经销商数据 - 给您一个交叉加入;你需要在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

请注意使用:

  • 别名以提高可读性
  • “modern”(自1992年起)加入语法
  • 子查询生成dealerIds列表
  • 左连接返回null是去年没有销售