使用concatrelated vba excel

时间:2012-10-12 19:24:01

标签: sql excel vba

我在使用sql语句上的concatrelated函数时遇到问题。我想获得如下:

我的数据是:

A.CABNRO E01.EXCCEX
90001 500
87000 400
90001 450
90001 300
87000 500
76005

我想获得:

A.CABNRO NEW FIELD
90001 500,450,300
87000 400,500
76005

当我包含与concatrelated相关的语句时,以下代码不会运行。如果有人可以帮助我,我将不胜感激!

MYSQL = "SELECT A.CABFPR, A.CABCIA, A.CABCTR, B.CTRDTR, A.CABLOE, "
MYSQL = MYSQL & "A.CABNRO, A.CABCAR, A.CABFED, A.CABHOD, A.CABFEI, "
MYSQL = MYSQL & "A.CABHOI, A.CABFEF, A.CABHOF, A.CABFEE, A.CABHOE, "
MYSQL = MYSQL & "A.CABCCL, A.CABNEM, A.CABDEM "
MYSQL = MYSQL & "CONCATRELATED(EXCCEX, E01, A.CABNRO=E01.EXCNRO) "
MYSQL = MYSQL & "FROM BDYOBEL.AIPDTA.AIPCAB A "
MYSQL = MYSQL & "LEFT OUTER JOIN BDYOBEL.AIPDTA.AIPCTR B ON A.CABCIA=B.CTRCIA AND A.CABCTR=B.CTRCTR "
MYSQL = MYSQL & "LEFT OUTER JOIN BDYOBEL.AIPDTA.AIPEXC E01 ON A.CABNRO=E01.EXCNRO "
MYSQL = MYSQL & "WHERE A.CABCIA='LOR' AND A.CABFED>="
MYSQL = MYSQL & Chr$(39) & fini & Chr$(39) & "AND A.CABFED<=" & Chr$(39) & ffin & Chr$(39)
MYSQL = MYSQL & " AND (A.CABCTR='DM' OR A.CABCTR='L1' OR A.CABCTR='L5' OR A.CABCTR='LR') AND A.CABCAR<>'0'"

1 个答案:

答案 0 :(得分:0)

声明http://allenbrowne.com/func-concat.html

  

如果ConcatRelated函数返回超过255个字符,和   你在查询中使用它作为另一个记录集的来源,一个错误   访问可能会返回剩余字符的垃圾。

这可以解释为什么您的代码无效。
在Access中无法实现聚合非固定数量列的数据。你能做的最好的事就是写下这样的东西,并将列与文本编辑器结合起来。

TRANSFORM Sum(E01.EXCCEX) AS SumOfEXCCEX
SELECT A.CABNRO
FROM [BDYOBEL-AIPDTA-AIPCAB] AS A INNER JOIN [BDYOBEL-AIPDTA-AIPEXC] AS E01 ON 
  A.CABNRO = E01.EXCNRO
GROUP BY A.CABNRO
ORDER BY A.CABNRO DESC , E01.EXCCEX DESC 
PIVOT E01.EXCCEX;  

运行查询会为您的示例数据提供以下结果: screenshot of access output