Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following query now i need the sum of the count(*) values how can i do so

SQL
select a.col1 , count(*) count_1  , sum(count_1)
from table1 a join table2 on a.id = b.id
Posted
Updated 8-Aug-13 2:36am
v3
Comments
Maciej Los 8-Aug-13 8:36am    
Please, use text formatting.
Please, post example data and expected output.

I'm not sure what you want to achieve, but have a look at below example:

SQL
SELECT F1, COUNT(F1) AS CountOfF1, SUM(F2) SumOfF2
FROM (
	SELECT 1 AS F1, 2 AS F2
	UNION ALL SELECT 1, 5
	UNION ALL SELECT 1, 3
	UNION ALL SELECT 1, 4
	UNION ALL SELECT 2, 12
	UNION ALL SELECT 2, 5
	UNION ALL SELECT 2, 5
	UNION ALL SELECT 2, 15
) AS T
GROUP BY F1

SELECT COUNT(F1) AS DistinctF1Fields, SUM(Q.CountOfF1) AS SumOfCountOfF1
FROM ( 
	SELECT T.F1, COUNT(T.F1) AS CountOfF1, SUM(T.F2) SumOfF2
	FROM (
		SELECT 1 AS F1, 2 AS F2
		UNION ALL SELECT 1, 5
		UNION ALL SELECT 1, 3
		UNION ALL SELECT 1, 4
		UNION ALL SELECT 2, 12
		UNION ALL SELECT 2, 5
		UNION ALL SELECT 2, 5
		UNION ALL SELECT 2, 15
	) AS T
	GROUP BY T.F1
) AS Q


Result of 1. query:
1	4	14
2	4	37


Result of 2. query:
2	8
 
Share this answer
 
Comments
[no name] 8-Aug-13 9:50am    
+5 from me.
Maciej Los 8-Aug-13 16:01pm    
Thank you, sisir patro ;)
[no name] 9-Aug-13 3:21am    
Yw
gvprabu 8-Aug-13 11:09am    
Nice Example my 5+ :-)
Maciej Los 8-Aug-13 16:01pm    
Thank you, Gopal ;)
hi lalitkr

try this

SQL
select  count(*) count_1 
from table1 a join table2 on a.id = b.id
 
Share this answer
 
Comments
Maciej Los 8-Aug-13 8:42am    
Please, read a question carefully... and post proper solution.
lalitkr 8-Aug-13 12:29pm    
Well my table is like this
id Col1 col2
1 2 6
2 3 6
3 1 6

now i want the result for the col2 as sum(col1) values and this will be displayed in the col2


so for this i used the query as

select xx.id , xx.col1 , xx.sum(col1) col2 from
( select a.id , cout(*) col1 from
table1 a join table2 b on a.id = b.id ) xx

on doing so i m getting repeating sum for the col1 now i want the sum in first row and for rest of coloumn
it resturn null value

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