I have a dynamic query:

ID Name Num
------------
22 A 2
22 B 3
23 C 1
23 D 2
23 E 2

How to create a dynamic query to calculate the total like below?

ID Name Num Total
-----------------
22 A 2 7 <=> (2*2)+3
22 B 3 11 <=> (3*3)+2

23 A 1 5 <=> (1*1)+2+2
23 B 2 7 <=> (2*2)+1+2
23 C 2 7 <=> (2*2)+2+1

The Total equals to Num multiply by itself, and add all Nums that has the same ID.
Total=(Num*Num)+Nums Where ID = ID

Here is my query:
Select ID, Name, Num, (Num*Num)+Num as Total
Where ID = 1

My query returns incorrect Total.

Can you please help?

Thanks.

AS PER MY UNDERSTANDING YOU CAN TRY WITH BELOW SQL:

SELECT ID,Name,NUM,NUM*NUM + (SELECT SUM(NUM) FROM tbl WHERE ID=t2.id AND NUM<>t2.num) 
from tbl t2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.