2

I have this query

Select 
    APM_Application.NAME AS AppName, APM_Application.ID AS AppID,
    APM_Application.NodeID, NodesData.Caption,
    APM_Component.ID AS CompID, APM_Component.NAME AS C
FROM 
    APM_Application 
INNER JOIN
    NodesData ON APM_Application.NodeID = NodesData.NodeID 
INNER JOIN 
    APM_Component ON APM_Application.ID = APM_Component.ApplicationID 
WHERE 
    NodesData.EngineID = 1

Result of the query

I want to count the number of NodeID instances. e.g 37 = Count is 9 and show it on the result

TIA

2
  • Please read meta.stackoverflow.com/questions/285551/… and the accepted answer Commented Mar 24, 2017 at 7:10
  • Which DBMS are you using? Postgres? Oracle? Commented Mar 24, 2017 at 7:10

1 Answer 1

2

One option would be to join to a subquery which finds the counts:

SELECT a1.NAME AS AppName,
       a1.ID AS AppID,
       a1.NodeID,
       nd.Caption,
       ac.ID AS CompID,
       ac.NAME AS C,
       a2.node_count
FROM APM_Application a1
INNER JOIN
(
    SELECT NodeID, COUNT(*) AS node_count
    FROM APM_Application
    GROUP BY NodeID
) a2
    ON a1.NodeID = a2.NodeID
INNER JOIN NodesData nd
    ON a1.NodeID = nd.NodeID
INNER JOIN APM_Component ac
    ON a1.ID = ac.ApplicationID 
WHERE nd.EngineID = 1

This would be your best option if you were using MySQL. If, on the other hand, you are using a database like SQL Server or Oracle, then you could use COUNT() as a window function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.