Error using STUFF in SQL Server

0

I have a query and I want to group data from one column in a row, separated by a comma, through examples I could put together the following query, but when I run it I get The multi-part identifier "arr_inventario_sal.ains_folio" could not be bound

SELECT  arr_inventario_sal.ains_folio,
          STUFF((SELECT ', ' + arr_inventario_sal.ains_num_serie AS Expr1
          FROM arr_inventario_sal INNER JOIN
          mov_inventario ON arr_inventario_sal.ains_folio = mov_inventario.inv_folio
  WHERE        (mov_inventario.inv_tip_doc_ref = 'FAC') AND (mov_inventario.inv_pto_vta = 'VIC') AND (mov_inventario.inv_fol_doc_ref = 134990) FOR XML PATH('')), 1, 2, '') AS SERIE

By removing arr_inventario_sal.ains_folio, it throws me the data, but I need to visualize more columns.

SELECT        STUFF
((SELECT ', ' + arr_inventario_sal.ains_num_serie AS Expr1
 FROM arr_inventario_sal INNER JOIN
 mov_inventario ON arr_inventario_sal.ains_folio = mov_inventario.inv_folio
 WHERE        (mov_inventario.inv_tip_doc_ref = 'FAC') AND (mov_inventario.inv_pto_vta = 'VIC') AND (mov_inventario.inv_fol_doc_ref = 134990) FOR XML PATH('')), 1, 2, '') AS SERIE

Any suggestions to make my query?

    
asked by Silvestre Silva 25.04.2018 в 01:23
source

1 answer

0

It's happening because you try to choose a column that is not in your subquery (you have to do the stuff)

Since I am not so steeped in the final result of your query, I leave you what I understand should be

SELECT  arr_inventario_sal.ains_folio, 
        STUFF(( SELECT ', ' + arr_inventario_sal.ains_num_serie AS Expr1
                FROM arr_inventario_sal 
                INNER JOIN mov_inventario 
                   ON arr_inventario_sal.ains_folio = mov_inventario.inv_folio 
                WHERE (mov_inventario.inv_tip_doc_ref = 'FAC') 
                  AND (mov_inventario.inv_pto_vta = 'VIC') 
                  AND (mov_inventario.inv_fol_doc_ref = 134990) 
                FOR XML PATH('')), 1, 2, '') AS SERIE
FROM arr_inventario_sal 
INNER JOIN mov_inventario 
    ON arr_inventario_sal.ains_folio = mov_inventario.inv_folio 
WHERE (mov_inventario.inv_tip_doc_ref = 'FAC') 
    AND (mov_inventario.inv_pto_vta = 'VIC') 
    AND (mov_inventario.inv_fol_doc_ref = 134990)

    
answered by 25.04.2018 / 15:47
source