WITH tagAndUserStats AS ( SELECT t.tagname, p.owneruserid...

0

Please login or register to vote for this query.

(click on this box to dismiss)

CiviCRM

Q&A for administrators and users of the CiviCRM Constituent Relationship Management software

WITH tagAndUserStats AS (
    SELECT
                t.tagname,
                p.owneruserid       AS userid,
                SUM (p.score)       AS tagScore,
                COUNT (p.id)        AS postCount
    FROM        posts       p
    INNER JOIN  posttags    pt      ON pt.postid = COALESCE (p.parentid, p.id)
    INNER JOIN  tags        t       ON pt.tagid = t.id
    WHERE       p.owneruserid >= 1
    AND         p.posttypeid IN (1, 2)
    GROUP BY    t.tagname,
                p.owneruserid
)

SELECT u.Id AS [User Link],
                            CONCAT('https://serfault.com/users/', u.Id) AS [Profile Link],
                            u.DisplayName,
                            u.Location,
                            u.WebsiteUrl,
                            u.AboutMe,
                            u.Views,
                            u.UpVotes,
                            u.DownVotes,
                            u.Reputation,
            (   SELECT TOP 1    tu.tagname
                FROM            tagAndUserStats tu
                WHERE           tu.userid = u.id
                ORDER BY        tu.tagScore DESC
            ) AS [Top Tag by Score],
            (   SELECT TOP 1    tu.tagname
                FROM            tagAndUserStats tu
                WHERE           tu.userid = u.id
                ORDER BY        tu.postCount DESC
            ) AS [Top Tag by Posts]
FROM        users u

WHERE LOWER(Location) LIKE '%stuttgart%'

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results