Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
aesquis
Starting Member
7 Posts |
Posted - 2012-07-23 : 05:08:58
|
Dear sirs.I'm doing a Weight reporting and i have a problem. I use this query to know the enters of weight in our warehouse, but when there are no transactions in a date this date doesn't appears in the results.SELECT erp.MKPF.BUDAT AS Data, Sum( erp.MSEG.MENGE * erp.MARM.BRGEW ) as pesFrom 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) -1and 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.BUDATNow the results are like this:Data PES20120720 9999999.999920120721 9999999.888820120723 9999999.7777And i need thisData PES20120720 9999999.999920120721 9999999.888820120722 020120723 999999.7777Can somebody help me?Thanks for all |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-23 : 06:46:02
|
You will need to use a calendar table and then join to it. If you don't already have a calendar table in your database, you can create one and use that like shown below:-- CREATE A CALENDAR TABLE.CREATE TABLE #Calendar(Dt DATE NOT NULL PRIMARY KEY CLUSTERED);;WITH N(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM N WHERE n < 31) -- number of days you want to considerINSERT INTO #Calendar SELECT DATEADD(dd,n-1,'20120701') FROM N OPTION (MAXRECURSION 0);-- join with the calendar tableSELECT c.Dt AS DATA, SUM(erp.MSEG.MENGE * erp.MARM.BRGEW) AS pesFROM #Calendar c LEFT JOIN (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) ON rp.MKPF.BUDAT = c.DtWHERE 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 |
 |
|
aesquis
Starting Member
7 Posts |
Posted - 2012-07-23 : 07:14:46
|
Ok. Thanks a lot, I'm sure it's a good solution and I'll try. Now i'm looking for a calendar table in SAP. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
aesquis
Starting Member
7 Posts |
Posted - 2012-08-02 : 08:07:02
|
Thanks to all, finally I did this and it worksSELECT c.BUDAT AS DATA, CASE When SAP.pes Is Null then '0' ELSE SAP.pes ENDFrom 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.pesORDER BY c.BUDAT |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-02 : 09:58:00
|
coolyou're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|