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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you just want an overall total, there is no need to do a count, just SUM() the prices directly.
from log
where 1 = 1
and time > Convert(Datetime, Convert(varchar, GetDate()-1, 101))
and time < Convert(Datetime, Convert(varchar, GetDate(), 101))