Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Populate the last value

Author  Topic 

bhaasjoshi
Starting Member

17 Posts

Posted - 2009-07-06 : 07:21:20
Hi,

I have a table
A B C D
1 x x 1
1 x x 2
1 x x 4
2 x x 3
2 x x 5
3 x x 1

Its sorted by column A and then by D, I need the table to look like this,
A B C D E
1 x x 1
1 x x 2
1 x x 4 4
2 x x 3
2 x x 5 5
3 x x 1 1

Please help me.

Thanks,
B

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-06 : 07:34:58
Sorry, but the condition for this output is not clear...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-07-06 : 07:50:07
Try This,...

update t1 set E =t.maxD from table t1 inner join
(select A, max(D) as maxD from table group by A) t on t.A=t1.A



Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-07-06 : 07:53:09
quote:
Originally posted by senthil_nagore

Try This,...

update t1 set E =t.maxD from table t1 inner join
(select A, max(D) as maxD from table group by A) t on t.A=t1.A



Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/





hi senthil_nagore

did u tried this which u have mention..







-------------------------
Your time is a valuable resource.
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-07-06 : 07:54:21
No i did't have SQL Box now?? any problem there?

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-07-06 : 07:54:25
Try

DECLARE @T TABLE
(
A INT,
D INT,
E INT
)
INSERT INTO @T(a,d) SELECT
1,1 UNION ALL SELECT
1,2 UNION ALL SELECT
1,4 UNION ALL SELECT
2,3 UNION ALL SELECT
2,5 UNION ALL SELECT
3,1
go

SELECT a,d,e
FROM @T

;WITH CTE AS
(
SELECT a,d,e,ROW_NUMBER()OVER(PARTITION BY A ORDER BY D DESC) AS SEQ
FROM @T
)
UPDATE CTE
SET e = d
WHERE SEQ=1


SELECT a,d,e
FROM @T




Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-07-06 : 07:57:28
quote:
Originally posted by senthil_nagore

No i did't have SQL Box now?? any problem there?

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/




You need add one more condition
on t.A=t1.A
AND t1.d = t.maxd

Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

bhaasjoshi
Starting Member

17 Posts

Posted - 2009-07-06 : 08:00:10
Thanks for replying guys, but I'm a complete newbie and I'm writing this sql code in a different application for someone which doesnt support sql procedures etc. Is there a way by which I can rfer to the rows like, if currentval not equal to nextval (in column A) then column E =column D. or something like that.

Thanks again,
B
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-07-06 : 08:02:43
quote:
Originally posted by senthil_nagore

No i did't have SQL Box now?? any problem there?

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/




CREATE TABLE #temp
(
a char(1),
b char(1),
c char(1),
d char(1),
e char(1)
)

INSERT INTO #temp(a,b,c,d)
SELECT 'a' ,'b' ,'c' ,'d' UNION ALL
SELECT '1' ,'x' ,'x' ,'1' UNION ALL
SELECT '1', 'x' ,'x' ,'2' UNION ALL
SELECT '1' ,'x' ,'x' ,'4' UNION ALL
SELECT '2' ,'x' ,'x' ,'3' UNION ALL
SELECT '2' ,'x' ,'x' ,'5' UNION ALL
SELECT '3' ,'x' ,'x' ,'1'


update t1 set E =t.maxD from #temp t1 inner join
(select A, max(D) as maxD from #temp group by A) t on t.A=t1.A AND t1.d = t.maxd
select * from #temp

DROP TABLE #temp



-------------------------
Your time is a valuable resource.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-07-06 : 08:36:23
quote:
Originally posted by Mangal Pardeshi

Try

DECLARE @T TABLE
(
A INT,
D INT,
E INT
)
INSERT INTO @T(a,d) SELECT
1,1 UNION ALL SELECT
1,2 UNION ALL SELECT
1,4 UNION ALL SELECT
2,3 UNION ALL SELECT
2,5 UNION ALL SELECT
3,1
go

SELECT a,d,e
FROM @T

;WITH CTE AS
(
SELECT a,d,e,ROW_NUMBER()OVER(PARTITION BY A ORDER BY D DESC) AS SEQ
FROM @T
)
UPDATE CTE
SET e = d
WHERE SEQ=1


SELECT a,d,e
FROM @T




Mangal Pardeshi
http://mangalpardeshi.blogspot.com


or

select a,d,case when d=new_d then d else null end as e from
(
select a,d,e,max(d) over (partition by a) as new_d from @t
) as t

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-07-06 : 13:06:13
quote:
Originally posted by bhaasjoshi

Thanks for replying guys, but I'm a complete newbie and I'm writing this sql code in a different application for someone which doesnt support sql procedures etc. Is there a way by which I can rfer to the rows like, if currentval not equal to nextval (in column A) then column E =column D. or something like that.

Thanks again,
B



What platform are you writing this in? VB.net, Java?

You are going to get TSQL solutions on this board



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

bhaasjoshi
Starting Member

17 Posts

Posted - 2009-07-07 : 00:43:02
Its an application called "Integrated Review" if anyone knows more about this application then do tell me.
Go to Top of Page

bhaasjoshi
Starting Member

17 Posts

Posted - 2009-07-07 : 01:17:48
Thanks guys, I got it to work somehow drawing inspiration from Madhivanan's logic. This has been resolved.

Thanks once again.
B
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-07-07 : 01:42:11
quote:
Originally posted by bhaasjoshi

Thanks guys, I got it to work somehow drawing inspiration from Madhivanan's logic. This has been resolved.

Thanks once again.
B


You are welcome

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -