Click here to Skip to main content
15,171,439 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
Hello Experts,

I came across this website during my online search for the above question.
http://www.w3resource.com/sql/aggregate-functions/sum-and-count-using-variable.php[^]

The very first statement in the website says,
To get sum of total number of records in 'customer' table, the following SQL statement can be used


Why can't we use select count(*) from Tablename to get the total number of records?
Basically I didn't the difference of this particular example in the url. Can someone help me with this?

Or can someone tell me how different can we write a query for below but it should return same resultset for both the queries

SQL
select icitem,icdsc1,icdsc2, sum(ifqoh) as onhand, sum(ifqcm) as commit from TESTDATA.VINITMB join TESTDATA.VINITEM on ifcomp=1 and ifitem=icitem and ifdel='A'
where icitem like 'VP%' group by icitem, icdsc1, icdsc2
having sum(ifqoh)=0 order by icitem;
Posted
Updated 29-Jun-15 4:47am
v2
Comments
Afzaal Ahmad Zeeshan 29-Jun-15 10:05am
   
COUNT would count the records and give you the result, SUM is used to return the SUM of the values in records.

1 solution

It's easy:

You can count anything. You can even ensure its distinct like Count(distinct status)

you can only sum a numerical value. sum adds each row together.

If you had a column with 1,1,1,1,2,2, then:

SQL
Count(col) = 6
Count(Distinct col) = 2
Sum(col) = 8



Hope that helps

Andy ^_^
   
Comments
sudevsu 29-Jun-15 16:44pm
   
Thanks

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