根据空白列连接列

concatenate columns based on blank columns

我有三列,- 和 / 应该用作分隔符结果应该出现在新列中(文件索引子 Index/Year),如果子索引为空则结果应该是(文件 Index/Year).

  SELECT 
[ File Index].[File Index]
, [ File Index].[Sub Index]
, [ File Index].[Financial Year]
, [ File Index].[File Index] & [Sub Index] & [Financial Year] AS [Composite Index]
  FROM [File Index];

您可以使用 IIF 函数:IIF(Condition;ConcatenationIfTrue;ConcatenationIfFalse)

  SELECT 
[ File Index].[File Index]
, [ File Index].[Sub Index]
, [ File Index].[Financial Year]
, IIF(ISNULL([Sub Index];[ File Index].[File Index] & "/" & [Financial Year];[ File Index].[File Index] & [Sub Index] & [Financial Year]) AS [Composite Index]
  FROM [File Index];

由于您同时列出了 MySQLMS Access,这里是两者的解决方案。

对于 MySQL,您可以为此使用 CASE 语句:

SELECT [File Index], [Sub Index], [Financial Year],
       CASE WHEN [Sub Index] IS NOT NULL 
             THEN Concat([File Index], '-', [Sub Index], '/', [Financial Year]) 
             ELSE Concat([File Index], '/', [Financial Year]) 
       END as [Composite Index] 
FROM [File Index];

对于MS Access,您将使用Switch(...):

SELECT [File Index], [Sub Index], [Financial Year],
       Switch(Not IsNull([Sub Index]), 
                 [File Index] & '-' & [Sub Index] & '/' & [Financial Year],
              IsNull([Sub Index]), 
                 [File Index] & '/' & [Financial Year]
       ) as [Composite Index] 
FROM [File Index];