| Author |
Topic |
|
bhaasjoshi
Starting Member
17 Posts |
Posted - 2009-07-06 : 07:21:20
|
| Hi,I have a tableA B C D1 x x 11 x x 21 x x 42 x x 32 x x 53 x x 1Its sorted by column A and then by D, I need the table to look like this,A B C D E1 x x 1 1 x x 2 1 x x 4 42 x x 3 2 x x 5 53 x x 1 1Please 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. |
 |
|
|
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.ASenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
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.ASenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
hi senthil_nagoredid u tried this which u have mention..-------------------------Your time is a valuable resource. |
 |
|
|
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 canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-07-06 : 07:54:25
|
TryDECLARE @T TABLE(A INT, D INT,E INT)INSERT INTO @T(a,d) SELECT1,1 UNION ALL SELECT1,2 UNION ALL SELECT1,4 UNION ALL SELECT2,3 UNION ALL SELECT2,5 UNION ALL SELECT3,1goSELECT a,d,eFROM @T;WITH CTE AS(SELECT a,d,e,ROW_NUMBER()OVER(PARTITION BY A ORDER BY D DESC) AS SEQFROM @T)UPDATE CTESET e = dWHERE SEQ=1SELECT a,d,eFROM @T Mangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
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 canceledhttp://senthilnagore.blogspot.com/
You need add one more conditionon t.A=t1.AAND t1.d = t.maxdMangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
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 |
 |
|
|
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 canceledhttp://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 ALLSELECT '1' ,'x' ,'x' ,'1' UNION ALLSELECT '1', 'x' ,'x' ,'2' UNION ALL SELECT '1' ,'x' ,'x' ,'4' UNION ALLSELECT '2' ,'x' ,'x' ,'3' UNION ALLSELECT '2' ,'x' ,'x' ,'5' UNION ALLSELECT '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.maxdselect * from #tempDROP TABLE #temp-------------------------Your time is a valuable resource. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-06 : 08:36:23
|
quote: Originally posted by Mangal Pardeshi TryDECLARE @T TABLE(A INT, D INT,E INT)INSERT INTO @T(a,d) SELECT1,1 UNION ALL SELECT1,2 UNION ALL SELECT1,4 UNION ALL SELECT2,3 UNION ALL SELECT2,5 UNION ALL SELECT3,1goSELECT a,d,eFROM @T;WITH CTE AS(SELECT a,d,e,ROW_NUMBER()OVER(PARTITION BY A ORDER BY D DESC) AS SEQFROM @T)UPDATE CTESET e = dWHERE SEQ=1SELECT a,d,eFROM @T Mangal Pardeshihttp://mangalpardeshi.blogspot.com
orselect 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 tMadhivananFailing to plan is Planning to fail |
 |
|
|
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 boardBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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 MadhivananFailing to plan is Planning to fail |
 |
|
|
|