Hi
I have a datatable which has 3 column. for one value of first column has many different value and some of them duplicated value in the second column. I want split the rows that are same value in second column.
first second third
1 5 d
1 5 w
1 8 q
1 5 y
1 8 h
2 4 p
1 5 l
2 4 e
I want category be as same below:
view1:
1 5 d
1 5 w
1 5 y
1 5 l
view2:
1 8 h
1 8 q
view3:
2 4 e
2 4 p
Please help me
I need urgently
Loading
Dhirendra MisraPosted Feb 5, 2014, 3:34 AM
diamond diamondPosted Feb 4, 2014, 12:03 PM
Thanks a lot. of course I want to save same as view in a text file, because after this split, I should use them in formul. Are you have any idea??
Thanks
Dhirendra MisraPosted Feb 4, 2014, 5:01 AM
If it is DataTable of c#, you need to first sort the table and then use foreach to loop each values and create views. Like pseudo code as below:
int oldValue = -1;
int currValue = -1;
int viewCounter = 0;
dt.DefaultView.Sort = dt.Columns[1].ColumnName + " DESC"; //2nd column
foreach (DataRow row in dt.Rows) // Loop over the rows.
{
currValue = convert.Int32(row[1]);
if (currValue != oldValue)
{
viewCounter++;
oldValue = currValue;
Console.WriteLine("view" + viewCounter.toString());
//Print columns : Like row[0], row[1], row[2]
}
else
{
//Print columns : Like row[0], row[1], row[2]
}
}
Hope it will help you.
Khan Abrar AhmedPosted Feb 4, 2014, 2:24 AM
if your are using sql then use.
DECLARE @t AS TABLE ([FIRST] INT, [second] INT ,[third] VARCHAR(4))
INSERT INTO @t
VALUES
(1 , 5, 'd'),
(1 , 5 , 'w'),
(1 , 8 , 'q'),
(1 , 5 ,'y'),
(1 , 8 ,'h'),
(2 , 4 ,'p'),
(1 , 5 ,'l'),
(2 , 4 ,'e')
SELECT * FROM @t AS t WHERE FIRST =1 AND second=5
SELECT * FROM @t AS t WHERE FIRST =1 AND second=8
SELECT * FROM @t AS t WHERE FIRST =2 AND second=4