2

複数の行を返す結合を持つSQL Server 2008ストアドプロシージャがあります。

default ~/  /   NULL    NULL    NULL    Lorem
default ~/  /   NULL    NULL    NULL    Ipsum

これらの 2 つの行を、最後の列を組み合わせた 1 つとして返したいと思います。

default ~/  /   NULL    NULL    NULL    Lorem, Ipsum

私の手順は以下です

BEGIN   
    SET NOCOUNT ON;

    SELECT 
            p.display_name AS Name,
            p.url AS Url,
            ISNULL(REPLACE(parentPage.url,'~','') + parentPage.alias,REPLACE(p.url,'~',''))  as ParentPageURL,
            p.page_image AS PageImage,
            p.synopsis AS Synopsis,
            p.metatitle AS Metatitle,
            t.tag_name as tag

            FROM page p
            LEFT JOIN page parentPage on parentPage.id = p.parentid
            LEFT JOIN page_features pf on p.id = pf.pageid and (pf.feature_type = 'TEXT' OR pf.feature_type = 'BLOG')
            JOIN dbo.tag_collection_tag tc on tc.tag_collection_id = p.tag_collection_id
            JOIN dbo.tag t on t.id = tc.tag_id 
            JOIN dbo.[Split](UPPER(@tags),',') Split on UPPER(t.tag_name) like '%' + split.Data + '%'

    WHERE p.status = 'PUBLISHED'
    AND p.include_in_nav = 1
    AND (p.pagelevel = @level OR @level IS NULL)
    AND (p.section_id = @sectionId OR @sectionId IS NULL)
    ORDER BY ISNULL(REPLACE(parentPage.url,'~','') + parentPage.alias,REPLACE(p.url,'~',''))

END

ありがとうございました。

4

1 に答える 1

1

テーブルページに列IDがある場合は注意してください。display_name の代わりに使用する必要があります

;WITH a as
(
SELECT  
        /*p.id,*/
        p.display_name AS Name,
        p.url AS Url,
        ISNULL(REPLACE(parentPage.url,'~','') + parentPage.alias,REPLACE(p.url,'~',''))  as ParentPageURL,
        p.page_image AS PageImage,
        p.synopsis AS Synopsis,
        p.metatitle AS Metatitle,
        t.tag_name as tag,
        ISNULL(REPLACE(parentPage.url,'~','') 
        + parentPage.alias,REPLACE(p.url,'~','')) XXX
        FROM page p
        LEFT JOIN page parentPage on parentPage.id = p.parentid
        LEFT JOIN page_features pf on p.id = pf.pageid and (pf.feature_type = 'TEXT' OR pf.feature_type = 'BLOG')
        JOIN dbo.tag_collection_tag tc on tc.tag_collection_id = p.tag_collection_id
        JOIN dbo.tag t on t.id = tc.tag_id 
        JOIN dbo.[Split](UPPER(@tags),',') Split on UPPER(t.tag_name) like '%' + split.Data + '%'

WHERE p.status = 'PUBLISHED'
AND p.include_in_nav = 1
AND (p.pagelevel = @level OR @level IS NULL)
AND (p.section_id = @sectionId OR @sectionId IS NULL)
)
SELECT Name, Url, ParentPageURL, PageImage, Synopsis, Metatitle
    ,STUFF(( 
        select ',' + [tag] 
        from a t1 
        -- I assume display_name is unique. I would use page.id, 
        -- but I am not sure you have that column
        -- t1.id = t.id
        where t1.display_name = t.display_name
        for xml path(''), type 
    ).value('.', 'varchar(max)'), 1, 1, '') [tags] 
from a t 
group by Name, Url, ParentPageURL, PageImage, Synopsis, Metatitle
ORDER BY XXX
于 2013-10-23T11:53:31.443 に答える