Principales respuestas
ayuda en consulta sql 2008

Pregunta
-
buenas noches con todos tengo 12 consulta para enero,febrero,marzo....,diciembre solo estoy mostrando de enero y ferbero
para enero
Dim desde As Date = DTPdesde.Value.Date
Dim hasta As Date = DTPhasta.Value.Date
Dim cmd As New SqlCommand("select o.LOCAL,o.EESS,o.RUC,o.CLIENTE,(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '1') [ENE" & (Year(DTPhasta.Value) - 1) & "],(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '12') [DIC" & (Year(DTPhasta.Value) - 1) & "],o.AÑO,o.ENE,o.FEB,o.MAR,o.ABR,o.MAY,o.JUN,o.JUL,o.AGO,o.SEP,o.OCT,o.NOV,o.DIC,o.[A_ENE" & Year(DTPhasta.Value) & "],(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '1') [A_ENE" & (Year(DTPhasta.Value) - 1) & "],((o.[A_ENE" & Year(DTPhasta.Value) & "]) - (select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '1')) [A_DIF],(o.ENE-(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '12')) [ENE" & Year(DTPhasta.Value) & "-DIC" & (Year(DTPhasta.Value) - 1) & "],(o.ENE-(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '1')) [ENE" & Year(DTPhasta.Value) & "-ENE" & (Year(DTPhasta.Value) - 1) & "] from ( select LOCAL,EESS,RUC,CLIENTE,AÑO,ISNULL([1], 0) ENE, ISNULL([2], 0) FEB, " & _
"ISNULL([3], 0) MAR,ISNULL([4], 0) ABR, ISNULL([5], 0) MAY, " & _
"ISNULL([6], 0) JUN,ISNULL([7], 0) JUL,ISNULL([8], 0) AGO, " & _
"ISNULL([9], 0) SEP, ISNULL([10], 0) OCT, " & _
"ISNULL([11], 0) NOV, ISNULL([12], 0) DIC, " & _
"(ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) + ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) + ISNULL([11], 0) + ISNULL([12], 0)) as [A_ENE" & Year(DTPhasta.Value) & "] " & _
"from( " & _
"select cv.cdlocal as LOCAL,ee.estacion as EESS,cv.ruccliente as RUC,cli.rscliente as CLIENTE,year(cv.fecproceso) as AÑO,month(cv.fecproceso) as Mes,cv.cantidad as Cantidad " & _
"from cuadredevtas cv inner join clientes cli on cv.ruccliente = cli.ruccliente inner join eess ee on cv.cdlocal = ee.cdlocal where cv.ruccliente like '%" & TxtRuc.Text.Trim & "%' and cv.rscliente like '%" & TXTclientes.Text.Trim & "%' and ee.estacion like '%" & CmbEstacion.Text & "%' and cv.fecproceso >= @desde and cv.fecproceso <= @hasta ) V " & _
"PIVOT(sum(Cantidad) FOR Mes IN ([1], [2], [3], [4], [5],[6],[7], [8], [9], [10], [11], [12]) ) as PT) as o order by o.EESS,o.ENE desc", Cn)
cmd.Parameters.AddWithValue("@desde", desde)
cmd.Parameters.AddWithValue("@hasta", hasta)
para febrero
Dim desde As Date = DTPdesde.Value.Date
Dim hasta As Date = DTPhasta.Value.Date
Dim cmd As New SqlCommand("select o.LOCAL,o.EESS,o.RUC,o.CLIENTE,(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '2') [FEB" & (Year(DTPhasta.Value) - 1) & "],(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '12') [DIC" & (Year(DTPhasta.Value) - 1) & "],o.AÑO,o.ENE,o.FEB,o.MAR,o.ABR,o.MAY,o.JUN,o.JUL,o.AGO,o.SEP,o.OCT,o.NOV,o.DIC,o.[A_FEB" & Year(DTPhasta.Value) & "],(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) BETWEEN '1' and '2') [A_FEB" & (Year(DTPhasta.Value) - 1) & "],((o.[A_FEB" & Year(DTPhasta.Value) & "]) - (select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) BETWEEN '1' and '2')) AS [A_DIF],(o.FEB-o.ENE) [FEB" & Year(DTPhasta.Value) & "-ENE" & Year(DTPhasta.Value) & "],(o.FEB-(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '2')) [FEB" & Year(DTPhasta.Value) & "-FEB" & Year(DTPhasta.Value) - 1 & "] from ( select LOCAL,EESS,RUC,CLIENTE,AÑO,ISNULL([1], 0) ENE, ISNULL([2], 0) FEB, " & _
"ISNULL([3], 0) MAR,ISNULL([4], 0) ABR, ISNULL([5], 0) MAY, " & _
"ISNULL([6], 0) JUN,ISNULL([7], 0) JUL,ISNULL([8], 0) AGO, " & _
"ISNULL([9], 0) SEP, ISNULL([10], 0) OCT, " & _
"ISNULL([11], 0) NOV, ISNULL([12], 0) DIC, " & _
"(ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) + ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) + ISNULL([11], 0) + ISNULL([12], 0)) as [A_FEB" & Year(DTPhasta.Value) & "] " & _
"from( " & _
"select cv.cdlocal as LOCAL,ee.estacion as EESS,cv.ruccliente as RUC,cli.rscliente as CLIENTE,year(cv.fecproceso) as AÑO,month(cv.fecproceso) as Mes,cv.cantidad as Cantidad " & _
"from cuadredevtas cv inner join clientes cli on cv.ruccliente = cli.ruccliente inner join eess ee on cv.cdlocal = ee.cdlocal where cv.ruccliente like '%" & TxtRuc.Text.Trim & "%' and cv.rscliente like '%" & TXTclientes.Text.Trim & "%' and ee.estacion like '%" & CmbEstacion.Text & "%' and cv.fecproceso >= @desde and cv.fecproceso <= @hasta ) V " & _
"PIVOT(sum(Cantidad) FOR Mes IN ([1], [2], [3], [4], [5],[6],[7], [8], [9], [10], [11], [12]) ) as PT) as o order by O.EESS,o.FEB desc", Cn)
cmd.Parameters.AddWithValue("@desde", desde)
cmd.Parameters.AddWithValue("@hasta", hasta)y tengo este resultado:
la consulta lo que hace es mostrar los consumos en galones de los clientes de las sucursales en meses hasta alli todo bien pero ahora quiero mostrar los clientes que consumieron mas de 1000.00 galones en los ultimos 3 meses trate de poner sum(cv.cantidad) >= '1000' pero me sale error por favor su ayuda . gracias
le dejo el script
https://www.dropbox.com/s/auliosb8ald2cq3/dataayuda.sql?dl=0
Respuestas
-
Hola markho , puedes dejarle toda la chamba a SQL crea store procedure con parametros y estos solo envialos desde tu codigo y que sql los reciba, asi no mandas todo el query y aparte las variables
Nota: constr es el nombre de mi conexion para mi BD
Protected Sub "nombre"(sender As Object, e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("nombre de tu SP")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@varusuario", Login1.UserName) <---Parametro
cmd.Connection = con
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
reader.Read()
userId = Convert.ToInt32(reader("idusuario")) <-- En este ejemplo yo traigo un idusuario
con.Close()
End Using
End SubSQL:
CREATE PROCEDURE [nombredetusp]
@varusuario NVARCHAR(20), <- variable local de SQL que recibira el valor de tu codigo
@varcontraseña NVARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @varidusuario INT, @varrol INT <-variable local de SQL interno del SP
SELECT @varidusuario = idusuario, @varrol = idgrupo
FROM tbl_usuario WHERE usuario = @varusuario AND [contraseña] = @varcontraseña
IF @varidusuario IS NOT NULL
BEGIN
SELECT @varidusuario [idusuario],
(SELECT descripcion FROM tbl_grupo
WHERE idgrupo = @varrol) [grupodescripcion] -- User Valid
END
ELSE
BEGIN
SELECT -1 [UserId], '' [Roles] -- User invalid. <- en mi codigo VB tengo un selectpara jugar con el resultado
END
END
GOPodrias probar cambiando la carga de trabajo directamente en SQL con store procedures y desde tu codigo solo mandar las variables
- Propuesto como respuesta Pablo RubioModerator lunes, 1 de octubre de 2018 16:13
- Marcado como respuesta Pablo RubioModerator viernes, 5 de octubre de 2018 15:01
Todas las respuestas
-
buenas noches el filtro mayor a mil galones lo hice asi :
Dim cmd As New SqlCommand("select o.LOCAL,o.EESS,o.RUC,o.CLIENTE,(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '1') [ENE" & (Year(DTPhasta.Value) - 1) & "],(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '12') [DIC" & (Year(DTPhasta.Value) - 1) & "],o.AÑO,o.ENE,o.FEB,o.MAR,o.ABR,o.MAY,o.JUN,o.JUL,o.AGO,o.SEP,o.OCT,o.NOV,o.DIC,o.[A_ENE" & Year(DTPhasta.Value) & "],(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '1') [A_ENE" & (Year(DTPhasta.Value) - 1) & "],((o.[A_ENE" & Year(DTPhasta.Value) & "]) - (select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '1')) [A_DIF],(o.ENE-(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '12')) [ENE" & Year(DTPhasta.Value) & "-DIC" & (Year(DTPhasta.Value) - 1) & "],(o.ENE-(select isnull(sum(cv2.cantidad),0) from cuadredevtas as cv2 where cv2.ruccliente = o.RUC and cv2.cdlocal = o.LOCAL and year(cv2.fecproceso) = " & (Year(DTPhasta.Value) - 1) & " and month(cv2.fecproceso) = '1')) [ENE" & Year(DTPhasta.Value) & "-ENE" & (Year(DTPhasta.Value) - 1) & "] from ( select LOCAL,EESS,RUC,CLIENTE,AÑO,ISNULL([1], 0) ENE, ISNULL([2], 0) FEB, " & _ "ISNULL([3], 0) MAR,ISNULL([4], 0) ABR, ISNULL([5], 0) MAY, " & _ "ISNULL([6], 0) JUN,ISNULL([7], 0) JUL,ISNULL([8], 0) AGO, " & _ "ISNULL([9], 0) SEP, ISNULL([10], 0) OCT, " & _ "ISNULL([11], 0) NOV, ISNULL([12], 0) DIC, " & _ "(ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) + ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) + ISNULL([11], 0) + ISNULL([12], 0)) as [A_ENE" & Year(DTPhasta.Value) & "] " & _ "from( " & _ "select cv.cdlocal as LOCAL,ee.estacion as EESS,cv.ruccliente as RUC,cli.rscliente as CLIENTE,year(cv.fecproceso) as AÑO,month(cv.fecproceso) as Mes,sum(cv.cantidad) as Cantidad " & _ "from cuadredevtas cv inner join clientes cli on cv.ruccliente = cli.ruccliente inner join eess ee on cv.cdlocal = ee.cdlocal where cv.ruccliente like '%" & TxtRuc.Text.Trim & "%' and cv.rscliente like '%" & TXTclientes.Text.Trim & "%' and ee.estacion like '%" & CmbEstacion.Text & "%' and cv.fecproceso >= @desde and cv.fecproceso <= @hasta group by cv.cdlocal,ee.estacion,cv.ruccliente,cli.rscliente,year(cv.fecproceso),month(cv.fecproceso) having sum(cv.cantidad) >= " & CDbl(CmbMayores.Text) & " ) V " & _ "PIVOT(sum(Cantidad) FOR Mes IN ([1], [2], [3], [4], [5],[6],[7], [8], [9], [10], [11], [12]) ) as PT) as o order by o.EESS,o.ENE desc", Cn)
cuando elijo 10,20,50,100,150 la consulta es rapida hasta 500 galones pero cuando coloco 1000 en el filtro se pone muy lento y me sale un msm de error hay manera de hacerlo mas rapido ????
- Propuesto como respuesta Pablo RubioModerator martes, 2 de octubre de 2018 15:42
-
Hola markho , puedes dejarle toda la chamba a SQL crea store procedure con parametros y estos solo envialos desde tu codigo y que sql los reciba, asi no mandas todo el query y aparte las variables
Nota: constr es el nombre de mi conexion para mi BD
Protected Sub "nombre"(sender As Object, e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("nombre de tu SP")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@varusuario", Login1.UserName) <---Parametro
cmd.Connection = con
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
reader.Read()
userId = Convert.ToInt32(reader("idusuario")) <-- En este ejemplo yo traigo un idusuario
con.Close()
End Using
End SubSQL:
CREATE PROCEDURE [nombredetusp]
@varusuario NVARCHAR(20), <- variable local de SQL que recibira el valor de tu codigo
@varcontraseña NVARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @varidusuario INT, @varrol INT <-variable local de SQL interno del SP
SELECT @varidusuario = idusuario, @varrol = idgrupo
FROM tbl_usuario WHERE usuario = @varusuario AND [contraseña] = @varcontraseña
IF @varidusuario IS NOT NULL
BEGIN
SELECT @varidusuario [idusuario],
(SELECT descripcion FROM tbl_grupo
WHERE idgrupo = @varrol) [grupodescripcion] -- User Valid
END
ELSE
BEGIN
SELECT -1 [UserId], '' [Roles] -- User invalid. <- en mi codigo VB tengo un selectpara jugar con el resultado
END
END
GOPodrias probar cambiando la carga de trabajo directamente en SQL con store procedures y desde tu codigo solo mandar las variables
- Propuesto como respuesta Pablo RubioModerator lunes, 1 de octubre de 2018 16:13
- Marcado como respuesta Pablo RubioModerator viernes, 5 de octubre de 2018 15:01