简体   繁体   English

sql 查询获取两个产品每个月的第一个和最后一个记录

[英]sql query to fetch first and last records of each month for two products

hello guys please i'm writing a query that can give me the first and last records of each month which is done but the case is i'm retrieving data for two products using the code below大家好,我正在写一个查询,可以给我每个月的第一个和最后一个记录,但情况是我正在使用下面的代码检索两个产品的数据

SELECT u1.product, 
       v.month,
       v.mindt,
       u1.opens,
       v.maxdt,
       u2.closingStockvalue 
FROM ( SELECT month, MIN(date) mindt, MAX(date) maxdt 
       FROM closingstock 
       where product='PMS' 
       GROUP BY month) v 
JOIN closingstock u1 ON u1.date=v.mindt 
JOIN closingstock u2 ON u2.date=v.maxdt;

but the result is in the screen shot below and i want only the highlighted result please any solution for that the screenshot is below enter image description here但结果在下面的屏幕截图中,我只想要突出显示的结果,请为下面的屏幕截图提供任何解决方案,在此处输入图片描述

SELECT product, 
       v.month,
       v.mindt,
       u1.opens,
       v.maxdt,
       u2.closingStockvalue 
FROM ( SELECT product, `month`, MIN(`date`) mindt, MAX(`date`) maxdt 
       FROM closingstock 
       WHERE product IN ('Product 1', 'Product 2')  -- the list of needed products
       GROUP BY product, `month`) v 
JOIN closingstock u1 USING (product)
JOIN closingstock u2 USING (product)
WHERE u1.`date`=v.mindt
  AND u2.`date`=v.maxdt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM