Solved

Sum Generate last 30 days

Posted on 2012-03-27
12
836 Views
Last Modified: 2012-03-29
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
0
Comment
Question by:rflorencio
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
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

0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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

0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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

0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
>> 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?
0
 

Author Comment

by:rflorencio
Comment Utility
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
0
 

Author Comment

by:rflorencio
Comment Utility
If a day does not have data I do not need to show.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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
0
 

Author Comment

by:rflorencio
Comment Utility
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
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
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
0
 

Author Comment

by:rflorencio
Comment Utility
The query works fine, how can i now filter from last 30 days
0
 

Author Closing Comment

by:rflorencio
Comment Utility
Forget last post, it´s done. Thank´s
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now