1

I have datatable with two columns; line and duration. I want the sum of the duration, based on the line group. For example:

LINE  DURATION 
A1    00:05:20 
A1    00:07:00 
A1    00:02:10 
A1    00:01:50 
A2    00:02:01 
A2    00:03:45

I want the output to be like this:

LINE  DURATION 
A1    00:14:30 
A2    00:05:46

I can't do this straight from Oracle query because the column duration is stored as a Char in the database. So I have to convert the column to timespan and store in another column in the datatable. Here is the code:

Imports System.Data
Imports Oracle.DataAccess.Client ' ODP.NET Oracle managed provider
Imports Oracle.DataAccess.Types

Partial Public Class WebForm3

Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim li As ListItem

For Each li In DDL1.Items()
If li.Selected = True Then
If Len(Trim(strLine)) <> 0 Then
strLine = strLine & "','" & li.Text
Else
strLine = li.Text
End If
End If
Next


End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim oradb As String = "Data Source=XXXXX;User Id=XXXXX;Password=XXXXX;"
Dim conn As New OracleConnection(oradb)
Dim cmd As New OracleCommand
Dim myDataAdapter As New OracleDataAdapter
Dim mySelect As String


    Try

        conn.Open()

        mySelect = "SELECT REFLINKTBL.LINENAME, JSFACTOR.EVENTTM  "
        mySelect = mySelect & " FROM (SELECT JSLINE.LINENAME, JSMC.RECID, JSMC.MCNAME, JSMC.GOH "
        mySelect = mySelect & " FROM JSLINE RIGHT OUTER JOIN  "
        mySelect = mySelect & " JSMC ON JSLINE.RECID = JSMC.ID_JSLINE) REFLINKTBL INNER JOIN  "
        mySelect = mySelect & " JSFACTOR ON JSFACTOR.ID_JSMC = REFLINKTBL.RECID  "
        mySelect = mySelect & " WHERE JSFACTOR.YOUIN LIKE '%CSTTRB%'  "
        mySelect = mySelect & " AND (JSFACTOR.EVENTST BETWEEN '2012/08/06 00:00:00' AND '2012/08/12 00:00:00') "
        mySelect = mySelect & " AND LINENAME IN ('" & strLine & "')  "
        mySelect = mySelect & " ORDER BY LINENAME, JSFACTOR.EVENTST "

        myDataAdapter = New OracleDataAdapter(mySelect, conn)


        Dim Ds As New DataSet()
        GridView1.DataSource = Ds
        myDataAdapter.Fill(Ds)


        Dim dc As DataColumn

        dc = New DataColumn("DURATION", GetType(TimeSpan))

        Ds.Tables(0).Columns.Add(dc)


        Dim linename As DataColumn = Ds.Tables(0).Columns(0)
        Dim duration As DataColumn = Ds.Tables(0).Columns(2)


        For i As Integer = 0 To Ds.Tables(0).Rows.Count - 1
            Ds.Tables(0).Rows(i).Item(duration) = TimeSpan.Parse(Ds.Tables(0).Rows(i).Item("EVENTTM"))
        Next


        Ds.Tables(0).Columns.Remove("EVENTTM")



        GridView1.DataSource = Ds
        GridView1.DataBind()


    Catch ex As OracleException

        MsgBox("Error: " & ex.ToString())

    End Try



End Sub

Can anyone help me group the line and get the sum of duration for each line?

4

0 回答 0