Solved

Using COALESCE in SQL Server 2000 DTS

Posted on 2010-11-30
6
347 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 69

Accepted Solution

by:
Scott Pletcher 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:Scott Pletcher
ID: 34243118
Lol, still an even split.  Good luck on future qs :-)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

770 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