+ 1
Explain this...table name is Employee
Empid name 1 A 2 B Select *,sum(empid) from Employee; What will be the output and explain how? The output I got is 1 A 3
8 Answers
+ 3
Did you get only one row in result set? I see the table contains two rows. Never seen a * followed by column selection before TBH.
SUM() function returns the sum of `Empid` column, I think SUM() would return 3 instead of 2, since the `Empid` value was 1 and 2.
COUNT() function however, will return 2 as it is counting rather than summing.
+ 1
Ipang I want all records with count how to achieve this
+ 1
Ipang help me
+ 1
Ipang I just want to display all records and it's counts in a query
+ 1
IMO this depends on what you are doing with the result set.
if it's going back to a program, you can easily count the results in the program code, there's probably a function in the database library your code uses that will do it for you.
otherwise I don't understand why you want the count of the records on each result. if you are imagining you can get a result set that has all the found results and then an additional result that just contains sum(empid), I don't think you can do that. I would agree with Ipang and say you need 2 queries.
+ 1
Hello you can use group by name so it will give you all records with empid sum. Or you can use count if you want to count of records.
Select name,sum(empid) from Employee
Group by name
0
C Lover
Can you tell me what you want to do with this table? I think there's no point in repeating a SUM or COUNT with selection for all records.
Perhaps separate into two queries? one for selecting all records, and another just to count the records. But I'm confused about the need to sum or count the `Empid` column.
Usually SUM or COUNT is used with a condition (WHERE clause) we don't usually use those functions with primary key column (such as `Empid`). This is why I hesitate to suggest something.
0
C Lover
I guess separate queries is the option. Select all records and then select SUM or COUNT. I don't think it's good idea mixing the results of all records with result of SUM or COUNT.
You can wait for others to give a second opinion, but IMHO it's better to separate the queries.