如何根据最古老的日期分组和返回值?
因此,我有一个表:
n | id_product | name | price | quantity | created_at
-------------------------------------------------------------------------------
1 1da05150 pencil 800 10 31-12-2021
2 1da05150 pencil 700 7 15-01-2022
3 1da05150 pencil 1500 13 20-01-2022
4 510eac00 book 350 25 29-12-2021
我想按ID_Product列对它们进行分组。 在我想回来的列中,将是ID_Product,名称,ID_Product的数量总和,对于我想获得的价格列,我想获得来自最古老的产品的价格值(我希望它考虑考虑到为价格目的
而创建的_at列应该是这样的:
n | id_product | name | price | quantity | created_at
-------------------------------------------------------------------------------
1 1da05150 pencil 800 30 31-12-2021
2 510eac00 book 350 25 29-12-2021
So I have this table:
n | id_product | name | price | quantity | created_at
-------------------------------------------------------------------------------
1 1da05150 pencil 800 10 31-12-2021
2 1da05150 pencil 700 7 15-01-2022
3 1da05150 pencil 1500 13 20-01-2022
4 510eac00 book 350 25 29-12-2021
I'd like to group them by ID_PRODUCT column.
On the columns i'd like on return would be ID_PRODUCT, NAME, sum of quantities by id_product, and for the PRICE column i'd like to obtain the price value that comes from the oldest product (i'd like it to consider the CREATED_AT column for the price purpose)
On the result should be like this:
n | id_product | name | price | quantity | created_at
-------------------------------------------------------------------------------
1 1da05150 pencil 800 30 31-12-2021
2 510eac00 book 350 25 29-12-2021
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
subquery
与自己加入表您的数据
查询
you need
Subquery
to join table with itselfyour data
your query