This site uses the standard prettify, which produces very different colours (strings are red and comments are grey). The code block below is marked with lang-sql
:
-- Tag: Tag for co-tag frequency search
DECLARE @Tag nvarchar(100) = ##Tag:string##
-- Make it a case-insensitive search
SET @Tag = LOWER(@Tag)
-- Co-tag name, count of co-tag instances, and links
-- First link is to site search, finding all questions with at minimum
-- the two specified tags
-- Second link is to a SEDE query, finding all questions with ONLY
-- the two specified tags, and no more.
SELECT t.TagName AS 'Co-Tag', COUNT(t.TagName) AS 'Occurrences',
'site://questions/tagged/' + @Tag + '+' + t.TagName +
'|[' + @Tag + '] [' + t.TagName + ']'
AS 'Posts w/At Least These Tags (Site Search)',
'http://data.stackexchange.com/chemistry/query/650075/' +
'questions-with-exactly-two-specified-tags?Tag1=' + @Tag +
'&Tag2=' + t.TagName + '&OpenQs=Y&ClosedQs=Y|[' +
@Tag + '] [' + t.TagName + ']'
AS 'Posts w/Just These Two Tags (SEDE)'
-- Pull from the entire list of posts first
FROM Posts p
-- Keep all post information; keep only tag info where relevant to the
-- posts that are returned. I think the LEFT JOIN speeds up the search
-- relative to a regular JOIN...?
-- Regardless, this returns one query record for each **tag** (not question),
-- allowing for the "COUNT(...)" above and "GROUP BY ..." below to work
LEFT JOIN Tags t ON p.Tags LIKE '%<' + t.TagName + '>%'
-- Only keep questions; only keep questions that are tagged with the
-- primary tag of interest; discard records marking the occurrences
-- of the primary tag of interest
WHERE p.PostTypeId = 1 AND p.Tags LIKE '%<' + @Tag + '>%'
AND t.TagName <> @Tag
-- Group the results by the secondary tags, enabling the above COUNT
GROUP BY t.TagName
-- Most to least common co-tags
ORDER BY COUNT(t.TagName) DESC