Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have a dynamic query:ID Name Num------------22 A 222 B 323 C 123 D 223 E 2How to create a dynamic query to calculate the total like below?ID Name Num Total-----------------22 A 2 7 <=> (2*2)+322 B 3 11 <=> (3*3)+223 A 1 5 <=> (1*1)+2+223 B 2 7 <=> (2*2)+1+223 C 2 7 <=> (2*2)+2+1The Total equals to Num multiply by itself, and add all Nums that has the same ID.Total=(Num*Num)+Nums Where ID = IDHere is my query:Select ID, Name, Num, (Num*Num)+Num as TotalWhere ID = 1My query returns incorrect Total.Can you please help?Thanks.There is no stupid question.www.single123.com
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-05-17 : 13:42:31
if you want Access query post this in Access forum. In sql server, this can be done like below
SELECT t.ID,t.Name,t.Num,POWER(t.Num,2) + (SELECT SUM(t.Num) FROM Table WHERE ID=t.ID AND Name<> t.Name) AS TotalFROM Table t