Iscala Sales Statistics/Month

101 views Asked by At

I am trying to get statistics on monthly sales in iScala ERP. iScala reporting system reports this values.

Iscala Internal Report

My Sql query returns

3 058 023   2017-01-01
2 237 651   2017-02-01
4 700 720   2017-03-01
2 268 501   2017-04-01
3 183 576   2017-05-01
3 238 173   2017-06-01
1 949 041   2017-07-01
3 077 111   2017-08-01

My Query, Selecting from invoices SL03 applying Order OR20 to subtract Freight Amount. After that i Union in SL29 Invoice Consolidation History using cross apply to subtract Freight and SaleTax.

BEGIN

    SELECT 
    SUM(cast(replace(isnull(b,0),',','.') as decimal(10,0))) Total, 
    cast(cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, d), 0) as date) as varchar(50))  date 
    FROM (
        SELECT 
        SUM(SL03100-aa.f) b , 
        cast(cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, SL03004), 0) as date) as varchar(50)) d 
        FROM SL030100 
                CROSS apply (  
                SELECT top 1 OR20044 f
                FROM OR200100
                WHERE SL030100.SL03036 = OR200100.OR20001 
                    ) aa

        WHERE SL03004 BETWEEN '2017-01-01' AND  DATEADD(d, 1,getdate()) 
        group  by cast(cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, SL03004), 0) as date) as varchar(50))  

    UNION

        SELECT 
        SUM((SL29007-SL29009-ba.f)) b,
        cast(cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, SL29006), 0) as date) as varchar(50)) d  
        FROM SL290100
             CROSS apply (
            SELECT top 1 OR20044 f
            FROM OR200100
            WHERE SL290100.SL29028 = OR200100.OR20001 
                ) ba
        WHERE SL29006 BETWEEN '2017-01-01' AND  DATEADD(d, 1,getdate()) 
        group by  cast(cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, SL29006), 0) as date) as varchar(50))  
    ) AS tbl
    group by cast(cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, d), 0) as date) as varchar(50)) 
    order by cast(cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, d), 0) as date) as varchar(50))



END

I don't know if this is the best way to do it or what fields are missing. I feel that the difference between the internal report and my SQL result is small but noticeable for the customer.

I hope you can point me in right direction or to some resources that can help.

Best regards

MK

0

There are 0 answers