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

Sales Analysis Comparison 9.0 PL10

Please advise how to create this query properly by to/from (period)dates? Having a problem, for some reason, adding units and %s.

Stores(a property in BP) Total Units Total Sales Total Units LY(same period) Total Sales LY(same period) %Units %Sales

I have:

DECLARE @DateFrom AS DATE

DECLARE @DateTo AS DATE

SELECT @DateFrom=DocDate FROM OINV TO WHERE T0.DocDate='[%0]'

SELECT @DateTo=DocDate FROM OINV T1 WHERE T1.DocDate='[%1]'

CREATE TABLE #temp

(CardCode NVARCHAR(15),CardName NVARCHAR(100),CurrentYear DECIMAL(18,2),Prior Year DECIMAL (18,2))

INSERT INTO #temp(CardCode,CardName,CurrentYear,PriorYear)

SELECT CardCode,CardName,SUM(DocTotal),0

FROM OINV

WHERE DocDate BETWEEN @DateFrom AND @DateTo

GROUP BY CardCode,CardName

UNION

SELECT CardCode,CardName,SUM(DocTotal*-1),0

FROM ORIN

WHERE DocDate BETWEEN @DateFrom AND @DateTo

GROUP BY CardCode,CardName

UNION

SELECT CardCode,CardName,0,SUM(DocTotal)

FROM OINV

WHERE DocDate BETWEEN DATEADD(YEAR,-1,@DateFrom)AND DATEADD(YEAR,-1@DateTo)

GROUP By CardCode,CardName

UNION

SELECT CardCode,CardName,0,SUM(DocTotal*-1)

FROM ORIN

WHERE DocDate BETWEEN DATEADD(YEAR,-1@DateFrom)AND DATEADD(YEAR,-1@DateTo)

GROUP BY CardCode,CardName

SELECT CardCode,CardName,SUM(CurrentYear)AS'CurrentYear',SUM(PriorYear)AS'PriorYear' FROM #temp

GROUP BY CardCode,CardName ORDER BY CardCode,CardName

DROP TABLE #temp

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on May 20, 2014 at 01:28 AM

    Hi,

    Try this:

    DECLARE @DateFrom AS DATE

    DECLARE @DateTo AS DATE

    SELECT @DateFrom=DocDate FROM OINV T0 WHERE T0.DocDate='[%0]'

    SELECT @DateTo=DocDate FROM OINV T1 WHERE T1.DocDate='[%1]'

    CREATE TABLE #temp

    (CardCode NVARCHAR(15),CardName NVARCHAR(100),CurrentYear DECIMAL(18,2),Totalunit NVARCHAR (15),Totalunits NVARCHAR(100), salesPrior Year DECIMAL (18,2))

    INSERT INTO #temp(CardCode,CardName,totalunit, totalsales,CurrentYear,PriorYear)

    SELECT CardCode,CardName,SUM(T1.[LineTotal]),0,sum(T1.[Quantity])

    FROM OINV t0 inner join inv1 t1 on t0.docentry = t1.docentry

    WHERE T0.DocDate BETWEEN @DateFrom AND @DateTo

    GROUP BY CardCode,CardName

    UNION

    SELECT CardCode,CardName,SUM(T1.[LineTotal]),0,sum(T1.[Quantity])

    FROM ORIN T0 inner join RIN1 t1 on t0.docentry = t1.docentry

    WHERE t0.DocDate BETWEEN @DateFrom AND @DateTo

    GROUP BY CardCode,CardName

    UNION

    SELECT CardCode,CardName,0,SUM(T1.[LineTotal),,sum(T1.[Quantity])

    FROM OINV t0 inner join inv1 t1 on t0.docentry = t1.docentry

    WHERE t0.DocDate BETWEEN DATEADD(YEAR,-1,@DateFrom)AND DATEADD(YEAR,-1@DateTo)

    GROUP By CardCode,CardName

    UNION

    SELECT CardCode,CardName,0,SUM(T1.[LineTotal),,sum(T1.[Quantity])

    FROM ORIN T0 inner join RIN1 t1 on t0.docentry = t1.docentry

    WHERE T0.DocDate BETWEEN DATEADD(YEAR,-1@DateFrom)AND DATEADD(YEAR,-1@DateTo)

    GROUP BY CardCode,CardName

    SELECT CardCode,CardName,Totalunit, Totalsales,SUM(CurrentYear)AS'CurrentYear',SUM(PriorYear)AS'PriorYear' FROM #temp

    GROUP BY CardCode,CardName ORDER BY CardCode,CardName

    DROP TABLE #temp


    Let me know the query result.

    Thanks & Regards,

    Nagarajan

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 20, 2014 at 02:03 AM

    Hi Dan,

    What's wrong with your query ? Could you post the print screen of the query execution result ?

    Rgds,
    Jimmy

    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.