MySQL查询中的几个最大

时间:2018-09-23 05:25:26

标签: mysql

我遇到了一个我无法解决的问题。 我有下表:

TABLE Sales (
  ClientCode varchar(20) NOT NULL,
  DocumentDate date NOT NULL,
  ItemCode varchar(20) DEFAULT NULL,
  ItemName varchar(500) DEFAULT NULL,
  Quantity decimal(10,2) DEFAULT NULL,
  Price decimal(12,2) DEFAULT NULL,
  InvoiceType varchar(9) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

所有销售额都记录在此表中。

有3种类型的发票(InvoiceType)。我需要一个给出类似结果的查询

ClientCode, ItemCode, ItemName, LastDateofSaleforInvoiceA, LastSalePriceforInvoiceA, LastDateofSaleforInvoiceB, LastSalePriceforInvoiceB, LastDateofSaleforInvoiceC, LastSalePriceforInvoiceC 

我不知道我是否清楚地解释了这一点,但是我需要一行包含我们每种发票类型的customerCode,ItemCode,ItemName以及最后日期和最后价格。

源数据如下:

+------------+--------------+----------+----------+----------+-------+-------------+

| ClientCode | DocumentDate | ItemCode | ItemName | Quantity | Price | InvoiceType |

+------------+--------------+----------+----------+----------+-------+-------------+

|      00001 | 2018-10-01   |    00001 | WidgetA  |      500 | 5.00  | Internal    |

|      00002 | 2018-09-27   |    00005 | WidgetB  |      100 | 1.50  | External    |

|      00001 | 2017-09-23   |    00001 | WidgetA  |      150 | 2.25  | External    |

|      00002 | 2016-03-03   |    00005 | WidgetB  |      360 | 5.99  | Internal    |

|      00001 | 2013-03-03   |    00001 | WidgetA  |      600 | 0.99  | Export      |    
+------------+--------------+----------+----------+----------+-------+-------------+

我需要的是这样的

+------------+----------+------------------+---------------+------------------+---------------+----------------+-------------+
| ClientCode | ItemCode | LastDateInternal | PriceInternal | LastDateExternal | PriceExternal | LastDateExport | PriceExport |
+------------+----------+------------------+---------------+------------------+---------------+----------------+-------------+
|      00001 |    00001 | 2018-10-01       | 5.00          | 2017-09-23       | 2.25          | 2013-03-03     | 0.99        |
|      00002 |    00005 | 2016-03-03       | 5.99          | 2018-09-27       | 1.50          |                |             |
+------------+----------+------------------+---------------+------------------+---------------+----------------+-------------+

感谢所有帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用子查询来查找按Client和InvoceType分组的max(DocumentDate) 那么您可以针对不同的invoceType每次将此结果与销售额结合3次

select a.ClientCode
    , a.ItemCode
    , a.ItemName
    , a.DocumentDate as LastDateofSaleforInvoiceA
    , a.Price as LastSalePriceforInvoiceA
    , b.DocumentDate as LastDateofSaleforInvoiceB
    , b.Price as  LastSalePriceforInvoiceB
    , b.DocumentDate as LastDateofSaleforInvoiceC
    , b.PriceC as LastSalePriceforInvoiceC 
from (
    select max(DocumentDate) max_date, InvoiceType, ClientCode
    from Sales 
    group by InvoiceType, cliendtCode
) t 
inner join sales a on t.ClientCode = a.ClientCode
    AND t.max_date = a.DocumentDate
        AND t.InvoiceType = a.InvoceType
inner join sales b on t.ClientCode = a.ClientCode
    AND t.max_date = b.DocumentDate
        AND t.InvoiceType = b.InvoceType
inner join sales c on t.ClientCode = a.ClientCode
    AND t.max_date = c.DocumentDate
        AND t.InvoiceType = c.InvoceType

答案 1 :(得分:0)

我认为DocumentDate是日期时间,并且实际上是唯一的(一次售出一次) 我还假设ItemCode和ItemName之间的关系是1to1

需要嵌套查询才能获取最后日期,而需要联接才能获取相应价格

    SELECT
    last.ClientCode, last.ItemCode, last.ItemName, last.InvoiceType, MAX(If(InvoiceType = 'A', s.LastDate, 0)) as LastDateofSaleforInvoiceA, s.Price AS  LastSalePriceforInvoiceA
    # The same for B and C
    (
    SELECT
    ClientCode, ItemCode, ItemName, InvoiceType, Price, MAX(DocumentDate) as LastDate
    FROM
     sales
    GROUP BY
    ClientCode, ItemCode, InvoiceType
    ) last
    INNER JOIN sales s ON last.ClientCode = s.ClientCode AND last.ItemCode = s.ItemCode AND last.InvoiceType = s.InvoiceType AND last.LastDate = s.DocumentDate

GROUP BY
last.ClientCode, last.ItemCode, last.InvoiceType