Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to work backwards. In order to get rows for dates that don't exist, you need to outer join them to rows that do have those dates. In order to outer join, you need to have a sequence to join to. Since you don't have a sequence, you need to create one.</p> <p>To create that sequence, you have two options:</p> <ul> <li>Create a static date sequence and store it in a permanent table (Larry's answer); or</li> <li>Use an existing numeric sequence (such as spt_values) to create one on the fly.</li> </ul> <p>Let's assume you want the flexibility of the second approach. Here's a common snippet I use for things like that:</p> <pre><code>SELECT DATEADD(DAY, v.number, @fromdate) FROM master.dbo.spt_values v WHERE v.type = 'P' AND v.number &lt;= DATEDIFF(DAY, @fromdate, @todate) </code></pre> <p>Now just toss that in a CTE and join it:</p> <pre><code>WITH Dates_CTE (dt) AS ( -- // Paste the snippet above in here ) SELECT d.dt AS paymentdate, ISNULL(SUM(p.paymentamount), 0) AS subtotal FROM Dates_CTE d LEFT JOIN tblpayment p ON p.paymentdate = d.dt GROUP BY d.dt ORDER BY d.dt </code></pre> <p>(Update: I left out the WHERE clause in the main query because it's technically handled by the the join, but in some instances you might get better performance by leaving it in)</p> <p>As for the currency conversion, look up the syntax for PIVOT.</p> <hr> <p>Update on PIVOT: You should be able to just enclose that entire query in parentheses, then go:</p> <pre><code>SELECT paymentdate, [Euro] AS euroamount, [Pound] as poundamount FROM ( -- // Insert the full query from above in here ) p PIVOT ( SUM(subtotal) FOR paymentcurrency IN ([Euro], [Pound]) ) AS pvt </code></pre> <p>Hard to verify without knowing exactly what kind of data is in there, but try that as a starting point.</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.
 

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