我有两列的数据表;线和持续时间。 我想要基于线组的持续时间总和。例如:
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
我希望输出如下:
LINE DURATION
A1 00:14:30
A2 00:05:46
我无法直接从Oracle查询中执行此操作,因为列持续时间在数据库中存储为Char。 所以我必须将列转换为时间跨度并存储在数据表中的另一列中。这是代码:
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
任何人都可以帮我分组线并获得每条线的持续时间总和吗?