Transpose query creates nodes (SQL Server 2008)

烈酒焚心 提交于 2019-12-25 02:12:48

问题


I finally found the query to execute to get all ids (comma separated) for one content in one row.

Following query did the trick:

You don't need to look at the query because it already does what it should do.

SELECT 
    taxonomy_item_id, 
    SUBSTRING(
      (SELECT ', ' + CAST(taxonomy_id AS varchar) AS Expr1
       FROM taxonomy_item_tbl AS t2
       WHERE (t1.taxonomy_item_id = taxonomy_item_id) AND (taxonomy_language_id = 2067)
       ORDER BY taxonomy_item_id, taxonomy_id FOR XML PATH('')
      ), 1, 1000) AS taxonomy_ids
FROM 
    taxonomy_item_tbl AS t1
WHERE 
    (taxonomy_language_id = 2067) AND (taxonomy_item_id = 180555)
GROUP BY 
    taxonomy_item_id

The only problem is the data result I get:

180555  |   <Expr1>, 404</Expr1><Expr1>, 405</Expr1><Expr1>, 723</Expr1><Expr1>, 1086</Expr1><Expr1>, 1087</Expr1><Expr1>, 1118</Expr1><Expr1>, 1124</Expr1><Expr1>, 1126</Expr1>

I don't need the <Expr1> nodes. Is there a way to delete this? If I delete AS Expr1in the query then it is automatically added back

Thanks


回答1:


If you don't want the <Expr1> - then just don't ask for it!

You have:

(SELECT ', ' + CAST(taxonomy_id AS varchar) AS Expr1

That AS Expr1 causes the <Expr1> to be added - so just don't have that expression there.

Try

SELECT 
    taxonomy_item_id, 
    SUBSTRING(
      (SELECT ', ' + CAST(taxonomy_id AS VARCHAR) 
       FROM dbo.taxonomy_item_tbl AS t2
       WHERE t1.taxonomy_item_id = taxonomy_item_id
       AND taxonomy_language_id = 2067
       ORDER BY taxonomy_item_id, taxonomy_id 
       FOR XML PATH('')
      ), 1, 1000) AS taxonomy_ids
FROM 
    dbo.taxonomy_item_tbl AS t1
WHERE 
    taxonomy_language_id = 2067
    AND taxonomy_item_id = 180555
GROUP BY 
    taxonomy_item_id


来源:https://stackoverflow.com/questions/8477500/transpose-query-creates-nodes-sql-server-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!