Skip to main content
Added prose to explain.
Source Link
csd
  • 1.8k
  • 10
  • 18
SELECT sp.user_id, SUM(dw.weight) FROM summing_period sp, diet_watch dw WHERE dw.user_id = sp.user_id AND dw.entry_date >= sp.start_date AND dw.entry_date <= sp.end_date GROUP BY sp.user_id 

What this query does is join each diet_watch row to the row in summing_period that matches its user_id and whose date falls in the summing_period's range.

The SELECT then asks for the SUM of the weights for each different user_id (as a result of the GROUP BY user_id).

SELECT sp.user_id, SUM(dw.weight) FROM summing_period sp, diet_watch dw WHERE dw.user_id = sp.user_id AND dw.entry_date >= sp.start_date AND dw.entry_date <= sp.end_date GROUP BY sp.user_id 
SELECT sp.user_id, SUM(dw.weight) FROM summing_period sp, diet_watch dw WHERE dw.user_id = sp.user_id AND dw.entry_date >= sp.start_date AND dw.entry_date <= sp.end_date GROUP BY sp.user_id 

What this query does is join each diet_watch row to the row in summing_period that matches its user_id and whose date falls in the summing_period's range.

The SELECT then asks for the SUM of the weights for each different user_id (as a result of the GROUP BY user_id).

Source Link
csd
  • 1.8k
  • 10
  • 18

SELECT sp.user_id, SUM(dw.weight) FROM summing_period sp, diet_watch dw WHERE dw.user_id = sp.user_id AND dw.entry_date >= sp.start_date AND dw.entry_date <= sp.end_date GROUP BY sp.user_id