Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL SUM ALL ROWS BY ID

I am trying to learn SQL queries and have this scenario where I have this table:

Table1

       ID | Name | Hour
       ----------------
        1 | Mark | 2
        2 | ken  | 1.5
        3 | jake | 3
        1 | Mark | 1.8
        2 | ken  | 1

Expected result

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

       ID | Name | Hour
       ----------------
        1 | Mark | 3.8
        2 | ken  | 2.5
        3 | jake | 3
   

I have tried to use the sum() function but I get an error.

My query:

Select ID, Name, Sum(Hour) 
From Table1 
Where ID = ID

Response:

  1. Kindly use Group by clause whenever the Aggregate functions (min(),max(),sum(),count(),…etc.,) and columns are used together.

  2. Non aggregated columns present in SELECT columns should be used in GROUP BY clause.

>Solution :

For using aggregate function you need to use Group By like this:

 Select ID, Name , Sum(Hour) AS Hour From Table1
 Group By ID, Name
 Order By ID
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading