次のクエリがあります。
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 行にする必要があります。