Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询中的几个max_Mysql - Fatal编程技术网

MySQL查询中的几个max

MySQL查询中的几个max,mysql,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,

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

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          |                |             |
+------------+----------+------------------+---------------+------------------+---------------+----------------+-------------+
我需要的是这样的东西:

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

| 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          |                |             |
+------------+----------+------------------+---------------+------------------+---------------+----------------+-------------+

非常感谢您的帮助。

您可以使用子查询查找按客户端和InvoceType分组的maxDocumentDate 然后,对于不同的invoceType,您可以每次将此结果与sales关联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

我假设DocumentDate是datetime,并且实际上是唯一的一次销售 我还假设ItemCode和ItemName之间的关系是1到1

需要一个嵌套查询来获取最后一个日期,需要一个联接来获取相应的价格

    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

什么版本的MySQL?有一个你尝试过的select查询不好吗?InvoiceType允许的值是什么?您需要提供一些示例数据和预期输出请看我使用的是mySQL 5.7。我尝试了许多select语句,但都没有给出我想要的结果。我发现mySQL服务器已关闭,我必须重试。@草莓。感谢您的建议,在分组中添加了clientcode by