2012-07-23 38 views
1

我正在做體重報告,並且遇到問題。我使用此查詢來了解倉庫中的重量輸入情況,但是如果日期中沒有交易,則此日期不會顯示在結果中。SQL顯示沒有交易的日期,值= 0

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 

有人可以幫助我嗎?

+0

我所知,沒有簡單的方法來修改您的查詢來獲取你想要的數據集。數據如何最終呈現給用戶?這可能是一個更好/更容易的地方來添加顯示邏輯來填充0值的空日期。 – paul 2012-07-23 10:56:38

+1

如果您構建輔助日期表並在budat上將其加入MKPF,您將獲得缺少日期。過濾條件應應用於日期表。 – 2012-07-23 10:59:48

+0

忽略我刪除的答案 - 完全誤讀了問題。在這裏同意尼古拉,雖然這應該給你一些想法。 http://weblogs.sqlteam.com/jeffs/archive/2007/09/10/group-by-month-sql.aspx – raven 2012-07-23 11:10:37

回答

0

使用表格或視圖來生成感興趣的日期範圍並讓此驅動器查詢。然後你外面加入你的結果到這個視圖。這可以在查詢中動態完成。例如,在Oracle中,你可以使用「連接通過」生成一系列:

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以上的例子中)。

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