Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 删除子查询_Sql Server 2005_Join_Query Optimization_Subquery - Fatal编程技术网

Sql server 2005 删除子查询

Sql server 2005 删除子查询,sql-server-2005,join,query-optimization,subquery,Sql Server 2005,Join,Query Optimization,Subquery,我有这个疑问 select T1.local, m.Nombre as Marca, T1.NombreLinea as Linea , case when T1.IdComprobanteTipo in (21,23,26,28) then 0 else -sum(T1.Cantidad) end as Cantidad, case when T1.IdComprobanteTipo in (21,23,26,28) --toma e

我有这个疑问

select 
    T1.local,
    m.Nombre as Marca, 
    T1.NombreLinea as Linea , 
    case  when T1.IdComprobanteTipo in (21,23,26,28) then 0 else -sum(T1.Cantidad) end as Cantidad,
    case  when T1.IdComprobanteTipo in (21,23,26,28) 
    --toma en cuenta los descuentos de las facturas que fueron anuladas
    then T1.TotalNeto 
    else isnull(T1.ImporteNeto,0) - isnull((select sum(importe) as importe 
                                            from GrimPosCon..Fact_Descuento (nolock) DD
                                            where DD.IdComprobante = T1.idComprobante  and T1.IdDim_Producto = IdDim_Producto
                                             and T1.IdDim_Medida = IdDim_Medida and T1.IdDim_Calidad = IdDim_Calidad 
                                             and exists 
                                                (
                                                select 1 
                                                    from GrimPosCon..ComprobanteReferencias (nolock) AA
                                                    inner join GrimPosCon..Fact_Comprobante (nolock) BB on aa.IdComprobanteCredito = BB.IdComprobante
                                                    where AA.IdComprobanteDebito = DD.IdComprobante
                                                    and BB.IdDim_ComprobanteTipo in (4,13,16,19)
                                                )
                                            ),0) end as IMPORTE, 
    T1.NombreFamilia as Familia
from 
#Query T1
inner join GrimPosCon..Dim_Marca(nolock) m on m.IdDim_Marca = T1.IDDim_Marca
Group by  
    T1.local, m.Nombre,T1.NombreLinea,T1.NombreFamilia,T1.TotalNeto,T1.IdComprobante, T1.IdDim_producto,
    T1.idDim_Medida,T1.ImporteNeto,T1.IdComprobanteTipo,T1.IdDim_Calidad
我想删除最大的子查询。 我尝试在不为null的位置执行
左连接,但它给出了不同的结果。
我如何修改它?

我想要相同的结果,但是使用一些连接,或者比子查询更快的东西。根据执行计划,该子查询占总查询的35%,运行所有查询几乎需要50秒

您可以尝试以下查询:

select 
  T1.local,
  m.Nombre as Marca, 
  T1.NombreLinea as Linea , 
  case  when T1.IdComprobanteTipo in (21,23,26,28) then 0 else -sum(T1.Cantidad) end as Cantidad,
  case  when T1.IdComprobanteTipo in (21,23,26,28) 
  --toma en cuenta los descuentos de las facturas que fueron anuladas
    then T1.TotalNeto 
    else isnull(T1.ImporteNeto,0) - isnull(DD.importe,0)
  end as IMPORTE,
  T1.NombreFamilia as Familia
from 
  #Query T1
  inner join
  GrimPosCon..Dim_Marca(nolock) m on m.IdDim_Marca = T1.IDDim_Marca
  left join
  (
    select
      CC.IdComprobante,
      CC.IdDim_Producto,
      CC.IdDim_Medida,
      CC.IdDim_Calidad,      
      sum(importe) as importe 
    from
      GrimPosCon..Fact_Descuento (nolock) CC
    where
      exists 
        (
          select 1
          from
            GrimPosCon..ComprobanteReferencias (nolock) AA
            inner join
            GrimPosCon..Fact_Comprobante (nolock) BB on aa.IdComprobanteCredito = BB.IdComprobante
          where
            AA.IdComprobanteDebito = CC.IdComprobante
            and
            BB.IdDim_ComprobanteTipo in (4,13,16,19)
        )
    group by
      DD.IdComprobante,
      DD.IdDim_Producto,
      DD.IdDim_Medida,
      DD.IdDim_Calidad
  ) as DD on
      DD.IdComprobante = T1.idComprobante
      and
      DD.IdDim_Producto = T1.IdDim_Producto
      and
      DD.IdDim_Medida = T1.IdDim_Medida
      and
      DD.IdDim_Calidad = T1.IdDim_Calidad 
  Group by  
    T1.local, m.Nombre,T1.NombreLinea,T1.NombreFamilia,T1.TotalNeto,T1.IdComprobante, T1.IdDim_producto,
    T1.idDim_Medida,T1.ImporteNeto,T1.IdComprobanteTipo,T1.IdDim_Calidad,DD.importe

它可能运行得更快或更慢,具体取决于您的数据。

为什么要删除它?预期的结果是什么?我的意思是,我想要相同的结果,但不是得到子查询,而是想做一些联接,或者做一些fasterEXISTS比LEFT JOIN/NOT NULL逻辑更有效的事情。@ChrisGessler仅当它用于单表子查询或不相关时。