Solved

sql query grouping by time

Posted on 2014-03-06
2
320 Views
Last Modified: 2014-03-08
Dtime                                    points
2014-02-01 08:00                   .3
2014-02-01 08:15                   .2
2014-02-02 08:00                   .6
2014-02-03 08:00                   .9
2014-02-02  08:15                  .85
2014-02-04  08:00                  .98

I want to group by the time and add the ones that are over .8 and divide by the total
so for above example it will be
8:00----2/4
8:15---1/2
0
Comment
Question by:Angela4eva
2 Comments
 
LVL 9

Expert Comment

by:DrewKjell
ID: 39910790
select DATEPART(hh, dtime) as hour, DATEPART(mm,dtime), SUM(CASE WHEN points >.8 then 1 else 0 end))/count('s')
from [table]
group by DATEPART(hh, dtime) as hour, DATEPART(mm,dtime)

Open in new window

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 39911120
SELECT
    DATEADD(MINUTE, DATEDIFF(MINUTE, 0, Dtime) / 15 * 15, 0) AS DTime,
    SUM(CASE WHEN points > 0.8 THEN 1 ELSE 0 END) / COUNT(*) AS points
FROM ...
GROUP BY     DATEADD(MINUTE, DATEDIFF(MINUTE, 0, Dtime) / 15 * 15, 0)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 32
Problem with SqlConnection 5 117
SQL server 2008 SP4 29 34
Sql query to Stored Procedure 6 20
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

758 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

22 Experts available now in Live!

Get 1:1 Help Now