Solved

Sql Stored Procedure

Posted on 2013-12-06
2
348 Views
Last Modified: 2013-12-06
Hi Experts

I would like to build a sp in mssql for the attached file with question and target answer

That's all

Thanks
Book1.xlsx
0
Comment
Question by:beckyng
[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
2 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39701221
SELECT Parent, Child, Level, Hierachy, Usage,
   Usage * CASE WHEN Level = 1 THEN 1 ELSE (SELECT Usage FROM MyTable WHERE Level = 1) END AS [New Usage]
FROM myTable AS T
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 350 total points
ID: 39701462
-- test data
select	Parent	= 'P001'
,	Child	= 'C002'
,	Usage	= 2.000
into	#temp
union all
select	Parent	= 'C002'
,	Child	= 'K003'
,	Usage	= 1.000
union all
select	Parent	= 'K003'
,	Child	= 'T001'
,	Usage	= 0.027
go

-- recursive call
WITH
	recursive_CTE
	(	Parent
	,	Child
	,	Hierarchy
	,	Usage
	,	Level
	)
	AS
	(
		SELECT	a.Parent
		,	a.Child
		,	CAST(a.Parent +'.'+ a.Child as varchar(50))
		,	CAST(a.Usage as numeric(9,3))
		,	1
		FROM	#temp	a
		LEFT
		JOIN	#temp	b	ON	a.Parent = b.Child
		WHERE	b.Child IS NULL

		UNION ALL

		SELECT	b.Parent
		,	b.Child
		,	CAST(a.Hierarchy + '.' + b.Child as varchar(50))
		,	CAST(a.usage * b.usage as numeric(9,3))
		,	a.Level + 1
		FROM	recursive_CTE	a
		JOIN	#temp		b	ON	a.Child = b.Parent
		
	)
SELECT	Parent
,	Child
,	Level
,	Hierarchy
,	Usage
FROM	recursive_CTE

Open in new window

0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

696 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