導出1時,我當前加入了2個其他表格,但這導致行被複制。而不是複製行以匹配值,是否可以用逗號分隔特定行的值?加入表格而不復制該行
這裏是我的表的樣本,因爲它現在是:
id,optioncatid,optionsdesc_sidenote,isproductcode,applytoproductcodes,stockstatus
"325","30","","BRB8PACK","00LDCLU131401C","17"
"325","30","","BRB8PACK","00LDDEV131401C","17"
"325","30","","BRB8PACK","00LDHEI131401C","17"
//etc
這是我想它是:
id,optioncatid,optionsdesc_sidenote,isproductcode,applytoproductcodes,stockstatus
"325","30","","BRB8PACK","00LDCLU131401C,00LDCLU131401C,00LDHEI131401C, etc...","17"
//etc
可以有成千上萬值的applytoproductcodes
,當以XML格式導出時,將文件擴展到200 MB以上。這顯然非常臃腫。 我的SQL查詢:
SELECT
Options.ID,
Options.OptionCatID,
Options.optionsdesc_sidenote,
Options.IsProductCode,
Options_ApplyTo.ProductCode AS ApplyToProductCodes,
Products.StockStatus AS StockStatus
FROM
Options
JOIN Options_ApplyTo ON Options.ID = Options_ApplyTo.OptionID
JOIN Products ON Options.IsProductCode = Products.ProductCode
WHERE
Options.IsProductCode <> ''
ORDER BY
Options.ID
編輯:現在我已經做了更多的研究和修改我的代碼如下:
SELECT
Options.ID,
Options.OptionCatID,
Options.optionsdesc_sidenote,
Options.IsProductCode,
t.ProductCode AS ApplyToProductCodes,
Products.StockStatus AS StockStatus
FROM
Options
LEFT JOIN
(
select OptA.ProductCode as ProductCode, OptA.OptionID as OptionID
from Options_ApplyTo AS OptA, Options
WHERE Options.ID = OptA.OptionID
order by OptA.OptionID
for xml path('')
) t
ON Options.ID = t.OptionID
LEFT JOIN Products
ON Options.IsProductCode = Products.ProductCode
WHERE
Options.IsProductCode <> ''
ORDER BY
Options.ID
但現在我得到的是沒有指定列錯誤't'的第1列。
所有的好和工作。但是現在我嘗試添加到'WHERE'部分'(StockStatus <='0'AND ApplyToProductCodes <>''),並且我得到了「無效的列名'ApplyToProductCodes'」。我如何參考? –
@Jack Cole爲了在where子句中引用'ApplyToProductCodes',你必須圍繞這個查詢包含另一個選擇。 where子句不知道它的關聯select子句,因爲它在select之前執行。 – dotjoe