Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

T-SQL Challenge - sum/avg

$
0
0

Hi All,

I am encountering a tricky situation and need your help. We have a table called @InitialFacts that has customer facts as shown in the first code below. There is another table build from this table using the second piece of code shown below, so far so good.

DECLARE @InitialFacts TABLE (CustomerID INT, Fact1 FLOAT, Fact2 FLOAT, Fact3 FLOAT)
INSERT INTO @InitialFacts 
SELECT 123, 2500, 1500, 0.4
UNION ALL
SELECT 234, 3500, 2500, 0.6

DECLARE @FinalFacts TABLE (CustomerID INT, FinalFact FLOAT)
INSERT INTO @FinalFacts
SELECT CustomerID, Fact1/(Fact2*Fact3) FROM @InitialFacts
SELECT * FROM @FinalFacts

The challenge here is that when pulling data from the @FinalFacts table, we’ll be using SUM to aggregate data across multiple customers– SELECT SUM(FinalFact) FROM@FinalFacts. For the above data set, this gives the result as 6.5. However, the requirement is to use Average for fact3 metric so we can’t use SUM(FinalFact). Is there a way to pull data from the @FinalFacts table and SUM fact1 and fact2 and average fact3? For the above data set, the result should be 3 not 6.5. Unfortunately, we only have access to the@FinalFacts table.

Thanks in advance!!


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>