sql 查询获取两个产品每个月的第一个和最后一个记录
sql query to fetch first and last records of each month for two products
大家好,我正在写一个查询,可以给我每个月的第一个和最后一个记录,但我正在使用下面的代码检索两个产品的数据
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;
但结果在下面的屏幕截图中,我只想要突出显示的结果,请为下面的屏幕截图提供任何解决方案
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;
大家好,我正在写一个查询,可以给我每个月的第一个和最后一个记录,但我正在使用下面的代码检索两个产品的数据
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;
但结果在下面的屏幕截图中,我只想要突出显示的结果,请为下面的屏幕截图提供任何解决方案 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;