Solved

SQL syntax to count the number of rows where the sum of a column is equal or greater than a value

Posted on 2013-12-13
3
418 Views
Last Modified: 2013-12-13
Hi,

I have a table that records points that users are awarded. A user can have multiple rows in a table as points are awarded at different times. I am trying to write a SQL statement that will count the number of users that have total points greater or equal to 12 points. For example:

User       Points      Date
John        2             2013-12-01
Adam      4             2013-12-02
Mike       6             2013-12-02
John              6             2013-12-04
Mike              8             2013-12-05
John          6             2013-12-06
 
So in this example the count would be 2 (John and Mike).

Any help would be greatly appreciated. Thank you
0
Comment
Question by:bootneck2222
3 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 total points
ID: 39716074
select count(*) 
  from (select [User], SUM(points) as sum_points
          from @myTable
         group by [User]
        having SUM(Points) > 12) t1

Open in new window

0
 
LVL 6

Expert Comment

by:Argenti
ID: 39716078
select user, sum(points)
from mytable t
group by user
having sum(points) >= 12

Open in new window

0
 

Author Closing Comment

by:bootneck2222
ID: 39717007
Thank you Louis01.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Creating and Managing Databases with phpMyAdmin in cPanel.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now