Hi, i'm a newbie and get stuck with this problem for a few days.
I have datatable with two column, line and duration.
I want to sum the duration based on the line group. For example:
I want the output to be like this:
I cant do this straight from Oracle query because column duration is stored as Char in the database.
So i hava to convert the column to timespan and store in another column in the datatable. Here is the code:
the problem now, i get stuck with how to group the line and get the sum of duration for each line.
Can anyone help me pls :apologetic:
I have datatable with two column, line and duration.
I want to sum 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 cant do this straight from Oracle query because column duration is stored as Char in the database.
So i hava to convert the column to timespan and store in another column in the datatable. Here is the code:
VB.NET:
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
the problem now, i get stuck with how to group the line and get the sum of duration for each line.
Can anyone help me pls :apologetic: