Wybierz z powrotem listę rozdzielaną przecinkami pogrupowaną według identyfikatora
Mam następujące tabele:
EntryTag
---------
EntryID
TagID
Przykład putput (EntryID, TagID):
1 2
1 4
1 5
2 3
2 4
2 5
etc...
i
Tags
----
TagID
Name
przykładowe wyjście:
1 peas
2 corn
3 carrots
...etc.
Chcę przywrócić listę znaczników na wpis, ale jako jedną linię, w której znaczniki są rozdzielane przecinkami.
Na przykład chcę to zobaczyć:
EntryID TagsCommaDelimited
------- ------------------
1 corn, peas, carrots
2 barley, oats
...and so on
Muszę więc wymienić każdy EntryID i odpowiadającą mu listę znaczników rozdzielonych przecinkami.
I wybieram z tabeli Zawartość, która wygląda tak:
Content
--------
ID -(which is in essence the EntryID, they didn't make it consistent)
Description
..etc.
Oto, co próbowałem, ale bez powodzenia w mojej składni:
declare @tagsCommaDelimited varchar (200)
set @tagsCommaDelimited = '';
With AllEntryTags_CTE(Name, EntryID )
as
(
select Tags .Name,
entryTags.EntryID
from EntryTag entryTags
join Tags on tags.Id = entryTags.TagID
group by entryTags.EntryID, tags.Name, entryTags.TagID
),
TagsByEntryCommaDelimited_CTE( EntryID, CommaDelimitedTags)
as
(
select distinct allTags.EntryID,
(select @tagsCommaDelimited from ( select @tagsCommaDelimited = coalesce (case when @tagsCommaDelimited = '' then allTags.Name
else @tagsCommaDelimited + ',' + allTags.Name end ,'') as CommaDelimitedTags
from AllEntryTags_CTE allTags
)
select EntryID, CommaDelimitedTags from TagsByEntryCommaDelimited_CTE
---------------------------AKTUALIZACJA---------------------- ------------
na razie poszedłem z Gotgnem do testów
Problem, jaki mam teraz, polega na tym, że próbuję użyć tego ostatniego CTE w mojej ostatniej instrukcji select, aby pobrać listę nazw tagów rozdzielonych przecinkami ... ale to mówi, że moja składnia nie jest właściwa:
;WITH CommaDelimitedTagIDs AS
(
SELECT DISTINCT EntryID,
(SELECT SUBSTRING((SELECT ',' + CAST(TagID AS NVARCHAR(10))
FROM EntryTag AS T1 WHERE T1.EntryID=T2.EntryID
ORDER BY TagID
FOR XML PATH('')),2,200)) AS commaDelimitedTagIDs
FROM EntryTag T2
),
CommaDelimittedTagNames_CTE (EntryID, CommaDelimitedTagNames) as
(
SELECT EntryID, (SELECT SUBSTRING((SELECT ',' + Name
FROM Tags
WHERE commaDelimitedTagIDs LIKE '%'+CAST(ID AS NVARCHAR(5))+'%'
ORDER BY ID FOR XML PATH('')),2,200) AS CSV)
FROM CommaDelimitedTagIDs
)
--select EntryID, CommaDelimitedTagNames from CommaDelimittedTagNames_CTE
SELECT Title,
[Description],
DateSyndicated,
DateUpdated,
1,
CAST([Text] AS NVARCHAR(MAX)),
Author,
(select CommaDelimitedTagNames from CommaDelimittedTagNames_CTE) as tagNamesCommaDelimited
FROM Content
Join CommaDelimittedTagNames_CTE tags on tags.EntryID = Content.ID
group by ID, Title, [Description],
DateSyndicated, DateUpdated,
CAST(subtextContent.[Text] AS NVARCHAR(MAX)), Author
próbowałem tego w ten sposób, bez szczęścia
Select
....other fields
(select CommaDelimitedTagNames from CommaDelimittedTagNames_CTE tagNames
join subContent on subContent.ID = tagNames.EntryID) as tags
FROM Content as subContent
ok Myślę, że nie możesz dołączyć, musiałem go zmienić na Where. Nie wiem dlaczego, ale to działa teraz:
Select
....other fields
(select CommaDelimitedTagNames from CommaDelimittedTagNames_CTE tagNames
where Content.ID = tagNames.EntryID) as tags
FROM Content