1

我有以下查询:

Select MAX("Counter") as "Counter", "DestinationId" FROM (          
 Select t1."Counter", t2.* From
           (SELECT "HierarchyNodeId", (SELECT "Description"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "Description", "DestinationId",
         (SELECT "DestinationTypeId"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "DestinationTypeId"
    FROM "HierarchyNode" hn
WHERE "DestinationId" IN (SELECT "DestinationId" From(SELECT COUNT ("HierarchyNodeId"), "DestinationId" FROM "HierarchyNode" GROUP BY "DestinationId" HAVING COUNT ("HierarchyNodeId") > 1))) t2, 
(select COUNT (*) "Counter", "HierarchyNodeId" From "HierarchyDetail"
Group By "HierarchyNodeId") t1
Where t1."HierarchyNodeId" = t2."HierarchyNodeId"
ORDER BY "DestinationId", "Counter" Desc
)
GROUP BY "DestinationId"

输出显示每个目标 ID 的最大计数器。我需要的额外信息是与该特定行相对应的 HierarchyId(具有该特定目标 ID 和计数器)。

如果我将其更改为:

Select MAX("Counter") as "Counter", "DestinationId", "HierarchyId" FROM ( ....

我收到一个错误,因为我没有在 Group By 中包含 HierarchyId。但是,如果我为 DestinationId - HierarchyId 的每个组合包含一个获取最大计数器(不是每个 DestinationId 仅)。

所以我尝试了这个:

Select * From (
Select t1."Counter", t2.* From
           (SELECT "HierarchyNodeId", (SELECT "Description"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "Description", "DestinationId",
         (SELECT "DestinationTypeId"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "DestinationTypeId"
    FROM "HierarchyNode" hn
WHERE "DestinationId" IN (SELECT "DestinationId" From(SELECT COUNT ("HierarchyNodeId"), "DestinationId" FROM "HierarchyNode" GROUP BY "DestinationId" HAVING COUNT ("HierarchyNodeId") > 1))) t2, 
(select COUNT (*) "Counter", "HierarchyNodeId" From "HierarchyDetail"
Group By "HierarchyNodeId") t1
Where t1."HierarchyNodeId" = t2."HierarchyNodeId"
ORDER BY "DestinationId", "Counter" Desc
) test1
Inner Join
(
Select MAX("Counter") as "Counter", "DestinationId" FROM (          
 Select t1."Counter", t2.* From
           (SELECT "HierarchyNodeId", (SELECT "Description"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "Description", "DestinationId",
         (SELECT "DestinationTypeId"
            FROM "Destination"
           WHERE "DestinationId" = hn."DestinationId") "DestinationTypeId"
    FROM "HierarchyNode" hn
WHERE "DestinationId" IN (SELECT "DestinationId" From(SELECT COUNT ("HierarchyNodeId"), "DestinationId" FROM "HierarchyNode" GROUP BY "DestinationId" HAVING COUNT ("HierarchyNodeId") > 1))) t2, 
(select COUNT (*) "Counter", "HierarchyNodeId" From "HierarchyDetail"
Group By "HierarchyNodeId") t1
Where t1."HierarchyNodeId" = t2."HierarchyNodeId"
ORDER BY "DestinationId", "Counter" Desc
)
GROUP BY "DestinationId"
) test2
On test1."DestinationId" = test2."DestinationId"
And test1."Counter" = test2."Counter"

当第一个查询给我 1256 时,这个给我 1456 条记录......但我只希望第一个查询的每一行都有 1 个 HierachyNodeId。所以这没有意义。它应该是 1256 行。

4

1 回答 1

2

使用keep dense_rank

Select 
    MAX("Counter") as "Counter", 
    "DestinationId" 
    max("HierarchyId") keep (dense_rank first order by counter desc) as "HierarchyId"
FROM ( query)
GROUP BY "DestinationId";
于 2013-01-08T10:48:07.277 回答