需要内部加入两个查询的帮助

时间:2017-12-12 03:40:11

标签: sql

嗨,大家好,你可以帮我创建这个内部联合查询。

这个想法是我需要首先得到哪个是前3个最高关键字计数然后显示每月关键字数量(我只需要月份数)

SELECT ReportRaw.Keyword, Format([DateApplying],'m') AS appdate, Count(ReportRaw.Keyword) AS CountOfKeyword1

FROM 

(
SELECT TOP 3 Count(Keyword) AS CountOfKeyword,Keyword
FROM ReportRaw
GROUP BY Keyword
ORDER BY Count(Keyword) DESC;
) as T1

INNER JOIN ReportRaw

ON T1.Keyword = ReportRaw.Keyword

GROUP BY ReportRaw.Keyword, Format([DateApplying],'m') ;

1 个答案:

答案 0 :(得分:0)

看起来像DESC之后的分号(;)。假设这是SQL Server

对查询进行微小更新以获取该月份数字:

SELECT #ReportRaw.Keyword, DATEPART(MONTH, [DateApplying]) AS appdate, 
 Count(#ReportRaw.Keyword) AS CountOfKeyword1
FROM 
(
    SELECT TOP 3 Count(Keyword) AS CountOfKeyword,Keyword
    FROM #ReportRaw
    GROUP BY Keyword
    ORDER BY Count(Keyword) DESC
) as T1

INNER JOIN #ReportRaw
    ON T1.Keyword = #ReportRaw.Keyword
GROUP BY #ReportRaw.Keyword, DATEPART(MONTH, [DateApplying]) ;