+ Reply to Thread
Results 1 to 8 of 8

Thread: How to sum identical names in SQL?

  1. #1
    Valued Forum Contributor
    Join Date
    12-22-2010
    Location
    Texas
    MS-Off Ver
    Excel 2003 & Excel/Access 2007
    Posts
    302

    How to sum identical names in SQL?

    Hello there

    I have a query that brings over the sum of cities... because we have multiple locations in those cities it breaks them down. For example:

    Beaumont 1 - $50,000.00
    Beaumont 2 - $25,000.00

    I'd like to sum those in my query, so I can of course get $75,000.00... I'm not sure how to go about it... I'm thinking if it is possible, then it can be done through SQL. Just don't know how.

    I attached a sample of what my query gives me... I'd like to sum those that are identical but exclude those that contain "SMALL" because thsoe are not really identical...

    I also attached the SQL view of my query.
    Attached Files Attached Files
    Last edited by jgomez; 10-06-2011 at 12:42 PM.

  2. #2
    Forum Guru Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    MS Excel 2010
    Posts
    2,235

    Re: How to sum identical names in SQL?

    Do a Google for SQL GROUP BY

  3. #3
    Valued Forum Contributor
    Join Date
    12-22-2010
    Location
    Texas
    MS-Off Ver
    Excel 2003 & Excel/Access 2007
    Posts
    302

    Re: How to sum identical names in SQL?

    I see what you mean but how would i exclude? or can i even exclude??? I went here: http://www.w3schools.com/sql/default.asp

    & I didn't see anything that allows me to exclude certain words...

  4. #4
    Forum Guru Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    MS Excel 2010
    Posts
    2,235

    Re: How to sum identical names in SQL?

    Quote Originally Posted by jgomez View Post
    I see what you mean but how would i exclude? or can i even exclude??? I went here: http://www.w3schools.com/sql/default.asp

    & I didn't see anything that allows me to exclude certain words...
    Wouldn't that just use the WHERE clause?

  5. #5
    Valued Forum Contributor
    Join Date
    12-22-2010
    Location
    Texas
    MS-Off Ver
    Excel 2003 & Excel/Access 2007
    Posts
    302

    Re: How to sum identical names in SQL?

    i'm not sure... i never used SQL to build my queries.

  6. #6
    Forum Guru Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    MS Excel 2010
    Posts
    2,235

    Re: How to sum identical names in SQL?

    Change

    WHERE ((([Total Balance of CC].CTR_NAME) Not Like "*ARMS*"))
    to

    WHERE [Total Balance of CC].CTR_NAME Not Like "*ARMS*" AND
                 [Total Balance of CC].CTR_NAME Not Like "*SMALL*"

  7. #7
    Valued Forum Contributor GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2007/2010/2011
    Posts
    310

    Re: How to sum identical names in SQL?

    I think in general you would do something like this (not sure, but you might try it) -- I'm just using general table identifiers A & B -- this would just return

    SELECT SUM(A.Balance_Field), A.Name_Field
    
    From City as A JOIN City as B ON LEFT(A.Name_Field, 6) = LEFT(B.Name_Field, 6)
    
    WHERE A.name_field <> B.name_field (and any other conditions you used).
    
    Group By A.Name_Field
    There's not a city field you can match? if not, maybe match the first 6 characters of the city name (or some number). use the join to get a partial match (like above) and the where clause to eliminate an exact match.

    But i would think that in this table or another (that can join to it) you would have city information in it's own field.

  8. #8
    Valued Forum Contributor
    Join Date
    12-22-2010
    Location
    Texas
    MS-Off Ver
    Excel 2003 & Excel/Access 2007
    Posts
    302

    Re: How to sum identical names in SQL?

    Thank you both! I understand it now... somewhat. I couldn't successfully run a query when I would use SQL... i just did for the 1st time!!!

    No city I can match but I can adjust the names... from Beaumont 1, Beaumont 2 to just Beaumont on both.... should do the trick.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0