Instead of using SQL statement, I did it via code behind. This is my code:
Function GetTable() As DataTable
' Create new DataTable instance.
Dim table As New DataTable
table.Columns.Add("AREA")
Dim sqlHS As String = "SELECT DISTINCT HS_TYPE_T FROM Q_MCLOC WHERE HS_TYPE_T IS NOT NULL"
Dim dtHS As DataTable = DBLayer.Util.Data.MsSql.DBFunction.BuildDataTable(sqlHS, connEPJSetting)
For Each hs As DataRow In dtHS.Rows
If table.Columns(hs("HS_TYPE_T")) Is Nothing Then
table.Columns.Add(hs("HS_TYPE_T"), GetType(String))
End If
Next
table.Columns.Add("TOTAL")
Return table
End Function
Private Sub test()
Dim dt As DataTable = GetTable()
Dim sqlBlock As String = "SELECT DISTINCT LOCAREA_T FROM Q_MCLOC WHERE LOCAREA_T IS NOT NULL"
Dim dtBlock As DataTable = DBLayer.Util.Data.MsSql.DBFunction.BuildDataTable(sqlBlock, connEPJSetting)
For Each area As DataRow In dtBlock.Rows
Dim a As DataRow = dt.NewRow
Dim count, total As Integer
total = 0
a("AREA") = area("LOCAREA_T")
For Each s As DataColumn In dt.Columns
If s.ColumnName() <> "AREA" Then
Dim sqlCount As String = "SELECT COUNT(HS_TYPE_T) FROM Q_MCLOC WHERE LOCAREA_T = '" & area("LOCAREA_T") & "' AND HS_TYPE_T = '" & s.ColumnName() & "'"
count = DBLayer.Util.Data.MsSql.DBFunction.executeScalar(sqlCount, connEPJSetting)
a(s.ColumnName()) = count
'count += count
total = total + count
End If
Next
a("TOTAL") = total
dt.Rows.Add(a)
Next
Dim c As DataRow = dt.NewRow
For Each col As DataColumn In dt.Columns
Dim rowtotal As Integer = 0
If col.ColumnName() <> "AREA" Then
For Each row As DataRow In dt.Rows
Dim b As String = row(col.ColumnName())
rowtotal += CType(row(col.ColumnName()), Integer)
Next
c(col.ColumnName) = rowtotal
End If
Next
dt.Rows.Add(c)
GridView1.DataSource = dt
GridView1.DataBind()
and here is the result in Gridview.
AREA - HSST L-10YH L-10YM NAL SS4 YH YM TOTAL
1A 21 0 0 0 0 0 0 5 26
1B 17 0 0 0 0 0 0 0 17
2A 15 0 0 1 0 0 2 10 28
2B 16 0 0 0 0 0 0 0 16
3A 3 0 0 0 0 21 6 14 44
3B 0 17 0 0 0 0 3 4 24
4A 0 0 2 0 0 0 19 22 43
4B 0 0 0 1 0 0 15 11 27
5A 0 0 0 0 0 0 11 33 44
5B 0 0 0 0 0 0 27 0 27
6A 0 0 0 2 0 0 14 27 43
6B 0 0 0 0 0 0 27 1 28
7A 0 0 0 0 0 0 11 32 43
7B 0 0 0 2 0 0 3 22 27
8A 0 0 0 0 11 0 14 6 31
8B 0 0 0 0 0 0 10 13 23
72 17 2 6 11 21 162 200 491
To produce such result using code behind, it might consume more time because need to use for loop. If anybody knows better solution (using SQL statement preferable), please let me know. Thank you