我有一个包含st_id
列的表格。
每列都有amount.eg jan_1,feb_1
有两行。一个用于jan_2,feb_2
数据,另一个用于SELECT st_id,SUM(jan_1+feb_1) as amt1,SUM(jan_2+feb_2) as amt2 FROM
fee_amount WHERE `st_id` IN (5,7,8);
数量。
我想要每个id的一行。现在我只得到了所有金额的一行。
查询:
autoHighlight="true"
答案 0 :(得分:1)
您需要GROUP BY st_id
。因此,您将获得每个st_id
的每一行。
SELECT
st_id,
SUM(jan_1+feb_1) as amt1,
SUM(jan_2+feb_2) as amt2
FROM fee_amount WHERE `st_id` IN (5,7,8)
GROUP BY `st_id`;
注意:没有Group By
的聚合函数会将结果集折叠为一行。
答案 1 :(得分:0)
在您的查询中,最后只添加GROUP BY
子句( GROUP BY st_id
)。
SELECT
st_id,
SUM(jan_1+feb_1) as amt1,
SUM(jan_2+feb_2) as amt2
FROM fee_amount
WHERE `st_id` IN (5,7,8)
GROUP BY `st_id`;
答案 2 :(得分:0)
合理的方法是将(jan_1 + jan2)求和为Jan,同时对feb数据进行相同的处理。
以下是查询:
SELECT
st_id,
SUM(jan_1 + jan_2) AS jan,
SUM(feb_1 + feb_2) AS feb
FROM
fee_amount
WHERE
`st_id` IN (5,7,8)
GROUP BY
st_id;
答案 3 :(得分:0)
SELECT st_id,
Sum(jan_1 + feb_1) AS amt1,
Sum(jan_2 + feb_2) AS amt2
FROM fee_amount
WHERE ` st_id ` IN ( 5, 7, 8 )
GROUP BY ` st_id ` ;