Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The dates in your schema are messing me up a bit...the order stamp is the UTC that the order was placed, right? And invoice_stamp is the UTC when the customer was invoiced?</p> <p>I'm not sure what you mean by "rolling monthly totals", so I'm going to assume that it's a collection of all invoices for a given month? And a collection of all orders for a given month?</p> <p>Unbilled customers are those that have no invoice...</p> <p>If so, something like:</p> <pre><code> SELECT YEAR(FROM_UNIXTIME(order_stamp)), MONTH(FROM_UNIXTIME(order_stamp)), SUM(price) FROM orders LEFT JOIN order_lines USING (order_id) LEFT JOIN invoices USING (invoice_id) WHERE invoices.invoice_id IS NULL GROUP BY YEAR(FROM_UNIXTIME(order_stamp)), MONTH(FROM_UNIXTIME(order_stamp)) </code></pre> <p>...should get you there.</p> <p>If you're looking for the collection cost for things over 30 days or something, you want something like:</p> <pre><code>SELECT YEAR(FROM_UNIXTIME(invoice_date)), MONTH(FROM_UNIXTIME(invoice_date)), SUM(OverThirtyDayPrice), AVG(CollectionDays) FROM ( SELECT invoice_date, IF( DATEDIFF( FROM_UNIXTIME(IFNULL(invoice_stamp, UNIX_TIMESTAMP()), order_stamp) &gt; 30, price, 0 ) AS OverThirtyDayPrice, DATEDIFF(FROM_UNIXTIME(IFNULL(invoice_stamp, UNIX_TIMESTAMP()), order_stamp)) AS CollectionDays FROM orders LEFT JOIN order_lines USING (order_id) LEFT JOIN invoices USING (invoice_id) ) GROUP BY YEAR(FROM_UNIXTIME(invoice_date)), MONTH(FROM_UNIXTIME(invoice_date)) </code></pre> <p>We can hack up this last one a bit to make it work for things that were ordered in one month, but billed in another:</p> <pre><code>SELECT YEAR(invoice_datetime), MONTH(invoice_datetime), SUM(OutOfMonthPrice) FROM ( SELECT FROM_UNIXTIME(IFNULL(invoice_stamp, UNIX_TIMESTAMP())) AS invoice_datetime, IF(MONTH( FROM_UNIXTIME(IFNULL(invoice_stamp, UNIX_TIMESTAMP())) ) &lt;&gt; MONTH(FROM_UNIXTIME(order_stamp)), price, 0 ) AS OutOfMonthPrice, FROM orders LEFT JOIN order_lines USING (order_id) LEFT JOIN invoices USING (invoice_id) ) GROUP BY MONTH(invoice_datetime), YEAR(invoice_datetime) </code></pre> <p>If you want to get "point in time" queries, you may find that an RDBMS isn't the thing you need (you kinda need to iterate over a series of queries, with each one being for a specific month/year) and/or that your fundamental design doesn't support the report you're trying to generate.</p> <p>Ideally, what you want is a monthly report archived and reported upon.</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. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThanks for the suggestions. That's got me thinking but I'm not sure I explained this properly. Going with your final example I think it might be close. Basically I'm looking for the value of all the orders that have not yet been invoiced at that particular point in time. So for each month it needs to exclude future orders (where the order_stamp is > 00:00 of the 1st of that month). And also exclude orders that have already been invoiced but including ones that will be invoiced in the future. Confused? I am!
      singulars
    2. COBut going with your 3rd example I think I can explain it by providing the sub-select to go in the FROM() clause... SELECT SUM(orders_lines.price) FROM orders_lines LEFT JOIN orders USING(order_id) LEFT JOIN invoices ON `orders_lines`.`invoice_id`=`invoices`.`invoice_id` WHERE `orders`.`order_stamp` <= UNIX_TIMESTAMP('yyyy-mm-01 00:00:00') AND (`orders_lines`.`invoice_id` IS NULL OR `invoices`.`invoice_stamp` > UNIX_TIMESTAMP('yyyy-mm-01 00:00:00') ) where yyyy-mm is the year/month of the current month that the "parent" query is looping through. Not sure how to pass that to the sub-select tho
      singulars
    3. CO@batfastad: I'm definitely confused. LOL! And comments suck for changing things so I'll edit my original answer with some more details. I think you may be outthinking the problem though: you're describing a very weird report that doesn't fit any business pattern that I'm aware of.
      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