Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was going to add this as an edit to my other answer, but it really is a different way of doing it so should be a separate answer. </p> <p>I think I prefer the other answer that I have given but this one should work even if there are gaps in the data.</p> <p>To set the parameters for the query, change the values of <code>period_start_date</code> and <code>period_days</code> in the <code>query_params</code> part of the with clause.</p> <pre><code>with query_params as ( select date '2011-01-01' as period_start_date, 7 as period_days ), summary_data as ( select user_id, (data_entry_date - period_start_date)/period_days as period_number, sum(wine_glasses_drunk) as wine_glasses_drunk, sum(whisky_shots_drunk) as whisky_shots_drunk, sum(beer_bottle_drunk) as beer_bottle_drunk from foobar cross join query_params group by user_id, (data_entry_date - period_start_date)/period_days ) select user_id, period_number, period_start_date + period_number * period_days as period_start_date, sum(wine_glasses_drunk) as wine_glasses_drunk, sum(whisky_shots_drunk) as whisky_shots_drunk, sum(beer_bottle_drunk) as beer_bottle_drunk from ( -- this weeks data select user_id, period_number, wine_glasses_drunk, whisky_shots_drunk, beer_bottle_drunk from summary_data union all -- last weeks data select user_id, period_number + 1 as period_number, -wine_glasses_drunk as wine_glasses_drunk, -whisky_shots_drunk as whisky_shots_drunk, -beer_bottle_drunk as beer_bottle_drunk from summary_data ) a cross join query_params where period_number &lt;= (select max(period_number) from summary_data) group by user_id, period_number, period_start_date + period_number * period_days order by 1, 2 </code></pre> <p>And again, a <a href="http://sqlfiddle.com/#!1/2aee7/54" rel="nofollow">SQL Fiddle</a> is available.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThanks for the updated code. I have run it, but the numbers are different from what is expected. For example, using a start date of '2011-01-01', eyeballing the `beer_bottle_druk` field in the table, I expect the `beer_bottle_drunk` column in the query result to be (1+1+1+1+1+1+1) - (1+1+1+1+1+1+2) = -1, but the query result for 2011-01-01 is 7. Am I missing something?. Could you please clarify the meaning of the query result - what is it calculating/showing (I may be able to use it as is).
      singulars
    2. COThe date shown is the start day of the week and is coma red with the previous week. So, the row showing 2011-01-01 is comparing the week 2011-01-01 - 2011-01-07 with the week 2010-12-25 - 2010-12-31. The row with the results that you have asked about is shown as 2011-01-08, since this is the total for the week starting 2011-01-08 minus the total for the week starting 2011-01-01. Make sense? The first row in the results refers to the first week in your source table, but there is no prior week to compare with so it is subtracting 0.
      singulars
    3. COHi, Thanks for the feedback. I understand what the query results mean now. Its not quite what I had intended (`date shown is the start day of the week` - may not be in table). My main concern however is that when I change the start_date from 2011-01-01 to 2011-01-8 (as an example, but you can use any two dates returned in the report), the query results change completely even though the query result dates remain the same - that seems to suggest that something is wrong, since only the period numbers should change if dates occuring in the query results are used as the start_date. Please clarify?
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload