?
Solved

Sql Stored Procedure

Posted on 2013-12-06
2
Medium Priority
?
349 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 1400 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

765 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