Iscala销售统计/月

时间:2017-09-13 07:38:09

标签: sql sql-server epicor

我正在尝试获取iScala ERP中每月销售额的统计数据。 iScala报告系统报告此值。

Iscala Internal Report

我的Sql查询返回

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

我的查询,从发票SL03中选择应用订单OR20以减去运费金额。之后我在SL29发票合并历史中使用交叉申请减去Freight和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

我不知道这是最好的方法还是缺少哪些字段。我觉得内部报告和我的SQL结果之间的差异很小,但对于客户而言是显而易见的。

我希望你能指出我正确的方向或一些可以提供帮助的资源。

祝你好运

MK

0 个答案:

没有答案