Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i have no idea about sql sum function. please help me how to write this command.

SQL
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);


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:
SQL
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.
Posted
Updated 20-Jun-11 19:19pm
v5

You need to add a group by[^] clause.
 
Share this answer
 
Comments
rahul dev123 20-Jun-11 9:06am    
i used this but shown error please help where i get wrong:
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);
R. Giskard Reventlov 20-Jun-11 9:42am    
Am I supposed to read your mind to figure out what the error is? Why have you added CUBE?
rahul dev123 21-Jun-11 1:12am    
if i remove CUBE then (,) are not accepted
R. Giskard Reventlov 21-Jun-11 3:14am    
Try this:
select
b.cat_name,
b.articles_count,
b.cat_id,
sum(a.article_visit) article_visit
from
article_list a inner join
category b on a.cat_id = b.cat_id
where
a.article_status = 1
and b.article_allow = 1
group by
b.cat_name,
b.articles_count,
b.cat_id
thatraja 20-Jun-11 11:13am    
Always include the error message in your question.
Try to modify the FROM clause in the below query

SQL
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900