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
425 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with SQL field formatting 3 20
xpath sql query 2008 8 44
SQL Query Producing decimal places when it shouldn;t be 8 36
configure dependency in POM for new database 3 18
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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.​
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

17 Experts available now in Live!

Get 1:1 Help Now