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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Show non existing dates as 0

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 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


Now the results are like this:

Data PES
20120720 9999999.9999
20120721 9999999.8888
20120723 9999999.7777

And i need this
Data PES
20120720 9999999.9999
20120721 9999999.8888
20120722 0
20120723 999999.7777

Can 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 consider
INSERT INTO #Calendar SELECT DATEADD(dd,n-1,'20120701')
FROM N OPTION (MAXRECURSION 0);

-- join with the calendar table
SELECT
c.Dt AS DATA,
SUM(erp.MSEG.MENGE * erp.MARM.BRGEW) AS pes
FROM
#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.Dt
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
Go to Top of Page

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.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-23 : 10:59:40
quote:
Originally posted by aesquis

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.


you can create it in sql itself using UDF

see an example

http://visakhm.blogspot.com/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

aesquis
Starting Member

7 Posts

Posted - 2012-08-02 : 08:07:02
Thanks to all, finally I did this and it works


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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-02 : 09:58:00
cool

you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -