Sum Generate last 30 days

I need to do a daily sum, in the last 30 days. I have this query that fetches data from two tables, but if  exists a date in the second table (CabCC) and there is not in first table (CabFT), the value is not shown. How can I change the query to automatically generate the last 30 days in Select Datedoc.

SELECT Datedoc,
SUM(CASE WHEN Type='FT' AND Anl = 0 THEN isnull(value*cota,0)  END) AS 'Invoice',
(SELECT sum(Valuedoc)from CabCC where DateDocF=CabFT.Datedoc) as 'Rec'
FROM CabFT
group BY Datedoc
ORDER BY Datedoc desc
rflorencioAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brendt HessSenior DBACommented:
The most common way is to create a table containing all of the dates within a large range, and join to that table when you need an "Everyday, even if there is no data" output.

Here is a block of code that will create a table and insert all days from Jan 1, 2000 to November 8, 2044.

CREATE TABLE allDatesList (Dt smalldatetime PRIMARY KEY CLUSTERED)
INSERT INTO alldateslist VALUES ('20000101')

DECLARE @offset int
SET @offset = 1
WHILE @offset < 16000
BEGIN
	INSERT INTO allDatesList (Dt) 
	SELECT DATEADD(day, @offset, dt)
	FROM allDatesList
	
	SET @offset = @offset * 2
END

SELECT MIN(adl.Dt),
	MAX(adl.Dt)
FROM allDatesList AS adl

Open in new window


Now that you have this data, you can use it as the date driver for your query.  Here is an example that should work:

SELECT adt.dt AS Datedoc,
	SUM(
		CASE 
			WHEN Type='FT' AND Anl = 0 
				THEN isnull(value*cota,0)  
			END
		) AS 'Invoice',
	sum(Valuedoc) as 'Rec' 
FROM allDatesList AS adt
LEFT JOIN CabFT
	ON adt.Dt = CabFT.Datedoc
LEFT JOIN CabCC 
	ON adt.dt = CabCC.DateDocF
WHERE adt.Dt BETWEEN CAST(CAST(CURRENT_TIMESTAMP AS int) AS smalldatetime) 
		AND CAST(CAST(DATEADD(DAY, 30, CURRENT_TIMESTAMP) AS int) AS smalldatetime)
group BY adt.Dt
ORDER BY adt.dt DESC

Open in new window

HainKurtSr. System AnalystCommented:
maybe something like this

with n as (
select 1 n 
union all select 2 
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 0
)
SELECT * 
FROM   (SELECT n1.n * 10 + n2.n n 
        FROM   n n1, 
               n n2 
        WHERE  n1.n * 10 + n2.n <= 30) x 
       LEFT JOIN (SELECT datedoc, 
                         SUM(CASE 
                               WHEN TYPE = 'FT' 
                                    AND anl = 0 THEN Isnull(VALUE * cota, 0) 
                             END)                          AS 'Invoice', 
                         (SELECT SUM(valuedoc) 
                          FROM   cabcc 
                          WHERE  datedocf = cabft.datedoc) AS 'Rec' 
                  FROM   cabft 
                  WHERE  Datediff(DAY, datedoc, Getdate()) <= 30 
                  GROUP  BY datedoc) d 
         ON x.n = d.d 
GROUP  BY n 
ORDER  BY n DESC 

Open in new window

HainKurtSr. System AnalystCommented:
if you create a view like

create view n as
select 1 n
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 0

create view nn as select n1.n*10+n2.n n from n n1, n n2

then your query will be

SELECT n, dateadd(day,-1,cast(GETDATE() as date)) docdate, x.rec 
FROM   (SELECT n 
        FROM   nn 
        WHERE  n <= 30) x 
       LEFT JOIN (SELECT Datediff(DAY, datedoc, Getdate()) d, 
                         datedoc, 
                         SUM(CASE 
                               WHEN TYPE = 'FT' 
                                    AND anl = 0 THEN Isnull(VALUE * cota, 0) 
                             END)                          AS 'Invoice', 
                         (SELECT SUM(valuedoc) 
                          FROM   cabcc 
                          WHERE  datedocf = cabft.datedoc) AS 'Rec' 
                  FROM   cabft 
                  WHERE  Datediff(DAY, datedoc, Getdate()) <= 30 
                  GROUP  BY datedoc) d 
         ON x.n = d.d 
ORDER  BY n DESC 

Open in new window

IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Scott PletcherSenior DBACommented:
>> How can I change the query to automatically generate the last 30 days in Select Datedoc. <<

Do you need to generate an output for *every* day, even if no input row matches that day, or summarize only existing days at the day level?

That is, say the table contained data for Feb 28 thru Mar 27, except for Mar 13, 15 and 17.

Should your result show *every* day, with 0s for Mar 13, 15 and 17?

Or should the result not show Mar 13, 15 and 17 at all since they have no data?
rflorencioAuthor Commented:
I do not really need to get a list of all the days of the month. In practice what i want is to get the data in Table CAbCC even if that date does not exist in table CabFT. But all in the same row
rflorencioAuthor Commented:
If a day does not have data I do not need to show.
Scott PletcherSenior DBACommented:
Thank you, excellent explanation!

Please try this:


SELECT
    COALESCE(ft.Datedoc, cc.Datedoc) AS Datedoc,
    SUM(CASE WHEN ft.Type='FT' AND ft.Anl = 0 THEN isnull(ft.value*ft.cota,0)  END) AS 'Invoice',
    SUM(cc.Valuedoc) as 'Rec'
FROM CabFT ft
FULL OUTER JOIN CAbCC cc ON
    cc.Datedoc = ft.Datedoc
group BY COALESCE(ft.Datedoc, cc.Datedoc)
ORDER BY COALESCE(ft.Datedoc, cc.Datedoc) desc
rflorencioAuthor Commented:
The values returned are not correct, for ex. (and only in Cabft) in my query without CabCC table, one row returns a  result of 693.05,  in your query returns  as a result of 2772.206400000
Scott PletcherSenior DBACommented:
Hmm, each date could likely appear multiple times in each table.

Please try this:


SELECT
    COALESCE(ft.Datedoc, cc.Datedoc) AS Datedoc,
    SUM(CASE WHEN ft.Type='FT' AND ft.Anl = 0 THEN isnull(ft.value*ft.cota,0)  END) AS [Invoice],
    MAX(cc.Rec) AS [Rec]
FROM CabFT ft
FULL OUTER JOIN (
    SELECT
        Datedoc, SUM(Valuedoc) AS [Rec]
    FROM CAbCC
    GROUP BY
        Datedoc
) AS cc ON
    cc.Datedoc = ft.Datedoc
GROUP BY
    COALESCE(ft.Datedoc, cc.Datedoc)
ORDER BY
    COALESCE(ft.Datedoc, cc.Datedoc) DESC

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rflorencioAuthor Commented:
The query works fine, how can i now filter from last 30 days
rflorencioAuthor Commented:
Forget last post, it´s done. Thank´s
Scott PletcherSenior DBACommented:
SELECT
    COALESCE(ft.Datedoc, cc.Datedoc) AS Datedoc,
    SUM(CASE WHEN ft.Type='FT' AND ft.Anl = 0 THEN isnull(ft.value*ft.cota,0)  END) AS [Invoice],
    MAX(cc.Rec) AS [Rec]
FROM CabFT ft
FULL OUTER JOIN (
    SELECT
        Datedoc, SUM(Valuedoc) AS [Rec]
    FROM CAbCC
    WHERE
        Datedoc >= DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(DAY, -30, GETDATE())), 0)
    GROUP BY
        Datedoc
) AS cc ON
    cc.Datedoc = ft.Datedoc
WHERE
    ft.Datedoc >= DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(DAY, -30, GETDATE())), 0)
GROUP BY
    COALESCE(ft.Datedoc, cc.Datedoc)
ORDER BY
    COALESCE(ft.Datedoc, cc.Datedoc) DESC
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.