Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Complicated Query Requirement


Complicated Query Requirement

Author
Message
Subbu S
Subbu S
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 138
Hi All,

I want the following manipulation without using and loop r cursors, i tried with CTE, but i didnt get the output.


***************************************************
Select 1 [Id],1000[value] into TempA



Select 2[Id], 10 Value, 1 parentid into TempB

Insert into TempB
Select 3,9,1
union all
select 4,5,1

***************************************************



Id Final Value Describtion Example
2 990 1 Value - 2 Value 1000-10
3 981 1 Value - 2 Value - 3 Value 1000-10- 9
4 976 1 Value - 2 Value - 3 Value - 4 value 1000-10-9-5
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45450 Visits: 39946
Subbu S (12/16/2012)
i tried with CTE, but i didnt get the output


Can we see it, please?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Subbu S
Subbu S
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 138
WITH CTE_BSI(ID, VAL)
AS
(
SELECT ID, VALUE FROM TempA AS A
UNION ALL
SELECT t.ID,(c.val - value) FROM TempB T join CTE_BSI C on T.parent = c.Id

)
SELECT ID, VAL FROM CTE_BSI
Subbu S
Subbu S
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 138
Finally i found the solution, here u go,

Select 1 [Id],1000[value] into TempA



Select 2[Id], 10 value, 1 parent into TempB

Insert into TempB
Select 3,9,1
union all
select 4,5,1


Select * From TempA;
Select * From TempB;


Select ROW_NUMBER() Over (Partition By Parent Order By B.Id) Rnum,B.*,A.value [PVal] into tempc From tempb B

join tempa A on a.id = b.parent


Select * From TempC;

Select *, PVal - (Select SUM(value) From tempc E Where E.parent = D.Parent AND E.Rnum <= D.Rnum) From tempc D
Bhuvnesh
Bhuvnesh
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2930 Visits: 4076
gad you found the solution yourself but in future always try to post table defintion , query you tried (though its incorrect) along with test data so that people can easliy jump into your prblm :-)

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
rhythm.varshney
rhythm.varshney
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

Group: General Forum Members
Points: 88 Visits: 195
Subbu S (12/16/2012)
Finally i found the solution, here u go,

Select 1 [Id],1000[value] into TempA



Select 2[Id], 10 value, 1 parent into TempB

Insert into TempB
Select 3,9,1
union all
select 4,5,1


Select * From TempA;
Select * From TempB;


Select ROW_NUMBER() Over (Partition By Parent Order By B.Id) Rnum,B.*,A.value [PVal] into tempc From tempb B

join tempa A on a.id = b.parent


Select * From TempC;

Select *, PVal - (Select SUM(value) From tempc E Where E.parent = D.Parent AND E.Rnum <= D.Rnum) From tempc D


As you mentioned in your original post that you don't want to use Loop or Cursor.But in your posted query also the inner query will execute repeatedly for outer query.Is not it ?
Then what is the gain you had.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search