| Author |
Topic |
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2009-08-05 : 06:27:31
|
HiI have table like this..ROW_ID SNO A B C D1 6 10 4.320 70.680 2 7 10 6.200 68.8003 8 10 10.580 64.4204 9 10 6.770 68.230 I want to update the D column based on the C column values.ConditionsIn C columns values are like this...70.680 68.80064.42068.230 Take the C column values.FIRST ROW SECOND ROW 70.680 < 68.800, 68.800 < 64.420 The above two conditions are false so the D column should update 0 for both the rowsThe below conditions are true so the D column should update 1 64.420<68.230This checks will continue all rows in table…The output is like this…ROW_ID SNO A B C D1 6 10 4.320 70.680 02 7 10 6.200 68.800 03 8 10 10.580 64.420 14 9 10 6.770 68.230 0 Please help out this... |
|
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2009-08-05 : 07:04:06
|
| HiPls Help on this...Guys.. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-08-05 : 07:15:30
|
Should be something like this:update t1set D= case when t2.C is null then 0 when t1.C < t2.C then 1 else 0 endfrom your_table t1left join your_table t2 on t2.ROW_ID = t1.ROW_ID+1 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2009-08-05 : 08:16:46
|
HiI have test your query it was like this...73.390 034.350 020.573 015.995 010.820 09.612 07.530 04.863 15.222 03.546 01.980 12.976 0 But i want once the query update 1 for first time rest will all rows are zero...[/code]73.390 034.350 020.573 015.995 010.820 09.612 07.530 04.863 15.222 03.546 01.980 0 -- even this row also i need zero..2.976 0...[/code]pls help this |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-08-05 : 08:51:47
|
I am busy now, but you should try my statement using "UPDATE TOP 1 t1 ..."Not sure but give it a try! No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2009-08-06 : 03:41:30
|
| Hi Please help on this............ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-06 : 03:48:21
|
[code]DECLARE @Sample TABLE ( RowID INT, Sno INT, A INT, B MONEY, C MONEY, D TINYINT )INSERT @SampleSELECT 1, 6, 10, 4.320, 70.680, 0 UNION ALLSELECT 2, 7, 10, 6.200, 68.800, 0 UNION ALLSELECT 3, 8, 10, 10.580, 64.420, 0 UNION ALLSELECT 4, 9, 10, 6.770, 68.230, 0SELECT *FROM @SampleUPDATE sSET s.D = 1FROM @Sample AS sLEFT JOIN @Sample AS q ON q.RowID = s.RowID + 1WHERE s.C < q.CSELECT *FROM @Sample[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2009-08-06 : 04:17:09
|
| Hi Thanks peso |
 |
|
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2009-08-06 : 04:28:18
|
Hi PesoI have tested these dataINSERT @SampleSELECT 1, 6, 10, 4.320, 73.390, 0 UNION ALLSELECT 2, 7, 10, 6.200, 34.350, 0 UNION ALLSELECT 3, 8, 10, 10.580, 20.573, 0 UNION ALLSELECT 4, 9, 10, 6.770, 15.995, 0 UNION ALLSELECT 5, 9, 10, 6.770, 10.820, 0 UNION ALLSELECT 6, 9, 10, 6.770, 9.612, 0 UNION ALLSELECT 7, 9, 10, 6.770, 7.530, 0 UNION ALLSELECT 8, 9, 10, 6.770, 4.863, 0 UNION ALLSELECT 9, 9, 10, 6.770, 5.222, 0 UNION ALLSELECT 10, 9, 10, 6.770, 3.546, 0 UNION ALLSELECT 11, 9, 10, 6.770, 1.980, 0 UNION ALLSELECT 12, 9, 10, 39.29, 2.976, 0 I got the result set like this..1 6 10 4.32 73.39 02 7 10 6.20 34.35 03 8 10 10.58 20.573 04 9 10 6.77 15.995 05 9 10 6.77 10.82 06 9 10 6.77 9.612 07 9 10 6.77 7.53 08 9 10 6.77 4.863 19 9 10 6.77 5.222 010 9 10 6.77 3.546 011 9 10 6.77 1.98 1 --- Here again update one12 9 10 39.29 2.976 0 I need when condition meet first time there only it's has to update one rest all should be zero even the condition meets also..6 9 10 6.77 9.612 07 9 10 6.77 7.53 08 9 10 6.77 4.863 19 9 10 6.77 5.222 010 9 10 6.77 3.546 011 9 10 6.77 1.98 0--- like this...12 9 10 39.29 2.976 0 |
 |
|
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2009-08-06 : 04:47:54
|
| Hi Peso There........... |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-06 : 04:51:38
|
[code]DECLARE @Sample TABLE ( RowID INT, Sno INT, A INT, B MONEY, C MONEY, D TINYINT )INSERT @SampleSELECT 1, 6, 10, 4.320, 73.390, 0 UNION ALLSELECT 2, 7, 10, 6.200, 34.350, 0 UNION ALLSELECT 3, 8, 10, 10.580, 20.573, 0 UNION ALLSELECT 4, 9, 10, 6.770, 15.995, 0 UNION ALLSELECT 5, 9, 10, 6.770, 10.820, 0 UNION ALLSELECT 6, 9, 10, 6.770, 9.612, 0 UNION ALLSELECT 7, 9, 10, 6.770, 7.530, 0 UNION ALLSELECT 8, 9, 10, 6.770, 4.863, 0 UNION ALLSELECT 9, 9, 10, 6.770, 5.222, 0 UNION ALLSELECT 10, 9, 10, 6.770, 3.546, 0 UNION ALLSELECT 11, 9, 10, 6.770, 1.980, 0 UNION ALLSELECT 12, 9, 10, 39.29, 2.976, 0 SELECT *FROM @SampleUPDATE @SampleSET D = 1WHERE RowID IN ( SELECT MIN(s.RowID) FROM @Sample AS s LEFT JOIN @Sample AS q ON q.RowID = s.RowID + 1 WHERE s.C < q.C )SELECT *FROM @Sample[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2009-08-06 : 04:59:26
|
| Thanks............ |
 |
|
|
|
|
|