Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sum a field from sub query

Status
Not open for further replies.

yfatgo

Programmer
Mar 8, 2004
8
IL
Hello, im trying to get the percentage of one field, meaning, to divide the field in the sum of that field.
the thing. the fields are also getting by a query :

select
Query,
Frequency,
Frequency/sum(Frequency) as FreqPerc,
Clicks,
BuyClicks
from (
SELECT top 10 Query,
SUM(Frequency) AS Frequency,
SUM(Clicks) AS Clicks,
SUM(BuyClicks) AS BuyClicks
FROM T_ANLX_REPORT_FRONT_QUERY
where StartTime >= 0 AND EndTime<= 555555555555
GROUP BY Query ) subq
group by subq.Query, subq.Frequency,subq.Clicks, subq.BuyClicks

somehow the Frequency field is equal to the FreqPerc.
what seems to be the problem ?
thanks.
 
Aggregate functions (SUM included) don't work that way. Try something like this:

- insert top 10 into temp table, plus empty column FreqPerc
- calculate total sum for temp table.Frequency
- update FreqPerc with Frequency/total sum
 
Create anothe derived table with the following code:
Code:
SELECT frequency, sum(frequency) from FROM  T_ANLX_REPORT_FRONT_QUERY
where StartTime >= 0  AND EndTime<= 555555555555
GROUP BY Frequency

Then join it to the first one on the Frequency column.


Questions about posting. See faq183-874
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top