我正在尝试从使用Microsoft SQL Server 2008的可用性软件获取数据。
我正在努力获得总金额并从一张发票中获得一个名为Numero de Obra(工作数量)的字段,问题是需要总结一些项目以获得总数,当我得到我的数据重复的额外字段数。
这是查询:
SELECT
GVA12.FECHA_EMIS AS [Fecha de emisión] ,
GVA53.T_Comp AS [Tipo comprobante] ,
GVA53.N_Comp AS [Nro. comprobante] ,
GVA12.COD_VENDED AS [Cód. vendedor] ,
CASE GVA12.COD_VENDED WHEN '**' THEN 'CONTADO' ELSE GVA23.NOMBRE_VEN END AS [Nombre Vendedor] ,
GVA12.COD_CLIENT AS [Cód. cliente] ,
CASE GVA12.COD_CLIENT WHEN '000000' THEN 'OCASIONAL' ELSE GVA14.RAZON_SOCI END AS [Razón social] ,
GVA12.COTIZ AS [Cotización] ,
GVA45.[DESC] as [Num_Obra]],
SUM(CASE WHEN GVA12.T_Comp <> 'FAC' and GVA15.Tipo_Comp = 'C' then (-1) ELSE (1) END * GVA53.CANTIDAD) AS [Cantidad] ,
SUM( CASE WHEN GVA12.T_Comp <> 'FAC' and GVA15.Tipo_Comp = 'C' then (-1) ELSE (1) END * CASE GVA12.Cotiz WHEN 0 THEN 0 ELSE CASE 'BIMONCTE' WHEN 'BIMONCTE' THEN (CASE GVA12.MON_CTE WHEN 1 THEN GVA53.IMP_NETO_P ELSE GVA53. IMP_NETO_P * GVA12.COTIZ END) WHEN 'BIORIGEN' THEN (CASE GVA12.MON_CTE WHEN 1 THEN GVA53.IMP_NETO_P / GVA12.COTIZ ELSE GVA53.IMP_NETO_P END) WHEN 'BICOTIZ' THEN(CASE GVA12.MON_CTE WHEN 1 THEN GVA53.IMP_NETO_P / 1 ELSE GVA53.IMP_NETO_P * GVA12.COTIZ / 1 END) END END ) AS [Total]
FROM
GVA12 (NOLOCK)
INNER JOIN
GVA53 (NOLOCK) ON GVA53.T_COMP = GVA12.T_COMP AND GVA53.N_COMP = GVA12.N_COMP
INNER JOIN
GVA23 (NOLOCK) ON GVA12.COD_VENDED = GVA23.COD_VENDED
LEFT JOIN
GVA14 (NOLOCK) ON GVA12.COD_CLIENT = GVA14.COD_CLIENT
LEFT JOIN
GVA15 ON GVA15.IDENT_COMP = GVA12.T_COMP
LEFT JOIN
GVA45 (NOLOCK) ON GVA53.COD_ARTICU = 'NºOBRA' AND
GVA45.N_COMP = GVA53.N_COMP AND GVA45.N_RENGLON = GVA53.N_RENGL_V
WHERE
(GVA53.COD_ARTICU <> 'Art. Ajuste') AND (GVA53.COD_ARTICU <> '')
AND (GVA53.RENGL_PADR = 0 OR GVA53.INSUMO_KIT_SEPARADO =1)
GROUP BY
GVA12.FECHA_EMIS , GVA53.T_Comp , GVA53.N_Comp , GVA12.COD_VENDED , CASE GVA12.COD_VENDED WHEN '**' THEN 'CONTADO' ELSE GVA23.NOMBRE_VEN END , GVA12.COD_CLIENT , CASE GVA12.COD_CLIENT WHEN '000000' THEN 'OCASIONAL' ELSE GVA14.RAZON_SOCI END , GVA12.COTIZ,GVA45.[DESC]
我目前的输出:
number_invoce client Number_Of_Work Total
FAC A000500000354 Hospital NULL 90
FAC A000500000354 Hospital 346300 0.000000
我的预期输出:
number_invoce client Number_Of_Work Total
A000500000354 Hospital 346300 90
我有很多不同的表格,其中大部分与问题无关,所以我只会解释那些重要的表格:
GVA53:它包含发票中的所有行
例如
number_invoce COD_ARTICU AMOUNT N_RENGL_V
A000500000354 Wall $50 1
A000500000354 Roof $40 2
A000500000354 number_of_work $0 3
GVA45:它有一篇文章的描述
number_invoce DESC N_RENGL
A000500000354 1
A000500000354 2
A000500000354 346300 3
简历中:我需要从发票中获取总额,我需要对项目进行求和,并且我需要从项目编号中获取DESC(描述)字段,关联来自GV45的n_rengl(行数)到GV53的n_rengl_v,其中COD_ARTIC等于number_of_work。一切正常,但我不知道为什么我的数据会被复制。
非常感谢
答案 0 :(得分:1)
好的最后一次尝试:P
正如评论中所建议的,我不确定noLock的价值在这里,但由于它们没有引起您想要解决的问题,我现在就把它们留在了。但是,我并不宽恕其使用。
问题是GVA45上的分组。[DESC]由于左连接和实际值而导致两个值为null。由于您只需要值,因此您需要让系统返回最大值并将其从组中删除。
这可能会这样做......
SELECT
GVA12.FECHA_EMIS AS [Fecha de emisión] ,
GVA53.T_Comp AS [Tipo comprobante] ,
GVA53.N_Comp AS [Nro. comprobante] ,
GVA12.COD_VENDED AS [Cód. vendedor] ,
CASE GVA12.COD_VENDED WHEN '**' THEN 'CONTADO' ELSE GVA23.NOMBRE_VEN END AS [Nombre Vendedor] ,
GVA12.COD_CLIENT AS [Cód. cliente] ,
CASE GVA12.COD_CLIENT WHEN '000000' THEN 'OCASIONAL' ELSE GVA14.RAZON_SOCI END AS [Razón social] ,
GVA12.COTIZ AS [Cotización] ,
max(GVA45.[DESC]) as [Num_Obra]],
SUM(CASE WHEN GVA12.T_Comp <> 'FAC' and GVA15.Tipo_Comp = 'C' then (-1) ELSE (1) END * GVA53.CANTIDAD) AS [Cantidad] ,
SUM( CASE WHEN GVA12.T_Comp <> 'FAC' and GVA15.Tipo_Comp = 'C' then (-1) ELSE (1) END * CASE GVA12.Cotiz WHEN 0 THEN 0 ELSE CASE 'BIMONCTE' WHEN 'BIMONCTE' THEN (CASE GVA12.MON_CTE WHEN 1 THEN GVA53.IMP_NETO_P ELSE GVA53. IMP_NETO_P * GVA12.COTIZ END) WHEN 'BIORIGEN' THEN (CASE GVA12.MON_CTE WHEN 1 THEN GVA53.IMP_NETO_P / GVA12.COTIZ ELSE GVA53.IMP_NETO_P END) WHEN 'BICOTIZ' THEN(CASE GVA12.MON_CTE WHEN 1 THEN GVA53.IMP_NETO_P / 1 ELSE GVA53.IMP_NETO_P * GVA12.COTIZ / 1 END) END END ) AS [Total]
FROM
GVA12 (NOLOCK)
INNER JOIN
GVA53 (NOLOCK) ON GVA53.T_COMP = GVA12.T_COMP AND GVA53.N_COMP = GVA12.N_COMP
INNER JOIN
GVA23 (NOLOCK) ON GVA12.COD_VENDED = GVA23.COD_VENDED
LEFT JOIN
GVA14 (NOLOCK) ON GVA12.COD_CLIENT = GVA14.COD_CLIENT
LEFT JOIN
GVA15 ON GVA15.IDENT_COMP = GVA12.T_COMP
LEFT JOIN
GVA45 (NOLOCK) ON GVA53.COD_ARTICU = 'NºOBRA' AND
GVA45.N_COMP = GVA53.N_COMP AND GVA45.N_RENGLON = GVA53.N_RENGL_V
WHERE
(GVA53.COD_ARTICU <> 'Art. Ajuste') AND (GVA53.COD_ARTICU <> '')
AND (GVA53.RENGL_PADR = 0 OR GVA53.INSUMO_KIT_SEPARADO =1)
GROUP BY
GVA12.FECHA_EMIS , GVA53.T_Comp , GVA53.N_Comp , GVA12.COD_VENDED , CASE GVA12.COD_VENDED WHEN '**' THEN 'CONTADO' ELSE GVA23.NOMBRE_VEN END , GVA12.COD_CLIENT , CASE GVA12.COD_CLIENT WHEN '000000' THEN 'OCASIONAL' ELSE GVA14.RAZON_SOCI END , GVA12.COTIZ