| Author |
Topic |
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-01-30 : 21:42:41
|
| Hi Guys,I have the a table with the folowing structure:Seq ID1 Qty1 ID2 Qty21 1 107 1 112 2 121 1 113 3 130 2 164 3 130 3 125 5 102 4 226 4 123 5 347 4 123 6 138 4 123 7 189 6 100 8 19I will like to set the values of Qty1 and Qty2 to zero (0)except the first record for records with duplicate values of ID1 and ID2EXPECTED RESULS:Seq ID1 Qty1 ID2 Qty21 1 107 1 112 2 121 1 03 3 130 2 164 3 0 3 125 5 102 4 226 4 123 5 347 4 0 6 138 4 0 7 189 6 100 8 19Any help will be welcomed |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-01-30 : 21:56:28
|
[code]DECLARE @sample TABLE( Seq int, ID1 int, Qty1 int, ID2 int, Qty2 int)INSERT INTO @sampleSELECT 1, 1, 107, 1, 11 UNION ALLSELECT 2, 2, 121, 1, 11 UNION ALLSELECT 3, 3, 130, 2, 16 UNION ALLSELECT 4, 3, 130, 3, 12 UNION ALLSELECT 5, 5, 102, 4, 22 UNION ALLSELECT 6, 4, 123, 5, 34 UNION ALLSELECT 7, 4, 123, 6, 13 UNION ALLSELECT 8, 4, 123, 7, 18 UNION ALLSELECT 9, 6, 100, 8, 19SELECT Seq, ID1, Qty1, ID2, Qty2 = CASE WHEN row_no = 1 THEN Qty2 ELSE 0 ENDFROM( SELECT Seq, ID1, Qty1, ID2, Qty2, row_no = row_number() OVER (PARTITION BY ID2 ORDER BY ID2, ID1) FROM @sample) s/*Seq ID1 Qty1 ID2 Qty2 ----------- ----------- ----------- ----------- ----------- 1 1 107 1 11 2 2 121 1 0 3 3 130 2 16 4 3 130 3 12 5 5 102 4 22 6 4 123 5 34 7 4 123 6 13 8 4 123 7 18 9 6 100 8 19 (9 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-01-30 : 22:07:22
|
| Thanks for your response, Qty1 for ID1=3 and 4 should also be set to 0exept the first case.How wilImodify you script to address this ? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-01-30 : 22:13:16
|
Same technique
DECLARE @sample TABLE( Seq int, ID1 int, Qty1 int, ID2 int, Qty2 int)INSERT INTO @sampleSELECT 1, 1, 107, 1, 11 UNION ALLSELECT 2, 2, 121, 1, 11 UNION ALLSELECT 3, 3, 130, 2, 16 UNION ALLSELECT 4, 3, 130, 3, 12 UNION ALLSELECT 5, 5, 102, 4, 22 UNION ALLSELECT 6, 4, 123, 5, 34 UNION ALLSELECT 7, 4, 123, 6, 13 UNION ALLSELECT 8, 4, 123, 7, 18 UNION ALLSELECT 9, 6, 100, 8, 19SELECT Seq, ID1, Qty1 = CASE WHEN row_no1 = 1 THEN Qty1 ELSE 0 END, ID2, Qty2 = CASE WHEN row_no2 = 1 THEN Qty2 ELSE 0 ENDFROM( SELECT Seq, ID1, Qty1, ID2, Qty2, row_no1 = row_number() OVER (PARTITION BY ID1 ORDER BY ID1, ID2), row_no2 = row_number() OVER (PARTITION BY ID2 ORDER BY ID2, ID1) FROM @sample) s/*Seq ID1 Qty1 ID2 Qty2 ----------- ----------- ----------- ----------- ----------- 1 1 107 1 11 2 2 121 1 0 3 3 130 2 16 4 3 0 3 12 5 5 102 4 22 6 4 123 5 34 7 4 0 6 13 8 4 0 7 18 9 6 100 8 19 (9 row(s) affected)*/ KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-01-31 : 06:30:37
|
| Sorry khtan,What will be an altenate solution inSQL Server 2000. Sorry for the inconvinience. Thanks for your elegant solution. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-31 : 06:37:23
|
quote: Originally posted by ucal Sorry khtan,What will be an altenate solution inSQL Server 2000. Sorry for the inconvinience. Thanks for your elegant solution.
Here's a sql 2000 compatible solutionSELECT Seq, ID1, Qty1 = CASE WHEN row_no1 = 1 THEN Qty1 ELSE 0 END, ID2, Qty2 = CASE WHEN row_no2 = 1 THEN Qty2 ELSE 0 ENDFROM( SELECT Seq, ID1, Qty1, ID2, Qty2, (SELECT COUNT(*) + 1 FROM @sample WHERE ID1=t.ID1 AND Seq<t.Seq) AS row_no1, (SELECT COUNT(*) + 1 FROM @sample WHERE ID2=t.ID2 AND Seq<t.Seq) AS row_no2 FROM @sample t) s |
 |
|
|
|
|
|