Thinking outside the box

Patron Saint of Lost Yaks
posts - 203, comments - 734, trackbacks - 4

My Links

Advertisement

News

Archives

Post Categories

How to get the productsum from a table

IF EXISTS(SELECT * FROM YourTable WHERE Number = 0)
    SELECT 0.0E
ELSE
    SELECT CASE IsNegativeProduct
               WHEN 1 THEN -EXP(theSum)
               ELSE EXP(theSum)
           END
    FROM   (
               SELECT SUM(LOG(ABS(Number))) AS theSum,
                      SUM(CASE WHEN Number < 0 THEN 1 ELSE 0 END) % 2 AS IsNegativeProduct
               FROM   YourTable
           ) AS d

Print | posted on Wednesday, November 19, 2008 2:13 PM | Filed Under [ SQL Server 2008 Algorithms SQL Server 2005 SQL Server 2000 ]

Feedback

Gravatar

# Mother of the bride dresses.

Bridesmaid dresses. Wedding dresses. Girls holiday dresses. Formal dresses.
9/27/2010 2:04 PM | Dresses.
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET