Solved

Using COALESCE in SQL Server 2000 DTS

Posted on 2010-11-30
6
341 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
  • 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 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 250 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 69

Expert Comment

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

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now