Solved

Sum Generate last 30 days

Posted on 2012-03-27
12
840 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
ID: 37772510
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
ID: 37773489
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
ID: 37773505
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37774213
>> 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
ID: 37775414
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
ID: 37775419
If a day does not have data I do not need to show.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37776909
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
ID: 37782763
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:
Scott Pletcher earned 500 total points
ID: 37782814
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
ID: 37783124
The query works fine, how can i now filter from last 30 days
0
 

Author Closing Comment

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

Expert Comment

by:Scott Pletcher
ID: 37783274
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

810 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