Title: Postmeta meta_value SUM
Last modified: August 21, 2016

---

# Postmeta meta_value SUM

 *  [Deyaan](https://wordpress.org/support/users/deyaan/)
 * (@deyaan)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/postmeta-meta_value-sum/)
 * Hy, everybody!
 * I’ve got wp_postmeta table with post_id, meta_key and meta_value.
    I would like
   to echo a sum of meta_values of meta_key ‘profit’ but only between two dates 
   that are stored as meta_values of meta_key ‘start’. Those dates are stored in
   form of 20140715
 * I am having a really hard time figuring that out.
 * Someone please help!

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/postmeta-meta_value-sum/#post-5100302)
 * I think you need to provide more detail before anyone can help. For example, 
   you want two values from the meta_key ‘start’. Are there multiple ‘start’ fields
   assigned to each post? If not, how do you select the two ‘start’ rows?
 * If you would make up a simple table of postmeta values as an example, it would
   help a great deal.
 *  Thread Starter [Deyaan](https://wordpress.org/support/users/deyaan/)
 * (@deyaan)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/postmeta-meta_value-sum/#post-5100350)
 * Only one start for each post and only one profit for each post and they share
   the same post_id. I will enter the _between starts_ manually into query.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/postmeta-meta_value-sum/#post-5100390)
 * Please try this:
 *     ```
       $begin_dt = '20130601';
       $end_dt = '20130718';
       $sql = "
       SELECT SUM(pm1.meta_value) as total_profit
       FROM $wpdb->postmeta pm1
       JOIN $wpdb->postmeta pm2 ON pm2.post_id = pm1.post_id
       WHERE pm1.meta_key = 'profit'
       AND pm2.meta_key = 'start'
       AND pm2.meta_value BETWEEN '$begin_dt' AND '$end_dt'
       ";
       $total_profit = $wpdb->get_var($sql);
       $total_profit += 0; // Make sure value is numeric
       echo "<p>Total Profit = $total_profit</p>";
       ```
   
 *  Thread Starter [Deyaan](https://wordpress.org/support/users/deyaan/)
 * (@deyaan)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/postmeta-meta_value-sum/#post-5100394)
 * It works perfectly!
    Thank you very much. I will have to study this little piece
   of code in detail to learn something. Thanx again!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Postmeta meta_value SUM’ is closed to new replies.

## Tags

 * [meta_value)](https://wordpress.org/support/topic-tag/meta_value/)
 * [sum](https://wordpress.org/support/topic-tag/sum/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [Deyaan](https://wordpress.org/support/users/deyaan/)
 * Last activity: [12 years, 1 month ago](https://wordpress.org/support/topic/postmeta-meta_value-sum/#post-5100394)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
