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.
What is the best code pratice to use do the following code,SELECT fo.no as LNum, fo.name as LName, sum(CASE fo.docnome WHEN "In" THEN fo.etotal ELSE 0 END) as In1, sum(CASE fo.docnome WHEN "In2" THEN fo.etotal ELSE 0 END) as In2, sum(In1+In2)/10 as inDec, from fo group by fo.no,fo.name order by fo.nameinstead ofSELECT fo.no as LNum, fo.name as LName, sum(CASE fo.docnome WHEN "In" THEN fo.etotal ELSE 0 END) as In1, sum(CASE fo.docnome WHEN "In2" THEN fo.etotal ELSE 0 END) as In2, ((sum(CASE fo.docnome WHEN "In" THEN fo.etotal ELSE 0 END))+sum(CASE fo.docnome WHEN "In2" THEN fo.etotal ELSE 0 END)))/10 as inDec, from fo group by fo.no,fo.name order by fo.nameI cant use functions and procedures. Is there any better and cleaner way to code this, reusing the calculated values?
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2006-11-14 : 11:43:57
I would use a derived table like this
SELECT [No], [Name], In1, In2, (In1 + In2) / 10.0 AS inDecFROM ( SELECT [No], [Name], SUM(CASE DocNome WHEN 'In' THEN eTotal ELSE 0 END) AS In1, SUM(CASE DocNome WHEN 'In2' THEN eTotal ELSE 0 END) AS In2 FROM fo GROUP BY [No], [Name] ) gORDER BY [Name]