[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL SP Get Last 30 days

Posted on 2010-09-22
6
Medium Priority
?
504 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 2000 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

649 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