Oki ahorita me pondre a leer pero solo le dejo lo que tenia y lo que hice :D y disculpeme por no tener la calidad correcta :/ aun estoy estudiando y pues hago muchas locuras XD igual mi lojica no es la mejor y se que no trata de que me salio :$
Código vb:
Ver originalPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'BDReportesDataSet.mi_vista' table. You can move, or remove it, as needed.
Me.Mi_vistaTableAdapter.Fill(Me.BDReportesDataSet.mi_vista)
Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim Servidor As String = "USUARIO-PC\SQL2008R2"
Dim BaseDatos As String = "BDReportes"
Dim cn As New SqlConnection("Data Source=" & Servidor & ";Initial Catalog=" & BaseDatos & ";Integrated Security=SSPI;")
Dim dt As New DataTable
With cmd
.CommandType = CommandType.Text
.CommandText = "Select NumCta,Nombre,SegNeg, SUM (periodo_pasado) AS Periodo_Pasado, SUM (Cargo) AS Cargo, SUM (Abono) AS Abono, Periodo_Pasado+Cargo-Abono AS SaldoFinal from (Select NumCta,Nombre,SegNeg, SUM (periodo_pasado) AS Periodo_Pasado, SUM (Cargo) AS Cargo, SUM (Abono) AS Abono from (select t1.Fecha,t1.NumCta,t1.Nombre,t1.SegNeg,t1.Cargo,t1.Abono,t1.Saldo_Inicial,t2.saldo_inicial periodo_pasado from(SELECT *FROM mi_vista WHERE Fecha BETWEEN '20120910' AND '20120930') as t1 left join(select NumCta,SegNeg,SUM(cargo) cargo ,SUM(abono) abono ,SUM(saldo_inicial) saldo_inicial from mi_vista WHERE Fecha BETWEEN '20121001' AND '20121007' group by NumCta,Nombre,SegNeg) as t2 on (t1.NumCta=t2.NumCta and t1.NumCta=t2.NumCta)) as completo group by NumCta,Nombre,SegNeg) as x group by NumCta,Nombre,SegNeg,Periodo_Pasado,Cargo,Abono"
.Connection = cn
End With
da.SelectCommand = cmd
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
LO QUE HICE
Código vb:
Ver originalPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim Servidor As String = "USUARIO-PC\SQL2008R2"
Dim BaseDatos As String = "BDReportes"
Dim cn As New SqlConnection("Data Source=" & Servidor & ";Initial Catalog=" & BaseDatos & ";Integrated Security=SSPI;")
Dim dt As New DataTable
Dim TxtfechaIni As Date
Dim TxtfechaFin As Date
Dim TxtfechaIni2 As Date
Dim TxtfechaFin2 As Date
With cmd
.CommandType = CommandType.Text
.CommandText = "Select NumCta,Nombre, SUM (periodo_pasado) AS Periodo_Pasado, SUM (Cargo) AS Cargo, SUM (Abono) AS Abono, Periodo_Pasado+Cargo-Abono AS SaldoFinal from (Select NumCta,Nombre,SegNeg, SUM (periodo_pasado) AS Periodo_Pasado, SUM (Cargo) AS Cargo, SUM (Abono) AS Abono from (select t1.Fecha,t1.NumCta,t1.Nombre,t1.SegNeg,t1.Cargo,t1.Abono,t1.Saldo_Inicial,t2.saldo_inicial periodo_pasado from(SELECT *FROM mi_vista WHERE Fecha " & _
"BETWEEN #" & Format(CDate(TxtfechaIni), "dd/MM/yyyy") & "#" & _
"AND #" & Format(CDate(TxtfechaFin), "dd/MM/yyyy") & "#" & _
")as t1 left join" & _
"(select NumCta,SegNeg,SUM(cargo) cargo ,SUM(abono) abono ,SUM(saldo_inicial) saldo_inicial from mi_vista WHERE Fecha" & _
"BETWEEN #" & Format(CDate(TxtfechaIni2), "dd/MM/yyyy") & "#" & _
"AND #" & Format(CDate(TxtfechaFin2), "dd/MM/yyyy") & "#" & _
"Group by termino NumCta,Nombre,Periodo_Pasado,Cargo,Abono" & _
") as t2 on (t1.NumCta=t2.NumCta and t1.NumCta=t2.NumCta)" & _
") as completo group by NumCta,Nombre,SegNeg" & _
") as x group by NumCta,Nombre,Periodo_Pasado,Cargo,Abono"
.Connection = cn
End With
da.SelectCommand = cmd
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Pero sigo trabajando en ello....