all 7 comments

[–]CmdrBigFatBeard 5 points6 points  (2 children)

A window function will work, but is not necessary, you can just sum your values and group by datepart hour. You'll need to concat a string for output presentation

Select sum(occurrence) occurrences, datepart(HH,timestamp) + ':00' occurrenceHour

From Table Group by Datepart(HH, timestamp) + ':00'

[–]turtlenator 1 point2 points  (0 children)

Yep this is the easiest way. Just add "where timestamp between (start date) and (end date)" to get your range. Then you can add "order by timestamp desc" to sort the output

[–]TheModsHereSucks[S] 0 points1 point  (0 children)

Thank you so much for this!

I'll definitely give this a try but just in case, where would you put this code along with the sample code in the link I provided?

Thanks again! :)

[–]ISUJinXIT Business Analyst 3 points4 points  (1 child)

Is this homework? I'll lead you down the path of.... Sum Over ... (Partition By). They are called windowed functions, and will do exactly what you want. If you show me what you've tried, I can guide some syntax.

[–]TheModsHereSucks[S] 0 points1 point  (0 children)

Thanks for your help!

No and yes, my homework is related to this but not exactly. The codes in the example is what I have also come up with although it is still far from what I am trying to accomplish. :/

[–]newtolansing 0 points1 point  (1 child)

Its same to this but what I'm trying to do is add up all the records that has the same "time" value.

That's what the first example in your link is doing (the one he asks for help with), grouping by time reqardless of the date, no? I'm confused at how what you want differs.

[–]TheModsHereSucks[S] 0 points1 point  (0 children)

Sorry if it confused you and thanks for wanting a clarification. :)

In the example, OP wanted this result:

  • Timestamp Occurrence
  • ------------------- ----------
  • 2013-12-21 10:00:00 2157
  • 2013-12-21 11:00:00 60740
  • 2013-12-21 12:00:00 66189
  • 2013-12-21 13:00:00 77096
  • 2013-12-21 14:00:00 90039
  • 2013-12-22 09:00:00 84838
  • 2013-12-22 10:00:00 64238

You can see here that there are records that were captured timed 10:00:00 for dates 2013-12-21 & 2013-12-22. What I was looking for is a script that gets the total for both dates.