Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Annual and Quarterly INNER JOIN
    primarykey
    data
    text
    <p>I have a first ANNUAL table like</p> <pre><code>ID | Date | AField1 | AField2 1a | 21/8/2000 | xxx | xxx 1a | 21/8/2001 | xxx | xxx 1a | 21/8/2002 | xxx | xxx ... 1a | 21/8/2013 | xxx | xxx 1b | 16/2/2000 | xxx | xxx 1b | 16/2/2001 | xxx | xxx ... </code></pre> <p>and a second QUARTERLY table like</p> <pre><code>ID | Date | QField1 | QField2 1a | 1/1/2000 | xxx | xxx 1a | 1/3/2000 | xxx | xxx 1a | 1/6/2000 | xxx | xxx 1a | 1/9/2000 | xxx | xxx 1a | 1/1/2001 | xxx | xxx 1a | 1/3/2001 | xxx | xxx ... 1a | 1/9/2013 | xxx | xxx 1b | 1/1/2000 | xxx | xxx 1b | 1/3/2000 | xxx | xxx ... </code></pre> <p><strong>I am trying to write a query that would return info for each quarterly row and info from the last annual statement available corresponding to this quarter, for a given ID joining the two tables i.e:</strong></p> <pre><code>ID | AnnualDate | QuarterlyDate| AnnualField1 | QuarteryField2 | ... 1a | 21/8/1999 | 1/1/2000 | xxx | xxx | ... 1a | 21/8/1999 | 1/3/2000 | xxx | xxx | ... 1a | 21/8/1999 | 1/6/2000 | xxx | xxx | ... 1a | 21/8/2000 | 1/9/2000 | xxx | xxx | ... 1a | 21/8/2000 | 1/1/2001 | xxx | xxx | ... 1a | 21/8/2000 | 1/3/2001 | xxx | xxx | ... ... 1a | 21/8/2012 | 1/1/2013 | xxx | xxx | ... 1a | 21/8/2012 | 1/3/2013 | xxx | xxx | ... 1a | 21/8/2012 | 1/6/2013 | xxx | xxx | ... 1a | 21/8/2013 | 1/9/2013 | xxx | xxx | ... </code></pre> <p>so far, my SQL query looks like:</p> <pre><code>SELECT q.ID, q.Date, q.QField1, q.QField2, a.AField1, a.AField2 FROM tableQ AS q INNER JOIN tableA AS a ON q.ID = a.ID WHERE q.ID = ('678abc') ORDER BY ID, Date DESC </code></pre> <p>but the date relation is obviously missing.. It might look simple but I have been stuck on this for more than 3 hours now and I would appreciate any help. I tried the SELECT TOP or MAX HAVING or INNER JOIN with q.Date >= a.Date but unsuccessfully...</p> <p><strong>EDIT:</strong> Also a basic inner join like: Year(q.Date) = Year(a.Date) doesn't work as sometimes the last annual statement might be from the year before or even older.</p> <p>Thanks for reading</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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