1

我正在做体重报告,但我遇到了问题。我使用此查询来了解我们仓库中的重量输入,但是当某个日期没有交易时,该日期不会出现在结果中。

SELECT  erp.MKPF.BUDAT AS Data,  
Sum( erp.MSEG.MENGE * erp.MARM.BRGEW ) as pes 

From erp.MKPF  
INNER Join erp.MSEG on erp.MKPF.MANDT = erp.MSEG.MANDT and erp.MKPF.MBLNR = erp.MSEG.MBLNR  
INNER Join erp.MARM on erp.MSEG.MANDT = erp.MARM.MANDT and erp.MSEG.MATNR = erp.MARM.MATNR And erp.MSEG.MEINS = erp.MARM.MEINH  
INNER JOIN erp.MARA on erp.MSEG.MANDT = erp.MARA.MANDT and erp.MSEG.MATNR = erp.MARA.MATNR

WHERE  erp.MKPF.MANDT = '100'  
and erp.MKPF.BUDAT >= '20120720' 
and erp.MKPF.BUDAT <= CONVERT(VARCHAR(8), GETDATE(), 112) -1 
and erp.MSEG.LGORT in ('1001','1069') 
and erp.MSEG.BWART In ('101','102','311','312') 
and erp.MSEG.WERKS = '1001' 
and erp.MARA.MTART in ('Z001','Z010','Z002','Z02E') 

GROUP BY erp.MKPF.BUDAT*

现在结果是这样的:

Data        PES
20120720    9999999.9999
20120721    9999999.8888
20120723    9999999.7777

我需要这个

Data        PES
20120720    9999999.9999
20120721    9999999.8888
20120722    0
20120723    999999.7777

有人可以帮助我吗?

4

2 回答 2

0

使用表或视图来生成感兴趣的日期范围并让其驱动查询。然后你将你的结果外部加入到这个视图中。这可以在查询中动态完成。例如,在 Oracle 中,您可以使用“connect by”来生成一个系列:

create table my_summary(the_day date, pes number);
insert into my_summary values(to_date('20120720', 'yyyymmdd'),  9999999.9999);
insert into my_summary values(to_date('20120721', 'yyyymmdd'),  9999999.8888);
insert into my_summary values(to_date('20120723', 'yyyymmdd'),  9999999.7777);

SELECT d.the_day, NVL(s.pes, 0) AS pes
  FROM ( SELECT to_date('20120720', 'yyyymmdd') + level -1 AS the_day
           FROM dual CONNECT BY level <= 4) d
       LEFT OUTER JOIN my_summary s ON (d.the_day = s.the_day)
  ORDER BY 1

THE_DAY   PES
--------- ---
20-JUL-12 9999999.9999 
21-JUL-12 9999999.8888 
22-JUL-12   0 
23-JUL-12 9999999.7777 

其他 rdbms 有其他方法来生成系列。这将要求您知道所需的开始日期和记录数(在上面的示例中为 20120720 和 4)。

于 2012-07-23T12:26:33.610 回答
0

谢谢大家,最后我做到了,它有效

    SELECT 
    c.BUDAT AS DATA, 
    CASE When SAP.pes Is Null then '0'
    ELSE SAP.pes
    END
From
    erp.YSD_CALENDAR as c LEFT JOIN

    (SELECT 
    erp.MKPF.BUDAT,
    Sum(
    erp.MSEG.MENGE
     * erp.MARM.BRGEW ) as pes
     FROM
     erp.MKPF 

INNER Join erp.MSEG on erp.MKPF.MANDT = erp.MSEG.MANDT and erp.MKPF.MBLNR = erp.MSEG.MBLNR  

INNER Join erp.MARM on erp.MSEG.MANDT = erp.MARM.MANDT and erp.MSEG.MATNR = erp.MARM.MATNR And erp.MSEG.MEINS = erp.MARM.MEINH 

INNER JOIN erp.MARA on erp.MSEG.MANDT = erp.MARA.MANDT and erp.MSEG.MATNR = erp.MARA.MATNR 

WHERE 
    erp.MKPF.MANDT = '100' 
and erp.MKPF.BUDAT >= '20120720'
and erp.MSEG.LGORT in ('1001','1069')
and erp.MSEG.BWART In ('101','102','311','312')
and erp.MSEG.WERKS = '1001'
and erp.MARA.MTART in ('Z001','Z010','Z002','Z02E')
and erp.MSEG.SHKZG = 'S'
GROUP BY erp.MKPF.BUDAT
) SAP ON SAP.BUDAT = c.BUDAT 

WHERE 
c.BUDAT >= '20120720'
and c.BUDAT <=  CONVERT(VARCHAR(8), GETDATE(), 112)

GROUP BY c.BUDAT, SAP.pes
ORDER BY c.BUDAT
于 2012-08-02T12:08:10.783 回答