WITH TempTable AS ( SELECT OwnerUserId, COUNT(CASE WHEN C...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Amateur Radio Meta

Q&A about the site for amateur radio enthusiasts

WITH TempTable AS (
    SELECT 
        OwnerUserId,
        COUNT(CASE WHEN CreationDate BETWEEN '2019-09-01' AND '2019-11-24' THEN Id ELSE NULL END) AS CountPeriodA,
        COUNT(CASE WHEN CreationDate BETWEEN '2019-12-01' AND '2020-02-23' THEN Id ELSE NULL END) AS CountPeriodB
    FROM posts
    WHERE PostTypeId = 1
    GROUP BY OwnerUserId
    HAVING 
        COUNT(CASE WHEN CreationDate BETWEEN '2019-09-01' AND '2019-11-24' THEN Id ELSE NULL END) >= 1
        AND 
        COUNT(CASE WHEN CreationDate BETWEEN '2019-12-01' AND '2020-02-23' THEN Id ELSE NULL END) >= 1
),
FINAL AS (
    SELECT 
        ROW_NUMBER() OVER (ORDER BY badges.UserId DESC) AS IndexColumn,
        Badges.UserId,
        FORMAT(Badges.Date, 'yyyy-MM-dd') AS CreationDate,        
        SUM(CASE WHEN badges.class = 1 THEN 1 ELSE 0 END) AS gold,
        SUM(CASE WHEN badges.class = 2 THEN 1 ELSE 0 END) AS silver,
        SUM(CASE WHEN badges.class = 3 THEN 1 ELSE 0 END) AS bronze
    FROM badges
    INNER JOIN TempTable ON badges.UserId = TempTable.OwnerUserID
    WHERE badges.Date BETWEEN '2019-09-01' AND '2020-02-23'
    GROUP BY badges.UserId, FORMAT(Badges.Date, 'yyyy-MM-dd')
    )
SELECT top 5 * 
FROM FINAL
Order by Indexcolumn desc;

Enter Parameters

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