Solved

# SQL SP Get Last 30 days

Posted on 2010-09-22
444 Views
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,
FROM
[PumpHourMeters]
WHERE
[ModuleID] = @ModuleID

END
0
Question by:MBoy
• 3
• 3

LVL 26

Accepted Solution

tigin44 earned 500 total points

``````BEGIN

SELECT SUM([TotalGallonsSinceLastEventReport]) As DailyTotal,

FROM

[PumpHourMeters]

[ModuleID] = @ModuleID

END
``````
0

LVL 3

Author Comment

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

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

LVL 26

Expert Comment

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

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

``````
0

LVL 3

Author Comment

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,
FROM
[PumpHourMeters]
WHERE
[ModuleID] = @ModuleID

END
GO
0

## Featured Post

### Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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â€¦
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Acâ€¦
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed