gpt4 book ai didi

mysql - mysql如何比较和求和单个表中的不同列

转载 作者:行者123 更新时间:2023-11-29 00:03:15 25 4
gpt4 key购买 nike

I have a table with some duplicate stock ids and release date.The table table data is like below.

|STOCK_ID |QTY| release_date|
-----------------------------------
|4046228009711 |12 | 25.02.2015 |
|4046228009711 |3 | 21.12.2014 |
|4046228009711 |13 | 21.12.2014 |
|4046228009711 |5 | 21.12.2014 |
------------------------------------

Now i want to sum the quantity of same stock id and same released_date and segregate records based on the released_date.
So the out put should be like this.This is the expected output

|STOCK_ID |QTY| release_date|
-----------------------------------
|4046228009711 |12 | 25.02.2015 |
|4046228009711 |21 | 21.12.2014 |
-----------------------------------

为了实现这一点,我尝试了以下查询。 为此,我在临时表中插入了相同的数据。

SELECT t1.STOCK_ID,t1.`released_date
(SELECT SUM(Qty) FROM
table1 t1
WHERE h1.STOCK_ID = t2.STOCK_ID AND t1.released_date = t2.released_date
) AS 'Bestand'
FROM table1 t1
JOIN table2 t2 ON t1.STOCK_ID = t2.STOCK_ID
GROUP BY t1.STOCK_ID

But i am getting the below output

|STOCK_ID |QTY| release_date|
-----------------------------------
|4046228009711 |12 | 25.02.2015 |
-----------------------------------

任何人都可以帮助我在哪里做错了吗?任何帮助将不胜感激。

最佳答案

可以这样做

select 
STOCK_ID,
sum(QTY) as QTY,
release_date
from table_name
group by STOCK_ID,release_date

关于mysql - mysql如何比较和求和单个表中的不同列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28737914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com