G
Guest
I have a SQL table comprised of 31 columns.
The first column is simply an id column, the next 30 columns are labelled
[1],[2]...[30].
The numerical columns have a tinyint type and the data stored is either 1 or
null.
I wish to count the number of times a one appears in one column
simultaneously with another column:
eg count the number of times 1 appears in column [1] and 1 also appears in
column[15] in the same row:
Column [1] Column[15]
1 1 Count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count
1 Don't count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count,
etc.
The following has been suggested to me as a possible solution:
Imports MWFN
Partial Class MembersPages_MyPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim pairsAdapter As New MWFNTableAdapters.MWFNTableAdapter
Dim pairs As MWFN.MWFNDataTable
pairs = pairsAdapter.GetData
Dim myColumns As Data.DataColumnCollection = pairs.Columns
Dim columnA As Data.DataColumn
Dim columnB As Data.DataColumn
Dim num3 As Hashtable
For Each columnA In myColumns
If columnA.ColumnName <> "Id" Then
For Each columnB In myColumns
If columnB.ColumnName <> "Id" And columnB.Ordinal >
columnA.Ordinal Then
'if columnB's name doesn't equal Id and columnB's
position number is over columnA's, sum columnA and columnB
num3.Add(columnA.ColumnName & "," &
columnB.ColumnName, pairs.Compute("SUM(" & columnA.ColumnName & ") + SUM( " &
columnB.ColumnName & ")", ""))
End If
Next
End If
Next
End Sub
End Class
I have used a dataset to obtain the columns from the database datatable in
an effort to use a data access layer in my project. The dataset is MWFN.
The above code produces the following:
Warning: num3 is used before it has been assigned a value.
And when the programme is run:
Expecting a single column argument with possible child qualifier.
At this point in time , the code only sums/counts two columns. I will wish
to broaden that to sum/count 3 , 4 and possibly 5 columns, so the code needs
to be extensible. The number of columns will alos vary (probably from 27 to
59).
It doesn't matter if your suggestion is to use transact sql is a stored
procedure before the data gets to the project, or use vb as per the above
mentioned suggestion.
Any assistance would be appreciated.
Thank you
onecorp
Can anyone help point me in the right direction
The first column is simply an id column, the next 30 columns are labelled
[1],[2]...[30].
The numerical columns have a tinyint type and the data stored is either 1 or
null.
I wish to count the number of times a one appears in one column
simultaneously with another column:
eg count the number of times 1 appears in column [1] and 1 also appears in
column[15] in the same row:
Column [1] Column[15]
1 1 Count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count
1 Don't count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count,
etc.
The following has been suggested to me as a possible solution:
Imports MWFN
Partial Class MembersPages_MyPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim pairsAdapter As New MWFNTableAdapters.MWFNTableAdapter
Dim pairs As MWFN.MWFNDataTable
pairs = pairsAdapter.GetData
Dim myColumns As Data.DataColumnCollection = pairs.Columns
Dim columnA As Data.DataColumn
Dim columnB As Data.DataColumn
Dim num3 As Hashtable
For Each columnA In myColumns
If columnA.ColumnName <> "Id" Then
For Each columnB In myColumns
If columnB.ColumnName <> "Id" And columnB.Ordinal >
columnA.Ordinal Then
'if columnB's name doesn't equal Id and columnB's
position number is over columnA's, sum columnA and columnB
num3.Add(columnA.ColumnName & "," &
columnB.ColumnName, pairs.Compute("SUM(" & columnA.ColumnName & ") + SUM( " &
columnB.ColumnName & ")", ""))
End If
Next
End If
Next
End Sub
End Class
I have used a dataset to obtain the columns from the database datatable in
an effort to use a data access layer in my project. The dataset is MWFN.
The above code produces the following:
Warning: num3 is used before it has been assigned a value.
And when the programme is run:
Expecting a single column argument with possible child qualifier.
At this point in time , the code only sums/counts two columns. I will wish
to broaden that to sum/count 3 , 4 and possibly 5 columns, so the code needs
to be extensible. The number of columns will alos vary (probably from 27 to
59).
It doesn't matter if your suggestion is to use transact sql is a stored
procedure before the data gets to the project, or use vb as per the above
mentioned suggestion.
Any assistance would be appreciated.
Thank you
onecorp
Can anyone help point me in the right direction