SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Show non existing dates as 0
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

aesquis
Starting Member

Spain
7 Posts

Posted - 07/23/2012 :  05:08:58  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/23/2012 :  06:46:02  Show Profile  Reply with Quote
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

Edited by - sunitabeck on 07/23/2012 06:47:44
Go to Top of Page

aesquis
Starting Member

Spain
7 Posts

Posted - 07/23/2012 :  07:14:46  Show Profile  Reply with Quote
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

India
47081 Posts

Posted - 07/23/2012 :  10:59:40  Show Profile  Reply with Quote
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

Spain
7 Posts

Posted - 08/02/2012 :  08:07:02  Show Profile  Reply with Quote
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

India
47081 Posts

Posted - 08/02/2012 :  09:58:00  Show Profile  Reply with Quote
cool

you're welcome

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000