簡體   English   中英

SQL查詢將多個記錄中的值組合到單個列中

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

在我的表格中,每條記錄最多可以包含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

您的查詢將返回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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM