troubleshooting Question

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

Avatar of bikieswim
bikieswim asked on
Microsoft SQL Server
3 Comments1 Solution330 ViewsLast Modified:
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.

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros