Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server 2008 left join where clause based on max date
    primarykey
    data
    text
    <p>Got some help the other day on this and now really want to make a change for the better</p> <p>Here is code</p> <pre><code>select machines.serialnum, DRPS.assetnumber, DRPS.MOCALCSUM, DRPS.MICALCSUM, DRPS.COCALCSUM, DRPS.CICALCSUM, ISSUED.TotalIssued, REDEEMED.TotalRedeemed,drps.dropdate1,drps.dropdate2 from (select serialnum from machineinfo) as machines LEFT JOIN (select assetnumber, min(dropdate) as [dropdate1], max(dropdate) as [dropdate2], sum(mocalc) AS [MOCALCSUM], sum(micalc) AS [MICALCSUM], sum(cocalc) AS [COCALCSUM],sum(cicalc) AS [CICALCSUM] from drops where dropdate &gt; '09/04/2012' and dropdate &lt; dateadd(hour, -0, getdate()) GROUP BY assetnumber) AS DRPS on machines.serialnum = drps.assetnumber LEFT JOIN (select snissued, cast(sum(amount) as money) / 100 AS [TotalIssued] from tickets where dateissued &gt; '09/04/2012' and dateissued &lt; dateadd(hour, 0, getdate()) group by snissued) AS ISSUED ON machines.serialnum = ISSUED.snissued LEFT JOIN (select snredeemed, cast(sum(amount) as money) / 100 AS [TotalRedeemed] from tickets where dateredeemed &gt; '09/04/2012' and dateredeemed &lt; dateadd(hour, 0, getdate()) group by snredeemed) AS REDEEMED ON machines.serialnum = REDEEMED.snredeemed </code></pre> <p>what I would like to accomplish if possible is for the second and third join is to use the <code>drps.drop1</code> in the where clause like this</p> <pre><code>where dropdate &gt; drps.dropdate1 and dropdate &lt; drps.dropdate2 </code></pre> <p>but it does not work</p> <p>here is working output</p> <pre> serialnum MOCALCSUM MICALCSUM COCALCSUM CICALCSUM TotalIssued TotalRedeemed dropdate1 dropdate2 0-2739-41401 5482 5498 132 148 3258.00 3110.00 2012-09-04 13:36:53.450 2012-09-05 13:55:38.750 0-2459-36182 1110 1054 114 58 1895.00 1657.00 2012-09-04 15:01:19.973 2012-09-05 13:55:38.967 </pre> <p>end result is I need total issued and total redeemed date range to be between the min and max date per serial number</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