• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

SQL SP Get Last 30 days

I have a sp that returns the top 30 results but I need the last 30 days even if the result is 0 for any of those days.  Today is brain fart day:)

BEGIN
     SELECT TOP 30
        SUM([TotalGallonsSinceLastEventReport]) As DailyTotal,
        CONVERT(VARCHAR(8), [ReceivedDate], 1) As Date            
     FROM
         [PumpHourMeters]
     WHERE
         [ModuleID] = @ModuleID
     GROUP BY CONVERT(VARCHAR(8), [ReceivedDate], 1)
     ORDER BY CONVERT(VARCHAR(8), [ReceivedDate], 1)ASC
            
END
0
MBoy
Asked:
MBoy
  • 3
  • 3
1 Solution
 
tigin44Commented:

BEGIN
     SELECT SUM([TotalGallonsSinceLastEventReport]) As DailyTotal,
        CONVERT(VARCHAR(8), [ReceivedDate], 1) As Date            
     FROM
         [PumpHourMeters]
     WHERE ReceivedDate > gatdate() -30
         [ModuleID] = @ModuleID
     GROUP BY CONVERT(VARCHAR(8), [ReceivedDate], 1)
     ORDER BY CONVERT(VARCHAR(8), [ReceivedDate], 1)ASC
            
END

Open in new window

0
 
MBoyAuthor Commented:
I get the same results as before... 5 days where there is data and no return results for the other 25 days??
0
 
MBoyAuthor Commented:
Sorry - This table is refined data that doesn't show dates other than the days with pumping data.  Sorry, I knew today was brain fart day.
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.

 
tigin44Commented:
if you dont have data related to that days than you cant get that days with the above query. If you want to list the last 30 days even if those days have corresponding records the above query have to be modified.
0
 
tigin44Commented:
try this
DECLARE @DATES (salesDate	datetime);
DECLARE @tmpDate	datetime;
SET @tmpDate = GETDATE() -30;

WHILE (@tmpDate <= GETDATE())
BEGIN
	INSERT INTO @DATES (salesDate)
	VALUES(@tmpDate)

	SET @tmpDate = DATEADD(DAY, 1, @tmpDate);
END


	SELECT  
		SUM([TotalGallonsSinceLastEventReport]) As DailyTotal,
        CONVERT(VARCHAR(8), D.salesDate, 1) As Date            
     FROM
		@DATES D
	LEFT OUTER JOIN [PumpHourMeters] Y ON Y.ReceivedDate = D.salesDate
     WHERE
         [ModuleID] = @ModuleID
     GROUP BY CONVERT(VARCHAR(8), [ReceivedDate], 1)
     ORDER BY CONVERT(VARCHAR(8), [ReceivedDate], 1)ASC

Open in new window

0
 
MBoyAuthor Commented:
I'm having trouble getting this into a valid SP. Any help would be appreciated.

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[Pump_GetChartPumpingSummaryLastThirtyDays]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[Pump_GetChartPumpingSummaryLastThirtyDays];
GO
CREATE PROCEDURE [dbo].[Pump_GetChartPumpingSummaryLastThirtyDays]
(
      @ModuleID INT
)
AS
BEGIN
     SELECT TOP 30
        SUM([TotalGallonsSinceLastEventReport]) As DailyTotal,
        CONVERT(VARCHAR(8), [ReceivedDate], 1) As Date            
     FROM
         [PumpHourMeters]
     WHERE
         [ModuleID] = @ModuleID
     GROUP BY CONVERT(VARCHAR(8), [ReceivedDate], 1)
     ORDER BY CONVERT(VARCHAR(8), [ReceivedDate], 1)ASC
            
END
GO
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now