需要在sql中查找特定于最小和最大日期的状态列值

kd3sttzy  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(487)

下表产品:

+--------------+-----------+--------+-----------+
| Product_code | Prod Name | Status |   Date    |
+--------------+-----------+--------+-----------+
| A            | AA1       | New    | 12/3/2019 |
| A            | AA2       | Expiry | 7/20/2017 |
+--------------+-----------+--------+-----------+

预期产量:

+--------------+-----------+------------+-----------+------------+------------+
| Product_code | Min_date  | Min_status | Max_date  | Max_status | count_Prod |
+--------------+-----------+------------+-----------+------------+------------+
| A            | 7/20/2017 | Expiry     | 12/3/2019 | New        |          2 |
+--------------+-----------+------------+-----------+------------+------------+

尝试以下查询

SELECT Product_code,MIN(Date) as "Min_date", MAX(Date) as 
"Max_date",count(Prod_Name) as "count_Prod"
FROM Product where Product_code ='A'  
GROUP BY Product_code
ORDER BY Product_code;

需要帮助来获取日期列的特定状态。

gywdnpxw

gywdnpxw1#

请使用下面的查询。您也可以对字符串列使用min和max函数。它根据字母顺序进行聚合。

SELECT Product_code,MIN(Date) as "Min_date", MIN(Status) as Min_Status,  MAX(Date) as 
"Max_date", MAX(Status) as Max_Status,count(Prod_Name) as "count_Prod"
FROM Product where Product_code ='A'  
GROUP BY Product_code
ORDER BY Product_code;

相关问题