Solved

SQL SP Get Last 30 days

Posted on 2010-09-22
6
488 Views
Last Modified: 2012-05-10
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
Comment
Question by:MBoy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 26

Accepted Solution

by:
tigin44 earned 500 total points
ID: 33735505

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
 
LVL 3

Author Comment

by:MBoy
ID: 33735954
I get the same results as before... 5 days where there is data and no return results for the other 25 days??
0
 
LVL 3

Author Comment

by:MBoy
ID: 33735969
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 26

Expert Comment

by:tigin44
ID: 33735986
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
 
LVL 26

Expert Comment

by:tigin44
ID: 33736046
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
 
LVL 3

Author Comment

by:MBoy
ID: 33738135
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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

688 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