MYSQL-无法通过2列分组正确连接

时间:2018-09-13 03:43:11

标签: mysql sql

我有3个表以2列分组进行2个查询。但是当我尝试连接(左/内部)查询1和查询2时,它导致我重复并且超出了预期的行数。以前我以1列为一组的方式加入查询没有问题。数据库很大,所以我离开了Here

查询1(42条记录):

SELECT custlist.Custcat as cn,itmnocate.GradeCategory as gc,custlist.Custtype ct,
       SUM(CASE WHEN MONTH(date1) in (1,2,3)  THEN dt2.quantity/1000 ELSE NULL END) AS Q1,
       SUM(CASE WHEN MONTH(date1) in (4,5,6)  THEN dt2.quantity/1000 ELSE NULL END) AS Q2,
       SUM(CASE WHEN MONTH(date1) in (7,8,9)  THEN dt2.quantity/1000 ELSE NULL END) AS Q3,
       SUM(CASE WHEN MONTH(date1) in (10,11,12)  THEN dt2.quantity/1000 ELSE NULL END) AS Q4
FROM (
SELECT 
    date1,region,quantity,unit,customeracc,location,item,
    (CASE WHEN (data2.region = 'central' OR data2.region = 'eastern' OR data2.region = 'northern' OR data2.region = 'southern' )
    THEN CONCAT('PENINSULAR',' ',data2.customeracc)
    WHEN (data2.region = 'kota kinabalu' OR data2.region = 'lahad datu' OR data2.region = 'sandakan' OR data2.region = 'tawau' )
    THEN CONCAT('SABAH',' ',data2.customeracc)
    ELSE concat('SARAWAK',' ',data2.customeracc) END) vlookrefdata 
FROM data2
) dt2
  INNER JOIN custlist on dt2.vlookrefdata = custlist.Vlookref 
  INNER JOIN itmnocate ON dt2.item = itmnocate.ItemNumber
WHERE
     date1 >= DATE_FORMAT('2018-06-11', '%Y-01-01')- INTERVAL 1 YEAR AND date1 <= DATE_FORMAT('2018-06-11', '%Y-12-31') - INTERVAL 1 YEAR
  AND
   dt2.unit = 'KG'
  AND
   dt2.customeracc not in (select Customeraccount from custlist WHERE Custcat = 'bcsb')  
  AND
   custlist.Custtype = 'JV Cos'
  AND
   custlist.Area = 'PENINSULAR' 
GROUP BY itmnocate.GradeCategory,custlist.Custcat

查询2(37条记录):

SELECT custlist.Custcat as cn,itmnocate.GradeCategory as gc,custlist.Custtype ct,
       SUM(CASE WHEN MONTH(date1) in (1,2,3)  THEN dt2.quantity/1000 ELSE NULL END) AS Q1,
       SUM(CASE WHEN MONTH(date1) in (4,5,6)  THEN dt2.quantity/1000 ELSE NULL END) AS Q2,
       SUM(CASE WHEN MONTH(date1) in (7,8,9)  THEN dt2.quantity/1000 ELSE NULL END) AS Q3,
       SUM(CASE WHEN MONTH(date1) in (10,11,12)  THEN dt2.quantity/1000 ELSE NULL END) AS Q4
FROM (
SELECT 
    date1,region,quantity,unit,customeracc,location,item,
    (CASE WHEN (data2.region = 'central' OR data2.region = 'eastern' OR data2.region = 'northern' OR data2.region = 'southern' )
    THEN CONCAT('PENINSULAR',' ',data2.customeracc)
    WHEN (data2.region = 'kota kinabalu' OR data2.region = 'lahad datu' OR data2.region = 'sandakan' OR data2.region = 'tawau' )
    THEN CONCAT('SABAH',' ',data2.customeracc)
    ELSE concat('SARAWAK',' ',data2.customeracc) END) vlookrefdata 
FROM data2
) dt2
  INNER JOIN custlist on dt2.vlookrefdata = custlist.Vlookref 
  INNER JOIN itmnocate ON dt2.item = itmnocate.ItemNumber
WHERE
   date1 >= DATE_FORMAT('2018-06-11', '%Y-01-01') AND month(date1) <= month('2018-06-11')
  AND
   dt2.unit = 'KG'
  AND
   dt2.customeracc not in (select Customeraccount from custlist WHERE Custcat = 'bcsb')  
  AND
   custlist.Custtype = 'JV Cos'
  AND
   custlist.Area = 'PENINSULAR' 

GROUP BY itmnocate.GradeCategory,custlist.Custcat

查询1&2联接(253条记录):

SELECT a.q1,a.q2,a.q3,a.q4,a.cn,a.gc,a.ct,b.q1,b.q2,b.q3,b.q4
FROM
(
SELECT custlist.Custcat as cn,itmnocate.GradeCategory as gc,custlist.Custtype ct,
       SUM(CASE WHEN MONTH(date1) in (1,2,3)  THEN dt2.quantity/1000 ELSE NULL END) AS Q1,
       SUM(CASE WHEN MONTH(date1) in (4,5,6)  THEN dt2.quantity/1000 ELSE NULL END) AS Q2,
       SUM(CASE WHEN MONTH(date1) in (7,8,9)  THEN dt2.quantity/1000 ELSE NULL END) AS Q3,
       SUM(CASE WHEN MONTH(date1) in (10,11,12)  THEN dt2.quantity/1000 ELSE NULL END) AS Q4
FROM (
SELECT 
    date1,region,quantity,unit,customeracc,location,item,
    (CASE WHEN (data2.region = 'central' OR data2.region = 'eastern' OR data2.region = 'northern' OR data2.region = 'southern' )
    THEN CONCAT('PENINSULAR',' ',data2.customeracc)
    WHEN (data2.region = 'kota kinabalu' OR data2.region = 'lahad datu' OR data2.region = 'sandakan' OR data2.region = 'tawau' )
    THEN CONCAT('SABAH',' ',data2.customeracc)
    ELSE concat('SARAWAK',' ',data2.customeracc) END) vlookrefdata 
FROM data2
) dt2
  INNER JOIN custlist on dt2.vlookrefdata = custlist.Vlookref 
  INNER JOIN itmnocate ON dt2.item = itmnocate.ItemNumber
WHERE
     date1 >= DATE_FORMAT('2018-06-11', '%Y-01-01')- INTERVAL 1 YEAR AND date1 <= DATE_FORMAT('2018-06-11', '%Y-12-31') - INTERVAL 1 YEAR
  AND
   dt2.unit = 'KG'
  AND
   dt2.customeracc not in (select Customeraccount from custlist WHERE Custcat = 'bcsb')  
  AND
   custlist.Custtype = 'JV Cos'
  AND
   custlist.Area = 'PENINSULAR' 
GROUP BY itmnocate.GradeCategory,custlist.Custcat
)a
inner join
(
SELECT custlist.Custcat as cn,itmnocate.GradeCategory as gc,custlist.Custtype ct,
       SUM(CASE WHEN MONTH(date1) in (1,2,3)  THEN dt2.quantity/1000 ELSE NULL END) AS Q1,
       SUM(CASE WHEN MONTH(date1) in (4,5,6)  THEN dt2.quantity/1000 ELSE NULL END) AS Q2,
       SUM(CASE WHEN MONTH(date1) in (7,8,9)  THEN dt2.quantity/1000 ELSE NULL END) AS Q3,
       SUM(CASE WHEN MONTH(date1) in (10,11,12)  THEN dt2.quantity/1000 ELSE NULL END) AS Q4
FROM (
SELECT 
    date1,region,quantity,unit,customeracc,location,item,
    (CASE WHEN (data2.region = 'central' OR data2.region = 'eastern' OR data2.region = 'northern' OR data2.region = 'southern' )
    THEN CONCAT('PENINSULAR',' ',data2.customeracc)
    WHEN (data2.region = 'kota kinabalu' OR data2.region = 'lahad datu' OR data2.region = 'sandakan' OR data2.region = 'tawau' )
    THEN CONCAT('SABAH',' ',data2.customeracc)
    ELSE concat('SARAWAK',' ',data2.customeracc) END) vlookrefdata 
FROM data2
) dt2
  INNER JOIN custlist on dt2.vlookrefdata = custlist.Vlookref 
  INNER JOIN itmnocate ON dt2.item = itmnocate.ItemNumber
WHERE
   date1 >= DATE_FORMAT('2018-06-11', '%Y-01-01') AND month(date1) <= month('2018-06-11')
  AND
   dt2.unit = 'KG'
  AND
   dt2.customeracc not in (select Customeraccount from custlist WHERE Custcat = 'bcsb')  
  AND
   custlist.Custtype = 'JV Cos'
  AND
   custlist.Area = 'PENINSULAR'   
GROUP BY itmnocate.GradeCategory,custlist.Custcat
)b on a.cn=b.cn

1 个答案:

答案 0 :(得分:0)

在join子句中使用所有三列,例如a.cn = b.cn和a.ct = b.ct和a.gc = b.gc

computeStatsMean()