查询SQL数据库

时间:2015-11-08 10:38:35

标签: android android-sqlite

我正在尝试用数据库创建一个android应用程序。但我没有多少经验。我想帮助查询我的数据库。 我的桌子"危机"如下:

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

现在我想知道怎样才能确定"危机"一天,一个月甚至一年。例如每日危机的次数:

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

我也有每天,每周甚至一年的平均攻击持续时间。 请,任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:3)

您需要的是每组只有GROUP BYCOUNT,但在使用之前,您需要将日期时间转换为:

  • DATE function约会以获取每天的计数。
  • Year获得年度统计数。
  • month(或月份名称)以获取每月点数。
  • WEEK获取每周计数。

    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);
    

对于每次危机的平均持续时间,您需要先使用TIMEDIFF获取每次危机的持续时间:

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

这会给你这样的东西:

enter image description here

然后,您可以使用AVG来获取整个危机期间的平均值:

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

这会给你类似的东西:

enter image description here

平均后SEC_TO_TIME的使用是显示从整数到hh:mm:ss格式的秒数。

要获得每个月,每年或每周的平均值,您只需要添加一个组:

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

这会给你:

enter image description here

答案 1 :(得分:0)

Group byCount

一起使用
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)

对于平均时差使用: -

AVG(timestampdiff(SECOND, startDate, EndDate))

答案 2 :(得分:0)

如果使用MySQL作为标记建议:

每天的危机数量,周末年份:

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)

每天平均持续时间,结束一周:

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)

使用的函数:https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

答案 3 :(得分: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