+ Reply to Thread
Results 1 to 8 of 8

Thread: SUMPRODUCT (Equivelant to Excel)

  1. #1
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    SUMPRODUCT (Equivelant to Excel)

    Is there a SUMPRODUCT function equivelant in SQL? SUMPRODUCT multiplies two arrays and sums the totals:

    s_shares, s_price_per_share
    50, 24.00
    40, 18.50
    30, 12.00

    The SUMPRODUCT of that would be:

    (50 * 24.00) + (40 * 18.50) + (30 * 12) = 1,200 + 740 + 360 = 2,300

    My table is called tblStocks_details and field names are above in bold. Any ideas how to do this in SQL? I want to GROUP BY s_id, as I need to display a summary of each s_id in a recordset.
    "You'll never be as perfect as BLaaaaaaaaarche."

  2. #2
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    can you try something like this:
    Code:
    SELECT id, sum(s_shares) as No_of_shares, sum(s_shares * s_price_per_share) as sum_product FROM tbl_TableName GROUP BY id
    
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  3. #3
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    If I wanted to get the "average" of the shares purchased, could I do something such as:

    Code:
    SELECT id, sum(s_shares) as No_of_shares, (sum(s_shares * s_price_per_share) / No_of_shares) as sum_product FROM tbl_TableName GROUP BY id
    
    Is that valid SQL?
    "You'll never be as perfect as BLaaaaaaaaarche."

  4. #4
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    sql has an AVG function ... SQL AVG() Function
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  5. #5
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Quote Originally Posted by mehere View Post
    sql has an AVG function ... SQL AVG() Function
    Yes, but its not truly an average. For instance, in my example, the SUMPRODUCT value is 2,300. You need to divide that by the number of shares, which is 120. Your average cost per share would be 2,300 / 120, or $19.17. Whereas the AVG() function would return 2,300 / 3, or $766.67.

    Far from the correct value!

    (It is much like the weighted average of something, if you have had any experience there)
    "You'll never be as perfect as BLaaaaaaaaarche."

  6. #6
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    gotcha ... but then you need to do it this way:
    Code:
    sum(s_shares * s_price_per_share) / sum(s_shares) AS sum_product FROM tbl_TableName GROUP BY id
    
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  7. #7
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Quote Originally Posted by mehere View Post
    gotcha ... but then you need to do it this way:
    Code:
    sum(s_shares * s_price_per_share) / sum(s_shares) AS sum_product FROM tbl_TableName GROUP BY id
    
    Perfect. As usual, thanks for your help.
    "You'll never be as perfect as BLaaaaaaaaarche."

  8. #8
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    no problem ... glad it worked for you.
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

+ Reply to Thread

Similar Threads

  1. If formula help in excel
    By Rebelle in forum Microsoft Office
    Replies: 5
    Last Post: April 2nd, 2009, 09:52 PM
  2. Excel Convertor
    By Centurion in forum ASP Development
    Replies: 6
    Last Post: March 24th, 2009, 07:08 AM
  3. Excel and Email
    By Chrissy in forum ASP Development
    Replies: 10
    Last Post: February 16th, 2009, 09:11 PM
  4. From excel to database/web app
    By Rebelle in forum Database Design Help
    Replies: 7
    Last Post: July 10th, 2008, 02:36 PM
  5. Excel question
    By Rebelle in forum Microsoft Office
    Replies: 11
    Last Post: May 27th, 2008, 08:22 PM

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

SEO by vBSEO