Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using COALESCE in SQL Server 2000 DTS

Posted on 2010-11-30
6
Medium Priority
?
376 Views
Last Modified: 2012-05-10
Hi all.

I have a table (Table A) with the following fields: ItemNumber, Year, CostPeriod1, CostPeriod2, CostPeriod3...CostPeriod12

Sample Data:
Item123--2007--5.50--3.5--NULL--2.5---etc until CostPeriod12
Item123--2008--10--NULL--NULL--44---etc until CostPeriod12
Item123--2009--NULL--33--NULL--44---etc until CostPeriod12

How can I use COALESCE so that if the period I'm looking at is null then go back and find me the last period with data (regardless of the year).  

Below is a sample code that ScottPletcher from a previous post suggested. But as you can see it looks at the previous year, I want to be able to look at ALL the previous years not just last year.

How can I do this?

Thank you in advance!


SELECT
    COAELSCE(CostPeriod1, 
        (SELECT COALESCE(CostPeriod12, CostPeriod11, CostPeriod10, ..., CostPeriod1) 
         FROM TableA aPrevious
         WHERE aPrevious.ItemNumber = a.ItemNumber AND aPrevious.Year = a.Year - 1
        ) ) AS CostPeriod1,
    COALESCE(CostPeriod2, CostPeridod1, 
        (SELECT COALESCE(CostPeriod12, CostPeriod11, CostPeriod10, ..., CostPeriod1) 
         FROM TableA aPrevious
         WHERE aPrevious.ItemNumber = a.ItemNumber AND aPrevious.Year = a.Year - 1
        ) ) AS CostPeriod2,
    COALESCE(CostPeriod3, CostPeriod2, CostPeridod1, 
        (SELECT COALESCE(CostPeriod12, CostPeriod11, CostPeriod10, ..., CostPeriod1) 
         FROM TableA aPrevious
         WHERE aPrevious.ItemNumber = a.ItemNumber AND aPrevious.Year = a.Year - 1
        ) ) AS CostPeriod3,
    ...
FROM TableA a

Open in new window

0
Comment
Question by:printmedia
[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
  • 2
6 Comments
 
LVL 7

Expert Comment

by:Cboudroz
ID: 34241321
I think you are looking for this:
SELECT
	itemNumber
	, COALESCE(a.CostPeriod1, b.CostPeriod12, b.CostPeriod11, b.CostPeriod10 /*,...*/ ) as CostPeriod1 
	, COALESCE(a.CostPeriod2, a.CostPeriod1, b.CostPeriod12, b.CostPeriod11, b.CostPeriod10 /*,...*/ ) as CostPeriod2 
	, COALESCE(a.CostPeriod3, a.CostPeriod2, a.CostPeriod1, b.CostPeriod12, b.CostPeriod11, b.CostPeriod10 /*,...*/ ) as CostPeriod3
FROM 
	TableA a
	left join TableA b
		on a.itemNumber = b.itemNumber
			and a.Year <> b.Year - 1

Open in new window

0
 
LVL 7

Expert Comment

by:Cboudroz
ID: 34241329
woups, error in ON clause, it's not  <> but =

SELECT
	itemNumber
	, COALESCE(a.CostPeriod1, b.CostPeriod12, b.CostPeriod11, b.CostPeriod10 /*,...*/ ) as CostPeriod1 
	, COALESCE(a.CostPeriod2, a.CostPeriod1, b.CostPeriod12, b.CostPeriod11, b.CostPeriod10 /*,...*/ ) as CostPeriod2 
	, COALESCE(a.CostPeriod3, a.CostPeriod2, a.CostPeriod1, b.CostPeriod12, b.CostPeriod11, b.CostPeriod10 /*,...*/ ) as CostPeriod3
FROM 
	TableA a
	left join TableA b
		on a.itemNumber = b.itemNumber
			and a.Year = b.Year - 1

Open in new window

0
 

Author Comment

by:printmedia
ID: 34241343
Thanks for the reply.

But I want to go as far back as there is data. Using a.Year = b.Year - 1 will only look at 2009 (if using this year as a reference), but what if I also want to look at 2008, 2007, 2006 as far back as I can go.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 34241608
SELECT
    COAELSCE(CostPeriod1, aMinus1.CostPeriod12, aMinus1.CostPeriod11, ..., aMinus1.CostPeriod1,
        aMinus2.CostPeriod12, aMinus2.CostPeriod11, ...
       aMinus5.CostPeriod12, ..., aMinus5.CostPeriod1, b.DefaultValue) AS CostPeriod1,
    ...
FROM TableA a
INNER JOIN TableB b ON --default cost if never a match
    a.ItemNumber = b.ItemNumber
LEFT OUTER JOIN TableA aMinus1 ON
    aMinus1.ItemNumber = a.ItemNumber AND aMinus1.Year = a.Year - 1
INNER JOIN TableA aMinus2 ON
    aMinus2.ItemNumber = a.ItemNumber AND aMinus2.Year = a.Year - 2
INNER JOIN TableA aMinus3 ON
    aMinus3.ItemNumber = a.ItemNumber AND aMinus3.Year = a.Year - 3
INNER JOIN TableA aMinus4 ON
    aMinus4.ItemNumber = a.ItemNumber AND aMinus4.Year = a.Year - 4
INNER JOIN TableA aMinus5 ON
    aMinus5.ItemNumber = a.ItemNumber AND aMinus5.Year = a.Year - 5
0
 
LVL 7

Assisted Solution

by:Cboudroz
Cboudroz earned 1000 total points
ID: 34241939
If you want to be able to go more then 5 years before used this:


In my example I only used 4 month
WITH Period (itemNumber, CostPeriod1, CostPeriod2, CostPeriod3, CostPeriod4, [Year], Level)
AS
(
-- Anchor member definition
    SELECT 
		itemNumber
		, P.CostPeriod1 AS CostPeriod1
		, COALESCE(P.CostPeriod2, P.CostPeriod1) AS CostPeriod2
		, COALESCE(P.CostPeriod3, P.CostPeriod2, P.CostPeriod1) AS CostPeriod3
		, COALESCE(P.CostPeriod4, P.CostPeriod3, P.CostPeriod2, P.CostPeriod1) AS CostPeriod4
		, [Year] 
		, 0 AS Level
		/* */
	FROM 
		TableA P
	WHERE 
		[Year] = '2010'

    UNION ALL

-- Recursive member definition
    SELECT 
		A.itemNumber
		, COALESCE(P.CostPeriod1,  A.CostPeriod4, A.CostPeriod3, A.CostPeriod2) AS CostPeriod1
		, COALESCE(P.CostPeriod2, P.CostPeriod1,  A.CostPeriod4, A.CostPeriod3) AS CostPeriod2
		, COALESCE(P.CostPeriod3, P.CostPeriod2, P.CostPeriod1,  A.CostPeriod4) AS CostPeriod3
		, COALESCE(P.CostPeriod4, P.CostPeriod3, P.CostPeriod2, P.CostPeriod1) AS CostPeriod4
		, P.[Year] 
		, Level + 1
	FROM 
		TableA A
		INNER JOIN Period AS P
			ON A.itemNumber = P.itemNumber
			   AND A.[Year] = P.[Year] - 1
	WHERE 
		P.CostPeriod1 IS NULL 
		OR P.CostPeriod2 IS NULL 
		OR P.CostPeriod3 IS NULL 
		OR P.CostPeriod4 IS NULL 

)
SELECT TOP 1
	*
FROM 
	Period
ORDER BY 
	Level DESC

Open in new window

0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 34243118
Lol, still an even split.  Good luck on future qs :-)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

609 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