Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 9091

Re: Joining two queries

$
0
0

Hi again

   Thought I had this issue resolved but when looking at the results i discovered some issues

If there is only one record per month then it works fine but if there is more than one record per month, then the sum is calculated for each record in that month.

So

Date                    Amount

01/2012               100

02/2012                200

02/2012                300

02/2012                100

 

03/2012                400

 

So Results should be

 

Date                Invoice Type              Should be                  Showing

01/2012           Moving balance         100                               100

02/2012           Moving Balance         700                             1900

03/2012           Moving Balance        1100                            1100

 

Here is my code

Have tried the with statement, distinct , but not sure what to do

 

select

 

 

          case_PrimaryCompanyId as companyid,

          CONVERT(VARCHAR(7),ca.case_createddate, 111) AS Date,

          'Moving Balance' as InvoiceType,

          sum( mb.Amount) as Amount

from

         cases as ca

left join (

select

    case_PrimaryCompanyId as companyid,

    case_createdDate,

    case_TotalExVat as Amount

from

    cases

) mb

on ca. case_primaryCompanyId = mb.companyid 

     and ca.case_createdDate >= mb.case_CreatedDate

where

    ca.case_primaryCompanyId = companyid

group by

     case_primaryCompanyId,

     CONVERT(VARCHAR(7),ca.case_createddate, 111)


Viewing all articles
Browse latest Browse all 9091

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>