Search code examples
sqlms-accessms-access-2016

SQL Query to combine values from multiple records into a single column


In my table each record can have up to 30 different budget Codes. I need a query that returns all budget codes in the table as a single column. The query I have errors stating that the subquery returned more than 1 value.

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

Solution

  • Your query is going to return 30 columns and will error if TABLE_NAME has more than one record. Instead, you want to use a UNION query here:

        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
    

    You can also add in which budgetcode column this was derived from:

        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