0
SELECT 
    strCodArtigo,
    SUM(fltQuantidadePend) OVER (PARTITION BY strCodArtigo),
    Lin.CA_Campo01
FROM
    Mov_Encomenda_Cab cab
INNER JOIN 
    Mov_Encomenda_Lin Lin ON Lin.strCodSeccao = cab.strCodSeccao
                          AND Lin.strCodExercicio = cab.strCodExercicio
                          AND Lin.strAbrevTpDoc = cab.strAbrevTpDoc
                          AND Lin.intNumero = cab.intNumero
INNER JOIN 
    Tbl_Tipos_Documentos tpdoc ON tpdoc.strAbreviatura = cab.strAbrevTpDoc
WHERE 
    Lin.fltQuantidadePend <> 0
    AND cab.bitAnulado = 0
    AND tpdoc.bitQuotation = 0
    AND tpdoc.intTpEntidade = 0
ORDER BY 
    strCodArtigo;

I have two rows with the same values (in strCodArtigo and fltQuantidadePend) except for the lin.CA_Campo01. I need to concatenate the strings in lin.CA_Campo01 so it becomes just 1 row, but I can't use string_agg because I have the SQL Server 2016 paid version. Every alternative to string_agg didn't work, and I can't figure out how to solve this problem

The result I am getting:

1.654.831.12 | 13.000000 | 16.05_TESTE
1.654.831.12 | 13.000000 | 16.05_TESTE1

The result I want must be like this:

1.654.831.12 | 13.000000 | 16.05_TESTE/16.05_TESTE1

pls :)

a37240
  • 1
  • 2
  • 2
    Please provide sample input data and expected outcome. – Jonas Metzler May 18 '22 at 13:55
  • 2
    How is this different from your [previous question](https://stackoverflow.com/questions/72288331/concatenate-two-strings-from-2-rows-into-1-row-sql-2016) which was closed as a duplicate of [this one](https://stackoverflow.com/questions/194852/how-to-concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql-serv)? Did you try anything at all from the latter? You say `Every alternative to string_agg didn't work`, now show what you tried and tell us what "didn't work" means... – Aaron Bertrand May 18 '22 at 14:03
  • Yes I tried, but I still have the problem and can't solve it, nothing from the "similar questions" worked for me – a37240 May 18 '22 at 14:05
  • 1
    While asking a question, you need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example): (1) DDL and sample data population, i.e. CREATE table(s) plus INSERT T-SQL statements. (2) What you need to do, i.e. logic and your code attempt implementation of it in T-SQL. (3) Desired output, based on the sample data in the #1 above. (4) Your SQL Server version (SELECT @@version;). – Yitzhak Khabinsky May 18 '22 at 14:09
  • Still don't know _what you tried_ or what "can't solve it" and "didn't work" mean. What code did you try? Did you get an error message? What was it? – Aaron Bertrand May 18 '22 at 14:09
  • Mr Aaron Bertrand, I tried the function STUFF with the XML path, and it only repeats the same value over and over again, I tried with coalesce with 2 select but they aren't working properly. – a37240 May 18 '22 at 14:12
  • 1
    Does this answer your question? [String\_agg for SQL Server before 2017](https://stackoverflow.com/questions/49361088/string-agg-for-sql-server-before-2017) – Thom A May 18 '22 at 14:36
  • 2
    If you tried `FOR XML PATH`, then [edit] your question to demonstrate that attempt, @a37240 . Don't delete and repost your question. – Thom A May 18 '22 at 14:37
  • 2
    It would also be nice to know what actual alternatives are included in `every alternative to string_agg`. May I assume this includes quantum tunneling? Because if you haven't tried that, then you haven't tried `every alternative`. There may be other alternatives we know of that you haven't tried, and some may even use SQL, but we can't know which ones you've already tried unless you're specific. – chris May 18 '22 at 14:51

0 Answers0