Question : Increment values based on other column value

Hi Guys,

I need some help with the SQL. I need to check for the IsValid Value and then derive the new column value.. this should be done per PID,PHId. So, I need to check iSValid Value and if it's "0" then "0" Else Increment by 1 starting with 1 for the whole set until the next IsValue = 0.. Any suggestions Please

Code Snippet  - 806554 has all the sql required to create the structure of the table and insert test values (Id is an indentity column and table has a Clustered index on Id,PID,PHId and Non clustered Index on Date column )

Code Snippet - 806536 has the structure of the output data

Guys please help me out.. Thanks a lot
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
REQUIRED OUTPUT DATA STRUCTURE

Create Table ##TEST1
(
Id INT,
PHId INT,
PId INT,
Rate Float,
IsValid BIT,
SectionId INT
PDate DateTime,
)

 SELECT 164128,2,       1195894,        60,1,1,'2010-07-02 09:20:00.000' UNION ALL
 SELECT 164129,2,       1195894,        80,1,1,'2010-07-02 09:25:00.000' UNION ALL
 SELECT 164130,2,       1195894,        10,1,1,'2010-07-02 09:30:00.000' UNION ALL
 SELECT 164131,2,       1195894,        60,1,1,'2010-07-02 09:35:00.000' UNION ALL
 SELECT 164132,2,1195894,0,0,0,'2010-07-02 09:40:00.000' UNION ALL
 SELECT 164133,2,       1195894,        0,0,0,'2010-07-02 09:45:00.000' UNION ALL
 SELECT 164134,2,       1195894,        0,0,0,'2010-07-02 09:50:00.000' UNION ALL
 SELECT 164135,2,       1195894,        0,0,0,'2010-07-02 09:55:00.000' UNION ALL
 SELECT 164136,2,       1195894,        0,0,0,'2010-07-02 10:00:00.000' UNION ALL
 SELECT 164137,2,       1195894,        0,0,0,'2010-07-02 10:05:00.000' UNION ALL
 SELECT 164138,2,       1195894,        0,0,0,'2010-07-02 10:10:00.000' UNION ALL
 SELECT 164139,2,       1195894,        0,0,0,'2010-07-02 10:15:00.000' UNION ALL
 SELECT 164140,2,       1195894,        0,0,0,'2010-07-02 10:20:00.000' UNION ALL
 SELECT 164141,2,       1195894,        0,0,0,'2010-07-02 10:25:00.000' UNION ALL
 SELECT 164142,2,       1195894,        0,0,0,'2010-07-02 10:30:00.000' UNION ALL
 SELECT 164143,2,       1195894,        20,1,2,'2010-07-02 10:35:00.000' UNION ALL
 SELECT 164144,2,       1195894,        80,1,2,'2010-07-02 10:40:00.000' UNION ALL
 SELECT 164145,2,       1195894,        0,1,2,'2010-07-02 10:45:00.000' UNION ALL
 SELECT 164146,2,       1195894,        0,1,2,'2010-07-02 10:50:00.000' UNION ALL
 
 SELECT 164147,1,       1195894,        0,0,0,'2010-07-02 10:05:00.000' UNION ALL
 SELECT 164148,1,       1195894,        0,0,0,'2010-07-02 10:10:00.000' UNION ALL
 SELECT 164149,1,       1195894,        0,0,0,'2010-07-02 10:15:00.000' UNION ALL
 SELECT 164150,1,       1195894,        0,0,0,'2010-07-02 10:20:00.000' UNION ALL
 SELECT 164151,1,       1195894,        0,0,0,'2010-07-02 10:25:00.000' UNION ALL
 SELECT 164152,1,       1195894,        0,0,0,'2010-07-02 10:30:00.000' UNION ALL
 SELECT 164153,1,       1195894,        20,1,1,'2010-07-02 10:35:00.000' UNION ALL
 SELECT 164154,1,       1195894,        80,1,1,'2010-07-02 10:40:00.000' UNION ALL
 SELECT 164155,1,       1195894,        0,1,1,'2010-07-02 10:45:00.000' UNION ALL
 SELECT 164156,1,       1195894,        0,1,1,'2010-07-02 10:50:00.000' UNION ALL
 
 SELECT 164157,1,       1195890,        0,1,1,'2010-07-02 10:05:00.000' UNION ALL
 SELECT 164158,1,       1195890,        0,1,1,'2010-07-02 10:10:00.000' UNION ALL
 SELECT 164159,1,       1195890,        0,1,1,'2010-07-02 10:15:00.000' UNION ALL
 SELECT 164160,1,       1195890,        0,0,0,'2010-07-02 10:20:00.000' UNION ALL
 SELECT 164161,1,       1195890,        0,0,0,'2010-07-02 10:25:00.000' UNION ALL
 SELECT 164162,1,       1195890,        0,0,0,'2010-07-02 10:30:00.000' UNION ALL
 SELECT 164163,1,       1195890,        20,1,2,'2010-07-02 10:35:00.000' UNION ALL
 SELECT 164164,1,       1195890,        0,1,2,'2010-07-02 10:40:00.000' UNION ALL
 SELECT 164165,1,       1195890,        0,0,0,'2010-07-02 10:45:00.000' UNION ALL
 SELECT 164166,1,       1195890,        20,1,3,'2010-07-02 10:50:00.000' UNION ALL
 SELECT 164165,1,       1195890,        20,1,3,'2010-07-02 10:45:00.000' UNION ALL
 SELECT 164165,1,       1195890,        20,1,3,'2010-07-02 10:45:00.000'
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
Create Table ##TEST
(
Id INT,
PHId INT,
PId INT,
Rate Float,
IsValid BIT,
PDate DateTime,
)
 
 SELECT 164128,2,1195894,60,1,'2010-07-02 09:20:00.000' UNION ALL
 SELECT 164129,2,1195894,80,1,'2010-07-02 09:25:00.000' UNION ALL
 SELECT 164130,2,1195894,10,1,'2010-07-02 09:30:00.000' UNION ALL
 SELECT 164131,2,1195894,60,1,'2010-07-02 09:35:00.000' UNION ALL
 SELECT 164132,2,1195894,0,0,'2010-07-02 09:40:00.000' UNION ALL
 SELECT 164133,2,       1195894,        0,0,'2010-07-02 09:45:00.000' UNION ALL
 SELECT 164134,2,       1195894,        0,0,'2010-07-02 09:50:00.000' UNION ALL
 SELECT 164135,2,       1195894,        0,0,'2010-07-02 09:55:00.000' UNION ALL
 SELECT 164136,2,       1195894,        0,0,'2010-07-02 10:00:00.000' UNION ALL
 SELECT 164137,2,       1195894,        0,0,'2010-07-02 10:05:00.000' UNION ALL
 SELECT 164138,2,       1195894,        0,0,'2010-07-02 10:10:00.000' UNION ALL
 SELECT 164139,2,       1195894,        0,0,'2010-07-02 10:15:00.000' UNION ALL
 SELECT 164140,2,       1195894,        0,0,'2010-07-02 10:20:00.000' UNION ALL
 SELECT 164141,2,       1195894,        0,0,'2010-07-02 10:25:00.000' UNION ALL
 SELECT 164142,2,       1195894,        0,0,'2010-07-02 10:30:00.000' UNION ALL
 SELECT 164143,2,       1195894,        20,1,'2010-07-02 10:35:00.000' UNION ALL
 SELECT 164144,2,       1195894,        80,1,'2010-07-02 10:40:00.000' UNION ALL
 SELECT 164145,2,       1195894,        0,1,'2010-07-02 10:45:00.000' UNION ALL
 SELECT 164146,2,       1195894,        0,1,'2010-07-02 10:50:00.000' UNION ALL
 
 SELECT 164147,1,       1195894,        0,0,'2010-07-02 10:05:00.000' UNION ALL
 SELECT 164148,1,       1195894,        0,0,'2010-07-02 10:10:00.000' UNION ALL
 SELECT 164149,1,       1195894,        0,0,'2010-07-02 10:15:00.000' UNION ALL
 SELECT 164150,1,       1195894,        0,0,'2010-07-02 10:20:00.000' UNION ALL
 SELECT 164151,1,       1195894,        0,0,'2010-07-02 10:25:00.000' UNION ALL
 SELECT 164152,1,       1195894,        0,0,'2010-07-02 10:30:00.000' UNION ALL
 SELECT 164153,1,       1195894,        20,1,'2010-07-02 10:35:00.000' UNION ALL
 SELECT 164154,1,       1195894,        80,1,'2010-07-02 10:40:00.000' UNION ALL
 SELECT 164155,1,       1195894,        0,1,'2010-07-02 10:45:00.000' UNION ALL
 SELECT 164156,1,       1195894,        0,1,'2010-07-02 10:50:00.000' UNION ALL
 
 SELECT 164157,1,1195890,0,1,'2010-07-02 10:05:00.000' UNION ALL
 SELECT 164158,1,1195890,0,1,'2010-07-02 10:10:00.000' UNION ALL
 SELECT 164159,1,1195890,0,1,'2010-07-02 10:15:00.000' UNION ALL
 SELECT 164160,1,1195890,0,0,'2010-07-02 10:20:00.000' UNION ALL
 SELECT 164161,1,1195890,0,0,'2010-07-02 10:25:00.000' UNION ALL
 SELECT 164162,1,1195890,0, 0,'2010-07-02 10:30:00.000' UNION ALL
 SELECT 164163,1,1195890,20,1,'2010-07-02 10:35:00.000' UNION ALL
 SELECT 164164,1,1195890,0, 1,'2010-07-02 10:40:00.000' UNION ALL
 SELECT 164165,1,1195890,0,0,'2010-07-02 10:45:00.000' UNION ALL
 SELECT 164166,1,1195890,20,1,'2010-07-02 10:50:00.000' UNION ALL
 SELECT 164165,1,1195890,20,1,'2010-07-02 10:45:00.000' UNION ALL
 SELECT 164165,1,1195890,20,1,'2010-07-02 10:45:00.000'

Answer : Increment values based on other column value

The company providing the co-location is your hosting ISP.  Ask them if they do anything to prevent IP address spoofing [of your assigned addresses].
Random Solutions  
 
programming4us programming4us