在我的表格中,每条记录最多可以包含30个不同的预算代码。 我需要一个查询来将表中的所有预算代码作为单列返回。 查询中出现错误,指出该子查询返回了多个值。
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
答案 0 :(得分:2)
您的查询将返回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
您还可以在以下预算代码列中添加其来源:
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