用于求和函数的SQL命令 [英] Sql command for sum function
本文介绍了用于求和函数的SQL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实际上我对sql sum函数一无所知.请帮助我如何编写此命令.
Actually i have no idea about sql sum function. please help me how to write this command.
SELECT B.cat_name as [cat_name],B.articles_count as [articles_count], B.cat_ID as [cat_ID],SUM(A.article_visit) AS [Article_Visit] FROM article_list A LEFT JOIN category B ON A.cat_ID = B.cat_ID WHERE A.article_status = 1 AND B.article_allow = 1 GROUP BY CUBE (B.cat_name,B.articles_count, B.cat_ID);
在此先感谢
我之前的命令看到了这样的输出
catId名称访问
1个帐户100
1个帐户50
但我想做:
catId名称访问
1个帐户150
错误是:
Thanks in advance
My previous command saw output like this
catId Name visit
1 Accounts 100
1 Accounts 50
But i want to do:
catId Name visit
1 Accounts 150
Error is:
error:
Column 'category.cat_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
推荐答案
您需要添加一个分组依据 [
You need to add a group by[^] clause.
尝试在下面修改FROM
子句查询
Try to modify theFROM
clause in the below query
DECLARE @tmpTable as TABLE (ID INT, Description VARCHAR(10), Amount INT)
INSERT INTO @tmpTable (ID, Description,Amount) VALUES(1,'Amount', 100)
INSERT INTO @tmpTable (ID, Description,Amount) VALUES(1,'Amount', 50)
SELECT * FROM @tmpTable
--(1 row(s) affected)
--ID Description Amount
------------- ----------- -----------
--1 Amount 100
--1 Amount 50
SELECT
ID,
Description,
SUM(Amount)AS Amount
FROM
@tmpTable
GROUP BY
ID,
Description
--ID Description
------------- ----------- -----------
--1 Amount 150
这篇关于用于求和函数的SQL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文