Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
May 25th, 2013, 09:51 AM
 cms9651-2
Registered User

Join Date: Apr 2013
Posts: 8
Time spent in forums: 6 h 44 m 28 sec
Reputation Power: 0
Sum of rows

Hi all, hope in your help.

This is my mysql table:
Code:
```+----+--------+--------+
| id | field1 | field2 |
+----+--------+--------+
|  1 | A1     | 7      |
|  2 | B1     | 9      |
|  3 | C1     | 0      |
|  4 | D1     | 3      |
|  5 | A2     | 5      |
|  6 | B2     | 6      |
|  7 | C2     | 7      |
|  8 | D2     | 8      |
+----+--------+--------+```

I need this output:
Code:
```+--------+--------------------+
| field1 | field2             |
+--------+--------------------+
| A2     | 19.230769230769200 |
+--------+--------------------+
| B2     | 23,076923076923100 |
+--------+--------------------+
| C2     | 26,923076923076900 |
+--------+--------------------+
| D2     | 30,769230769230800 |
+--------+--------------------+```

and tried this query where calculate the value of single field1 (5,6,7,8) divided by the sum of field2 equal to A1, B2, C2 and D2 (26) :
Code:
```A = 5/26 * 100 = 19
B = 6/26 * 100 = 23
C = 7/26 * 100 = 26
D = 8/26 * 100 = 30:
SELECT
field1,
field2/Sum(field2)*100 as field2
FROM
`tbl_t`
WHERE
1
AND field1 IN ('A2', 'B2', 'C2', 'D2');

+--------+--------------------+
| field1 | field2             |
+--------+--------------------+
| A2     | 19.230769230769234 |
+--------+--------------------+
1 row in set```

But the ouput is not what I want, can you help me?
Thank you
Any help would be greatly appreciated.

#2
May 25th, 2013, 01:56 PM
 r937
SQL Consultant

Join Date: Feb 2003
Posts: 26,811
Time spent in forums: 3 Months 1 Week 4 Days 7 h 31 m 5 sec
Reputation Power: 4208
Code:
```SELECT field1
, field2 /
( SELECT SUM(field2) * 100
FROM tbl_t
WHERE field1 IN ('A2', 'B2', 'C2', 'D2') ) AS field2
FROM tbl_t
WHERE field1 IN ('A2', 'B2', 'C2', 'D2')```
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

#3
May 25th, 2013, 05:54 PM
 cms9651-2
Registered User

Join Date: Apr 2013
Posts: 8
Time spent in forums: 6 h 44 m 28 sec
Reputation Power: 0
thanks so much!

 Viewing: Dev Shed Forums > Databases > MySQL Help > Sum of rows

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |