Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

ms access - SQL, combining the result

I am using Access and have this SQL

SELECT land.id, land.official_name, vaksiner.vaksiner
FROM land INNER JOIN (vaksiner INNER JOIN land_sykdom ON vaksiner.id = land_sykdom.sykdom)        ON land.kort = land_sykdom.land
ORDER BY land.official_name

The SQL gives me a result like this:

id    official_name    vaksiner

1     a                A
1     a                C    
2     b                A
2     b                B
2     b                C

But I want to combine the result so that it looks like this:

id    official_name    vaksiner

1     a                A, C
2     b                A, B, C
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See Allen Browne's ConcatRelated function (Concatenate values from related records)

If you save your existing query as qryVaksinerRaw, you can build a new query like this to produce the results you're after.

SELECT DISTINCT
    id,
    official_name,
    ConcatRelated("vaksiner","qryVaksinerRaw","id = " & [id]) AS vaksiner
FROM qryVaksinerRaw;

To add the function to a module, copy the function from the web page starting with

Public Function ConcatRelated(strField As String, _

and continuing to include

End Function

Then paste the copied text into your module.

Note ConcatRelated() will then be available for queries run from inside Access. However, like any custom VBA function, it can not be used in queries run from outside Access (like classic ASP, .Net, etc.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...