• 0

[SQL] Build a SQL Query


Question

Given a SQL Server Database has a table named say mydata, with fields start_time (Date/Time) and duration (number) fields ... The table is actually a telco call data record (fields trimmed). There is no primary key

The requirement is to calculate : -

The sum of duration for a record for all the days of a given month, e.g.

Day 1 .... nnn duration calls

Day 2 .... nnn duration calls

...

Day x .... nnn duration calls

Please help me design a SQL query (based on transact-sql for SQL Server)

Help much appreciated.

Sample data shown in figure attached.

post-47-1087141048.jpg

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

something like this:

we use db2. I'm not sure if the month function is db2-specific.

select start_time, SUM(duration) from mydata where month(start_time) = 1 group by start_time

Link to comment
Share on other sites

  • 0

Try this:

select day(start_time), sum(duration) from mydata where month(start_time)=5 group by day(start_time)

Change the "5" to whatever month you want (January = 1, December = 12).

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.