Link to home
Start Free TrialLog in
Avatar of bikieswim
bikieswim

asked on

How to add all the prices and give total in one Select statement

I have a scenario where in I want to have a result that counts each price and the related number of billables and then add all those up and give me a total.

Basically I have this code that gives me the price and the number of billables for each price.

select price, count(price)
from log
where 1=1
and time > Convert(Datetime, Convert(varchar, GetDate()-1, 101))
and time < Convert(Datetime, Convert(varchar, GetDate(), 101))
group by price

Now I get the result like below:
$0.30      31
$0.75      126
$0.99      44

Now, I want to take this and give a result so that I get a total like below:
$0.30 * 31 + $0.75 * 126 + $0.99 * 44

How can I do this, and can I do all this in one Select, if not, it is ok.. please let me know ...however possible.

Thanks,
Swim.
ASKER CERTIFIED SOLUTION
Avatar of rafrancisco
rafrancisco

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott Pletcher
select sum(price) as [Total Price]
from log
where 1 = 1
and time > Convert(Datetime, Convert(varchar, GetDate()-1, 101))
and time < Convert(Datetime, Convert(varchar, GetDate(), 101))
If you just want an overall total, there is no need to do a count, just SUM() the prices directly.