Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询将多个记录中的值合并到一个列中_Sql_Ms Access_Ms Access 2016 - Fatal编程技术网

SQL查询将多个记录中的值合并到一个列中

SQL查询将多个记录中的值合并到一个列中,sql,ms-access,ms-access-2016,Sql,Ms Access,Ms Access 2016,在我的表格中,每条记录最多可以有30个不同的预算代码。 我需要一个查询,将表中的所有预算代码作为一列返回。 查询I有错误,指出子查询返回的值超过1 SELECT (SELECT [budgetCode1] FROM TABLE_NAME), (SELECT [budgetCode2] FROM TABLE_NAME), (SELECT [budgetCode3] FROM TABLE_NAME), (SELECT [budgetC

在我的表格中,每条记录最多可以有30个不同的预算代码。 我需要一个查询,将表中的所有预算代码作为一列返回。 查询I有错误,指出子查询返回的值超过1

SELECT 
        (SELECT [budgetCode1] FROM TABLE_NAME),
        (SELECT [budgetCode2] FROM TABLE_NAME),
        (SELECT [budgetCode3] FROM TABLE_NAME),
        (SELECT [budgetCode4] FROM TABLE_NAME),
        (SELECT [budgetCode5] FROM TABLE_NAME),
        (SELECT [budgetCode6] FROM TABLE_NAME),
        (SELECT [budgetCode7] FROM TABLE_NAME),
        (SELECT [budgetCode8] FROM TABLE_NAME),
        (SELECT [budgetCode9] FROM TABLE_NAME),
        (SELECT [budgetCode10] FROM TABLE_NAME),
        (SELECT [budgetCode11] FROM TABLE_NAME),
        (SELECT [budgetCode12] FROM TABLE_NAME),
        (SELECT [budgetCode13] FROM TABLE_NAME),
        (SELECT [budgetCode14] FROM TABLE_NAME),
        (SELECT [budgetCode15] FROM TABLE_NAME),
        (SELECT [budgetCode16] FROM TABLE_NAME),
        (SELECT [budgetCode17] FROM TABLE_NAME),
        (SELECT [budgetCode18] FROM TABLE_NAME),
        (SELECT [budgetCode19] FROM TABLE_NAME),
        (SELECT [budgetCode20] FROM TABLE_NAME),
        (SELECT [budgetCode21] FROM TABLE_NAME),
        (SELECT [budgetCode22] FROM TABLE_NAME),
        (SELECT [budgetCode23] FROM TABLE_NAME),
        (SELECT [budgetCode24] FROM TABLE_NAME),
        (SELECT [budgetCode25] FROM TABLE_NAME),
        (SELECT [budgetCode26] FROM TABLE_NAME),
        (SELECT [budgetCode27] FROM TABLE_NAME),
        (SELECT [budgetCode28] FROM TABLE_NAME),
        (SELECT [budgetCode29] FROM TABLE_NAME),
        (SELECT [budgetCode30] FROM TABLE_NAME) AS BudgetCodes
  FROM TABLE_NAME

您的查询将返回30列,如果
TABLE\u NAME
有多条记录,则会出错。相反,您希望在此处使用联合查询:

    SELECT [budgetCode1] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode2] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode3] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode4] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode5] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode6] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode7] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode8] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode9] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode10] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode11] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode12] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode13] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode14] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode15] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode16] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode17] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode18] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode19] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode20] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode21] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode22] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode23] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode24] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode25] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode26] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode27] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode28] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode29] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode30] FROM TABLE_NAME
您还可以添加从中派生的budgetcode列:

    SELECT [budgetCode1],"budgetCode1" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode2],"budgetCode2" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode3],"budgetCode3" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode4],"budgetCode4" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode5],"budgetCode5" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode6],"budgetCode6" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode7],"budgetCode7" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode8],"budgetCode8" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode9],"budgetCode9" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode10],"budgetCode10" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode11],"budgetCode11" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode12],"budgetCode12" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode13],"budgetCode13" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode14],"budgetCode14" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode15],"budgetCode15" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode16],"budgetCode16" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode17],"budgetCode17" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode18],"budgetCode18" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode19],"budgetCode19" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode20],"budgetCode20" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode21],"budgetCode21" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode22],"budgetCode22" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode23],"budgetCode23" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode24],"budgetCode24" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode25],"budgetCode25" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode26],"budgetCode26" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode27],"budgetCode27" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode28],"budgetCode28" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode29],"budgetCode29" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode30],"budgetCode30" as budgetcode FROM TABLE_NAME AS BudgetCodes
FROM TABLE_NAME

您的查询将返回30列,如果
TABLE\u NAME
有多条记录,则会出错。相反,您希望在此处使用联合查询:

    SELECT [budgetCode1] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode2] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode3] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode4] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode5] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode6] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode7] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode8] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode9] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode10] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode11] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode12] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode13] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode14] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode15] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode16] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode17] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode18] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode19] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode20] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode21] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode22] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode23] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode24] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode25] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode26] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode27] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode28] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode29] FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode30] FROM TABLE_NAME
您还可以添加从中派生的budgetcode列:

    SELECT [budgetCode1],"budgetCode1" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode2],"budgetCode2" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode3],"budgetCode3" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode4],"budgetCode4" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode5],"budgetCode5" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode6],"budgetCode6" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode7],"budgetCode7" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode8],"budgetCode8" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode9],"budgetCode9" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode10],"budgetCode10" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode11],"budgetCode11" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode12],"budgetCode12" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode13],"budgetCode13" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode14],"budgetCode14" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode15],"budgetCode15" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode16],"budgetCode16" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode17],"budgetCode17" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode18],"budgetCode18" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode19],"budgetCode19" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode20],"budgetCode20" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode21],"budgetCode21" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode22],"budgetCode22" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode23],"budgetCode23" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode24],"budgetCode24" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode25],"budgetCode25" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode26],"budgetCode26" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode27],"budgetCode27" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode28],"budgetCode28" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode29],"budgetCode29" as budgetcode FROM TABLE_NAME
    UNION ALL
    SELECT [budgetCode30],"budgetCode30" as budgetcode FROM TABLE_NAME AS BudgetCodes
FROM TABLE_NAME

我们SQL人员需要强调这样的次优数据库设计,这会导致冗长、复杂的查询@冻糕完全同意。为每个预算代码设置列是一个糟糕的选择,这将导致一些讨厌的(而且很慢的)联合查询,以将其转换为正确的格式(就像我们在这里所做的那样)。糟糕。可以修改此查询以排除NULL或空字符串值吗?您必须将其包装在子查询中,并添加WHERE子句
,其中[budgetcode1]为NULL
或。。将WHERE子句添加到每个SELECT语句中。我们SQL人员需要强调这样的次优数据库设计,这会导致冗长、复杂的查询@冻糕完全同意。为每个预算代码设置列是一个糟糕的选择,这将导致一些讨厌的(而且很慢的)联合查询,以将其转换为正确的格式(就像我们在这里所做的那样)。糟糕。可以修改此查询以排除NULL或空字符串值吗?您必须将其包装在子查询中,并添加WHERE子句
,其中[budgetcode1]为NULL
或。。将WHERE子句添加到每个SELECT语句中。重新考虑您的宽数据库表,因为这是低效的存储,无法正确扩展。创建一个带有外键链接到TabLeNAME的预算代码表,并保持一个列指示符为1-30和一个列指示符,用于对应的数字或字符串值。如@ RolandMySQLDBA:没有替代或带帮助可以弥补糟糕的设计。请,为了你将来的理智起见,今天就让那张桌子正常化!!!我非常希望我的桌子正常化。不幸的是,此表是通过另一个系统的数据转储创建的。我对表结构没有发言权。请重新考虑您的宽数据库表,因为这是低效的存储,无法正确扩展。创建一个带有外键链接到TabLeNAME的预算代码表,并保持一个列指示符为1-30和一个列指示符,用于对应的数字或字符串值。如@ RolandMySQLDBA:没有替代或带帮助可以弥补糟糕的设计。请,为了你将来的理智起见,今天就让那张桌子正常化!!!我非常希望我的桌子正常化。不幸的是,此表是通过另一个系统的数据转储创建的。我对表格结构没有发言权。