Skip to Content
author's profile photo Former Member
Former Member

Help with Query and PIVOT

Hi Guys,

I have written this query to hopefully give me the Monthly Sales by Country, however I cannot get it to quite work correctly,

Does anyone have any thoughts as to where I have gone wrong. Help appreciated.

Select distinct Country,sum(SalesYTD) from

(SELECT distinct T2.Country, SUM(T1.Debit) - sum(T1.Credit) as SalesYTD, T1.Shortname, MONTH(T1.RefDate) as [Month] FROM JDT1 T1 INNER JOIN OCRD T2 ON T1.ShortName = T2.CardCode

WHERE Year(T1.RefDate) = 2011 AND T1.TransType IN ('13','14') and T2.CardType = 'C'

group by T2.Country,T1.REFDATE, T1.Shortname) sYTD

PIVOT (sum(SalesYTD) FOR [Month] IN

([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) P

group by Country

Kind regards

Sean Martin

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • author's profile photo Former Member
    Former Member
    Posted on Aug 23, 2011 at 03:06 PM

    Not to worry guys, resolved it

    select * from (

    SELECT DISTINCT Country, SUM(SalesYTD) as YTDSales, [MONTH] FROM

    (

    SELECT distinct T2.Country, SUM(T1.Debit) - SUM(T1.Credit) as SalesYTD, T1.Shortname, MONTH(T1.RefDate) as [Month] FROM JDT1 T1 INNER JOIN OCRD T2 ON T1.ShortName = T2.CardCode

    WHERE Year(T1.RefDate) = 2011 AND T1.TransType IN ('13','14') and T2.CardType = 'C'

    group by T2.Country, T1.Shortname,month(T1.RefDate))

    sYTD

    group by Country,[MONTH]

    ) Q

    PIVOT (sum([YTDSales]) FOR [Month] IN

    ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) P

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.