2015-11-08 222 views
0

我想用數據庫做一個android應用程序。但我沒有太多經驗。我想要一些幫助來查詢我的數據庫。 我的表「危機」如下:查詢SQL數據庫

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 

我也有平均的攻擊時間每天,一週甚至一年。 請,任何幫助將不勝感激。

+0

是sqlite還是mysql? – pskink

+0

您正在使用哪些DBMS? Postgres的?甲骨文? –

+0

我使用sqlite數據庫 –

回答

3

你所需要的就是隻爲每一組GROUP BYCOUNT,但在使用前組由你需要的日期時間轉換爲:

  • 日期DATE function以獲得每個天數。
  • Year獲得年度計數。
  • month(或monthname)獲得每月計數。
  • 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

+0

非常感謝。我只是嘗試它的作品。如果我想有平均的危機時間? –

+0

他正在詢問6個查詢,實際上... –

+0

@EricMbatchou - 查看我的編輯 –

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)) 
+0

你錯過了平均持續時間,他沒有要求月(但它對他有用,我猜...) –

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

+0

我真的懷疑OP真的意味着' mysql',因爲他也標記'android'和'sqlite' – pskink

+0

好的...不清楚... –

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