在一个查询中返回多个组中的最高“ X”记录

时间:2019-01-15 11:40:04

标签: ms-access ms-access-2016

我有多个月份(即9月,10月,11月等)的记录列表,我需要按同一查询中每个月的计数返回前10个结果。到目前为止,我有以下内容:

SELECT [Financial Year] & "Resolved" & [Text Month] & [SegmentDescriptionST] AS [Key], "FY" & IIf(Month([MinOfResolvedDate])<10,Year([MinOfResolvedDate]),Year([MinOfResolvedDate])+1) AS [Financial Year], "Resolved" AS Metric, Format([MinOfResolvedDate],"mmm-yy") AS [Resolved Month], Format([MinOfResolvedDate],"mmm") AS [Text Month], Content_Accuracy_q.SegmentDescriptionST, Count(Content_Accuracy_q.SegmentDescriptionST) AS Amount

FROM Content_Accuracy_q

WHERE (((([Content_Accuracy_q].[MinOfResolvedDate]) Between #9/1/2018# And (DateSerial(Year(Date()),Month(Date()),0))) In 
    (SELECT TOP 10 Content_Accuracy_q.SegmentDescriptionST

    FROM Content_Accuracy_q

    Where ((Content_Accuracy_q.[Level 3 - Issue ST])="Inaccurate Information" Or (Content_Accuracy_q.[Level 3 - Issue ST])="Change of accommodation") AND ((Content_Accuracy_q.[Level 4 - Reason ST])<>"Cost"))))
    GROUP BY "FY" & IIf(Month([MinOfResolvedDate])<10,Year([MinOfResolvedDate]),Year([MinOfResolvedDate])+1), "Resolved", Format([MinOfResolvedDate],"mmm-yy"), Format([MinOfResolvedDate],"mmm"), Content_Accuracy_q.SegmentDescriptionST

    HAVING (((Content_Accuracy_q.SegmentDescriptionST)<>""))

    ORDER BY Format([MinOfResolvedDate],"mmm-yy") DESC , Count(Content_Accuracy_q.SegmentDescriptionST) DESC)

ORDER BY Format([MinOfResolvedDate],"mmm-yy") DESC , Count(Content_Accuracy_q.SegmentDescriptionST) DESC;

但是,我不断收到“ ORDER BY子句中的语法错误”,无法超越这一点。有没有人能够成功运行与此类似的查询,并在所需的月份内按计数返回前X个结果?

1 个答案:

答案 0 :(得分:0)

在使用缩进格式格式化SQL时,代码中有些奇怪之处:<​​/ p>

SELECT 
    [Financial Year] & "Resolved" & [Text Month] & [SegmentDescriptionST] AS [Key], 
    "FY" & IIf(Month([MinOfResolvedDate])<10,Year([MinOfResolvedDate]),Year([MinOfResolvedDate])+1) AS [Financial Year], 
    "Resolved" AS Metric, 
    Format([MinOfResolvedDate],"mmm-yy") AS [Resolved Month], 
    Format([MinOfResolvedDate],"mmm") AS [Text Month], 
    Content_Accuracy_q.SegmentDescriptionST, 
    Count(Content_Accuracy_q.SegmentDescriptionST) AS Amount
FROM 
    Content_Accuracy_q
WHERE 
    (
        (
            (
                [Content_Accuracy_q].[MinOfResolvedDate] BETWEEN #9/1/2018# AND DateSerial(Year(Date()),Month(Date()),0)
            )
            IN 
            (
                SELECT TOP 10 Content_Accuracy_q.SegmentDescriptionST
                FROM Content_Accuracy_q
                WHERE 
                (
                    Content_Accuracy_q.[Level 3 - Issue ST] = "Inaccurate Information" OR 
                    Content_Accuracy_q.[Level 3 - Issue ST] = "Change of accommodation"
                )
                AND 
                Content_Accuracy_q.[Level 4 - Reason ST] <> "Cost"
            )
        )
    )
GROUP BY 
    "FY" & IIf(Month([MinOfResolvedDate])<10,Year([MinOfResolvedDate]),Year([MinOfResolvedDate])+1), 
    "Resolved", 
    Format([MinOfResolvedDate],"mmm-yy"), 
    Format([MinOfResolvedDate],"mmm"), 
    Content_Accuracy_q.SegmentDescriptionST
HAVING 
    Content_Accuracy_q.SegmentDescriptionST <> ""
ORDER BY 
    Format([MinOfResolvedDate],"mmm-yy") DESC, 
    Count(Content_Accuracy_q.SegmentDescriptionST) DESC)
ORDER BY 
    Format([MinOfResolvedDate],"mmm-yy") DESC,
    Count(Content_Accuracy_q.SegmentDescriptionST) DESC;

首先,明显的错误是您的第一个order by子句中的括号不平衡:

ORDER BY 
    Format([MinOfResolvedDate],"mmm-yy") DESC, 
    Count(Content_Accuracy_q.SegmentDescriptionST) DESC)
                                                       ^------- here

但是您还有两个order by子句,这是无效的语法:

ORDER BY 
    Format([MinOfResolvedDate],"mmm-yy") DESC, 
    Count(Content_Accuracy_q.SegmentDescriptionST) DESC)
ORDER BY 
    Format([MinOfResolvedDate],"mmm-yy") DESC, 
    Count(Content_Accuracy_q.SegmentDescriptionST) DESC;

having子句也似乎是多余的,因为条件没有引用聚合的结果字段:

HAVING 
    Content_Accuracy_q.SegmentDescriptionST <> ""

因此,该标准可以改为包含在where子句中:

FROM 
    Content_Accuracy_q
WHERE 
    Content_Accuracy_q.SegmentDescriptionST <> "" AND

但是,看看您的where子句,这句话对我来说没有意义:

(
    [Content_Accuracy_q].[MinOfResolvedDate] BETWEEN #9/1/2018# AND DateSerial(Year(Date()),Month(Date()),0)
)
IN 
(
    SELECT TOP 10 Content_Accuracy_q.SegmentDescriptionST
    FROM Content_Accuracy_q
    WHERE 
    (
        Content_Accuracy_q.[Level 3 - Issue ST] = "Inaccurate Information" OR 
        Content_Accuracy_q.[Level 3 - Issue ST] = "Change of accommodation"
    )
    AND 
    Content_Accuracy_q.[Level 4 - Reason ST] <> "Cost"
)

在这里,代码在说:

“选择记录,其中[Content_Accuracy_q].[MinOfResolvedDate] BETWEEN #9/1/2018# AND DateSerial(Year(Date()),Month(Date()),0)的结果是SELECT TOP 10 Content_Accuracy_q.SegmentDescriptionST ...返回的结果之一”

但是表达式:

[Content_Accuracy_q].[MinOfResolvedDate] BETWEEN #9/1/2018# AND DateSerial(Year(Date()),Month(Date()),0)

将返回布尔值(即True/False -1/0);而且我猜测您不是要检查布尔值是否是子查询返回的结果的成员。