MS Access - combine non-null column values into a (column name: column value) text string / field

丶灬走出姿态 提交于 2019-12-25 12:33:23

问题


I have an Excel file that's basically a query result to a database server.

I linked one sheet of this Excel file as an external table from within a MS Access database.

This external "Excel table" has a set of columns representing optional parameters.

I want to create one "all_parameters" query result field that represents a semicolon-separated list of all non-null parameters as "parameter name: parameter value" tuples. In this case, "parameter name" is also the name of the column in which the parameter value is to be found.

Imagine the following table:

id param_foo param_bar param_baz
 1 a
 2           b         c
 3

I hope to create a MS Access query that generates the following field:

id parameter_list
 1 param_foo: a
 2 param_bar: b; param_baz: c
 3 (null)

What does NOT work, is this:

SELECT
  id,
  Iif([param_foo]="Null";"";"; param_foo: "&[param_foo])
& Iif([param_bar]="Null";"";"; param_bar: "&[param_bar])
& Iif([param_baz]="Null";"";"; param_baz: "&[param_baz]) AS all_parameters
FROM
  my_table
;

Any help is more than welcome.


回答1:


Silly me.

MS Access requires commas as parameter separator, not semicolons.

Hence the following query works:

SELECT
  id,
  Iif([param_foo]="Null", "", "; param_foo: "&[param_foo])
& Iif([param_bar]="Null", "", "; param_bar: "&[param_bar])
& Iif([param_baz]="Null", "", "; param_baz: "&[param_baz]) AS all_parameters
FROM
  my_table
;

Problem solved.



来源:https://stackoverflow.com/questions/32589086/ms-access-combine-non-null-column-values-into-a-column-name-column-value-te

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!