SQL 将多个记录中的值合并到一个列中的查询
SQL Query to combine values from multiple records into a single column
在我的 table 中,每条记录最多可以有 30 个不同的预算代码。
我需要一个查询 returns table 中的所有预算代码作为单个列。
查询我有错误指出子查询返回超过 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
您的查询将 return 30 列,如果 TABLE_NAME
有多个记录,将会出错。相反,您想在此处使用 UNION 查询:
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
在我的 table 中,每条记录最多可以有 30 个不同的预算代码。 我需要一个查询 returns table 中的所有预算代码作为单个列。 查询我有错误指出子查询返回超过 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
您的查询将 return 30 列,如果 TABLE_NAME
有多个记录,将会出错。相反,您想在此处使用 UNION 查询:
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