我有以下查询:
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"
当FIRST查询给我1256时,这个给了我1456条记录...... 但我只想为第一个查询的每一行提供1个HierachyNodeId。所以它没有意义。 它应该是1256行。
答案 0 :(得分: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";