all groups > sql server programming > april 2007 >
You're in the

sql server programming

group:

SQL Delema



SQL Delema Phill
4/17/2007 9:08:04 PM
sql server programming: I have a stored procedure that returns outage totals by month for the past 7
years. The problem is that if there is no outage for a given month it
doesn't return all of the previous years. For example, I have 4 outages in
Jan 07, 0 in Jan 06, 1 in Jan 05; My resultset returns 4 Jan 07 and 1 Jan 05.
I want it to also return 0 Jan 06. This is for a graph. Does anyone have
any ideas? Here is my SP. Thanks.

SP:
CREATE PROCEDURE dbo.usp_GetTotal_SAIFI
AS
DECLARE @maxMonth varchar(5)
DECLARE @maxYear varchar(5)
DECLARE @maxDate datetime
DECLARE @endMonthTitle varchar(50)

--Get the last day of the previous month
SELECT @maxDate = DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0,
GETDATE()), 0))

--GetDate()

SELECT @maxYear = Year(@maxDate)

-- Get the value for the most current month in the most current year
SELECT @maxMonth = Month(@maxDate)
SET @endMonthTitle=DateName(month, @maxDate) + ' ' +Cast(Year(@maxDate) AS
varchar(4))
SELECT Year(OutageDate) AS OutageYear,
COUNT(OutageID) AS Outages,
(SELECT CustomerCount FROM tblCustomerCount WHERE YearID =
Year(OutageDate)) AS CustomerCount,
(SELECT CustomersPerFeeder FROM tblCustomerCount WHERE YearID =
Year(OutageDate)) AS CustomersPerFeeder,
@endMonthTitle AS EndMonth

FROM
tblOutages
WHERE
Month(OutageDate) <= @maxMonth
AND CAST(Year(OutageDate) AS Int) > @maxYear - 7
GROUP BY Year(OutageDate)
GO
Re: SQL Delema Uri Dimant
4/18/2007 12:00:00 AM
Hi
Can yopu provide DDL+ sample data + an expected result?

[quoted text, click to view]

Re: SQL Delema Tibor Karaszi
4/18/2007 12:00:00 AM
Create a calendar table with (at least) each month, then do an outer join against that table, having
the calendar table as the preserved table.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]

AddThis Social Bookmark Button