0

I am trying to make an android application with a database. But I have not much experience. I would like some help to make query to my database. My table "crisis" is as follows:

ID  StartDate             EndDate
1   2014-11-05 19:26:16   2014-11-05 19:26:46
2   2014-11-05 19:33:33   2014-11-05 19:33:43
3   2014-11-05 19:33:53   2014-11-06 19:35:14
4   2014-11-06 19:35:24   2014-11-06 19:35:54
5   2014-10-07 09:12:00   2014-10-07 09:12:34
6   2014-10-07 09:18:08   2014-10-07 09:19:11
7   2014-12-05 08:12:12   2014-12-06 08:13:11
8   2014-12-12 10:12:00   2014-12-12 10:12:26
9   2014-12-13 07:33:22   2014-12-13 07:33:59

Now I'd like to know how can I do to have the number of "crisis" a day, a month or even a year. For example the number of daily crisis:

Days        NumberOfCrisis
2014-11-05  3
2014-11-06  1

I am also having the average duration of attacks per day, week and even year. Please, any help would be much appreciated.

3
  • is it sqlite or mysql ??? Commented Nov 8, 2015 at 11:00
  • Which DBMS are you using? Postgres? Oracle? Commented Nov 8, 2015 at 12:47
  • I use sqlite database Commented Nov 8, 2015 at 22:10

4 Answers 4

3

What you need here is just GROUP BY and COUNT for each group, but before use the group by you need to cast the datetime to:

  • date with DATE function to get counts for each days.
  • Year to get yearly count.
  • month (or monthname) to get monthly counts.
  • WEEK to get weekly counts.

    SELECT DATE(startDate) AS Days, COUNT(ID) AS NumberOfCrisis
    FROM crisis
    GROUP BY DATE(startDate);
    
    ------
    
    SELECT monthname(startDate) AS Month, COUNT(ID) AS NumberOfCrisis
    FROM crisis
    GROUP BY monthname(startDate);
    
    -------
    
    SELECT YEAR(startDate) AS Year, COUNT(ID) AS NumberOfCrisis
    FROM crisis
    GROUP BY YEAR(startDate);
    
    -------
    
    SELECT Week(startDate) AS Weeks, COUNT(ID) AS NumberOfCrisis
    FROM crisis
    GROUP BY Week(startDate);
    

For the average duration for each crisis, you need to get the duration for each crisis first using TIMEDIFF:

SELECT ID, TIMEDIFF(EndDate, StartDate) AS CrisisDuration
FROM crisis

This will give you something like this:

enter image description here

Then you can use AVG to get the average of the duration across all the crisis:

SELECT SEC_TO_TIME(AVG(TIMEDIFF(EndDate, StartDate))) AS AverageCrisisDuration
FROM crisis;

This will give you something like:

enter image description here

The use of SEC_TO_TIME after average is to display the seconds from integer to hh:mm:ss format.

And to get the average for each month, year, or week, you just need to add a group by:

SELECT 
  Monthname(startdate) AS Month, 
  SEC_TO_TIME(AVG(TIMEDIFF(EndDate, StartDate))) AS AverageCrisisDuration
FROM crisis
GROUP BY Monthname(startdate);

This will give you:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you a lot. I just try it works. If I would like to have the average duration of crises?
@MahmoudGamal : you forgot to use AVG ?
@EricMbatchou - Please see my edit for weekly and yearly counts
0

use Group by with Count

SELECT COUNT(startDate) From crisis GROUP BY DATE(startDate)

SELECT COUNT(startDate) From crisis GROUP BY MONTH(startDate)

SELECT COUNT(startDate) From crisis GROUP BY YEAR(startDate)

SELECT COUNT(startDate) From crisis GROUP BY WEEK(startDate)

SELECT COUNT(startDate) From crisis GROUP BY YEAR(startDate)

For average time difference use :-

AVG(timestampdiff(SECOND, startDate, EndDate))

1 Comment

You're missing average duration, and he didn't ask for month (but it's useful for him, I guess...)
0

If using MySQL as your tag suggests :

Number of crisis per day, week end year :

SELECT DATE(startDate) AS Days, COUNT(ID) AS NumberOfCrisis
FROM crisis
GROUP BY DATE(startDate)

SELECT WEEKOFYEAR(startDate) AS Days, COUNT(ID) AS NumberOfCrisis
FROM crisis
GROUP BY WEEKOFYEAR(startDate)

SELECT YEAR(startDate) AS Days, COUNT(ID) AS NumberOfCrisis
FROM crisis
GROUP BY YEAR(startDate)

Average duration per day, week end year :

SELECT DATE(startDate), SEC_TO_TIME (AVG(TIME_TO_SEC(TIMEDIFF(EndDate - StartDate))))
FROM crisis
GROUP BY DATE(startDate)

SELECT WEEKOFYEAR(startDate), SEC_TO_TIME (AVG(TIME_TO_SEC(TIMEDIFF(EndDate - StartDate))))
FROM crisis
GROUP BY WEEKOFYEAR(startDate)

SELECT YEAR(startDate), SEC_TO_TIME (AVG(TIME_TO_SEC(TIMEDIFF(EndDate - StartDate))))
FROM crisis
GROUP BY YEAR(startDate)

Functions used : https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

1 Comment

i really doubt OP really means mysql as he tags also android and sqlite
0
WITH ETC AS(
    SELECT
    CONVERT(VARCHAR(100),startDate,102) AS startDate
     FROM crisis
) SELECT 
    startDate AS Days, 
    COUNT(1) AS NumberOfCrisis
FROM ETC GROUP BY startDate ORDER BY 1 DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.