MySql: Year, Quarter, Month, Day, Hour statistics

来源:https://www.cnblogs.com/geovindu/archive/2018/08/24/9529214.html
-Advertisement-
Play Games

-- 統計 select count(*) as '當天記錄數' from web_product where date(p_createtime) = curdate(); select count(*) as '當天記錄數' from web_product where to_days(p_cr... ...


 

 

--  統計

select count(*) as '當天記錄數' from web_product where date(p_createtime) = curdate(); 
select count(*) as '當天記錄數' from web_product where to_days(p_createtime) = to_days(now());

SELECT count(*) as '昨天記錄數'  FROM web_product WHERE TO_DAYS( NOW( ) ) - TO_DAYS( p_createtime) <= 1;


-- 前一天
select count(*) as '前一天記錄數' from web_product where date(p_createtime) = date_sub(curdate(),interval 1 day);

select count(*) as '本周記錄數' from web_product where date(p_createtime) >= date_sub(curdate(),interval 7 day)    
and date(p_createtime) <=  date_sub(curdate(),interval 1 day);

SELECT count(*) as '7天的記錄數' FROM web_product where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(p_createtime);

-- 查詢近30天的記錄
SELECT * FROM web_product where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(p_createtime);

-- 查詢本月的記錄
SELECT * FROM web_product WHERE DATE_FORMAT(p_createtime,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m');


-- 查詢上一月的記錄
SELECT * FROM web_product WHERE PERIOD_DIFF(date_format(now(),'%Y%m'),date_format(p_createtime,'%Y%m'))=1;


-- 查詢本季度數據
select * from web_product where QUARTER(p_createtime)=QUARTER(now());

-- 查詢上季度數據
select * from web_product where QUARTER(p_createtime)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

-- 查詢本年數據
select * from web_product  where YEAR(p_createtime)=YEAR(NOW());

-- 查詢上年數據
select * from web_product where year(p_createtime)=year(date_sub(now(),interval 1 year));


-- 查詢當前這周的數據 
SELECT * FROM web_product WHERE YEARWEEK(date_format(p_createtime,'%Y-%m-%d')) = YEARWEEK(now());

-- 查詢上周的數據
SELECT * FROM web_product WHERE YEARWEEK(date_format(p_createtime,'%Y-%m-%d')) = YEARWEEK(now())-1;

-- 查詢當前月份的數據
select * from web_product   where date_format(p_createtime,'%Y-%m')=date_format(now(),'%Y-%m');
-- 查詢距離當前現在6個月的數據
select p_name,p_createtime from web_product where p_createtime between date_sub(now(),interval 6 month) and now();

-- 按年彙總,統計:

select sum(mymoney) as totalmoney, count(*) as sheets from web_product group by date_format(p_createtime, '%Y');

select date_format(p_createtime, '%Y') as 'year',count(*) as sheets from web_product group by date_format(p_createtime, '%Y');


select DATE_FORMAT(p_createtime,'%Y') years,sum(duration) dur from web_product tv where 1=1 GROUP BY years ORDER BY years desc;

select DATE_FORMAT(p_createtime,'%Y') years,count(*) as sheets  from web_product  where 1=1 GROUP BY years ORDER BY years desc;

SELECT DATE_FORMAT(p_createtime,'%Y') years,COUNT(*) COUNT FROM web_product GROUP BY years;


SELECT year(p_createtime) as 'yearname',count(*) as'sheet' FROM `web_product` group by  yearname;

SELECT count(*), year(p_createtime) yearname  FROM `web_product`  group by  yearname;

SELECT year(p_createtime) yearname  FROM `web_product`;

SELECT  DISTINCT(year(p_createtime)) yearname  FROM `web_product`;

SELECT  COUNT(DISTINCT(year(p_createtime))) yearname  FROM `web_product`;


SELECT year(Addtime) as 'yearname',count(*) as'sheet' FROM `duwebstat` group by  yearname;

SELECT  COUNT(DISTINCT(year(Addtime))) yearname  FROM `duwebstat`;

-- 按月彙總,統計:

select sum(mymoney) as totalmoney, count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m');

select date_format(p_createtime, '%Y-%m') as 'month',count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m');

select DATE_FORMAT(p_createtime,'%Y%m') months,count(*) as sheets from web_product  where 1=1 GROUP BY months ORDER BY months desc;

SELECT DATE_FORMAT(p_createtime,'%Y%m') months,COUNT(*) COUNT FROM web_product GROUP BY months;

SELECT year(p_createtime) as 'yearname',month(`p_createtime`) as 'monthname',count(*) as'sheet' FROM `web_product` group by  yearname,monthname;

SELECT year(Addtime) as 'yearname',month(`Addtime`) as 'monthname',count(*) as'sheet' FROM `duwebstat` group by  yearname,monthname;

SELECT  count(DISTINCT(concat(cast(year(Addtime) as char(50)),cast(month(Addtime) as char(50)))))  FROM duwebstat;

select DATE_FORMAT(Addtime,'%Y-%m') months,count(*) as sheets from duwebstat  where 1=1 GROUP BY months ORDER BY months desc;


-- 按季度彙總,統計:

select sum(mymoney) as totalmoney,count(*) as sheets from web_product group by concat(date_format(p_createtime, '%Y'),FLOOR((date_format(p_createtime, '%m')+2)/3));

select count(*) as sheets from web_product group by concat(date_format(p_createtime, '%Y'),FLOOR((date_format(p_createtime, '%m')+2)/3));

select concat(date_format(p_createtime,'%Y'),FLOOR((date_format(p_createtime, '%m')+2)/3)) quarters,sum(duration) dur from web_product  where 1=1  GROUP BY quarters ORDER BY quarters desc;

select concat(date_format(p_createtime,'%Y'),FLOOR((date_format(p_createtime, '%m')+2)/3)) quarters,count(*) as sheets  from web_product  where 1=1  GROUP BY quarters ORDER BY quarters desc;


SELECT id, year(p_createtime),quarter(`p_createtime`) FROM `web_product`;

SELECT year(p_createtime) as 'yearname',quarter(`p_createtime`) as 'quartername',count(*) as'sheet' FROM `web_product` group by  yearname,quartername;

SELECT  DISTINCT(concat(cast(year(p_createtime) as char(50)),cast(quarter(p_createtime) as char(50))))  FROM web_product;

SELECT  count(DISTINCT(concat(cast(year(p_createtime) as char(50)),cast(quarter(p_createtime) as char(50)))))  FROM web_product;


select CAST(122 as CHAR);

select now();

select quarter(now());

SELECT CAST(123 AS CHAR); 

select concat(DATE_FORMAT(now(),'%Y'),cast(quarter(now()) as char(20)));



SELECT year(Addtime) as 'yearname',quarter(`Addtime`) as 'quartername',count(*) as'sheet' FROM `duwebstat` group by  yearname,quartername;


SELECT  COUNT(DISTINCT(year(Addtime))) yearname  FROM `duwebstat`;


SELECT  count(DISTINCT(concat(cast(year(Addtime) as char(50)),cast(quarter(Addtime) as char(50)))))  FROM duwebstat;

--  按周統計
select DATE_FORMAT(p_createtime,'%Y%u') weeks,count(*) as sheets from web_product where 1=1 GROUP BY weeks ORDER BY weeks desc;

select DATE_FORMAT(p_createtime,'%Y-%u') weeks,count(*) as sheets from web_product where 1=1 GROUP BY weeks ORDER BY weeks desc;

SELECT DATE_FORMAT(p_createtime,'%Y%u') weeks,COUNT(*) COUNT FROM web_product GROUP BY weeks;

SELECT DATE_FORMAT(Addtime,'%Y-%u') weeks,COUNT(*) COUNT FROM duwebstat GROUP BY weeks;


select DISTINCT(DATE_FORMAT(p_createtime,'%Y-%u')) from web_product;

SELECT year(p_createtime) yearname,week(p_createtime) weeks,COUNT(*) COUNT FROM web_product GROUP BY weeks,yearname;

SELECT year(Addtime) yearname,week(Addtime) weeks,COUNT(*) COUNT FROM duwebstat GROUP BY weeks,yearname;

select DATE_FORMAT(Addtime,'%Y%u') weeks,count(*) as sheets from duwebstat where 1=1 GROUP BY weeks ORDER BY weeks desc;

select count(DISTINCT(DATE_FORMAT(Addtime,'%Y-%u'))) from duwebstat;

-- 按日統計
-- https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

SELECT DATE_FORMAT(p_createtime,'%Y%m%d') days,COUNT(*) COUNT FROM web_product GROUP BY days; 

SELECT DATE_FORMAT(p_createtime,'%Y-%m-%d') days,COUNT(*) COUNT FROM web_product GROUP BY days; 

SELECT DATE_FORMAT(Addtime,'%Y-%m-%d') days,COUNT(*) as sheet FROM duwebstat GROUP BY days;

select count(DISTINCT(DATE_FORMAT(Addtime,'%Y-%m-%d'))) from duwebstat;


--
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');


-- 
-- 按小時:Hour

select date_format(p_createtime, '%Y-%m-%d %H'),count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m-%d %H');

select date_format(p_createtime, '%Y-%m-%d %H'),count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m-%d %H') limit 0,30;


select date_format(Addtime, '%Y-%m-%d %H') as hours,count(*) as sheet from duwebstat group by date_format(Addtime, '%Y-%m-%d %H');


select sum(mymoney) as totalmoney,count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m-%d %H ');

-- 查詢 本年度的數據:

SELECT * FROM web_product WHERE year(FROM_UNIXTIME(p_createtime)) = year(curdate());

-- 查詢數據附帶季度數:

SELECT id, quarter(FROM_UNIXTIME(p_createtime)) FROM web_product;

-- 查詢 本季度的數據:

SELECT * FROM web_product WHERE quarter(FROM_UNIXTIME(p_createtime)) = quarter(curdate());

-- 本月統計:

select * from web_product where month(p_createtime) = month(curdate()) and year(p_createtime) = year(curdate());

-- 本周統計:

select * from web_product where month(p_createtime) = month(curdate()) and week(p_createtime) = week(curdate());

  


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 索引,是資料庫中專門用於幫助用戶快速查詢數據的一種數據結構。類似於字典中的目錄,查找字典內容時可以根據目錄查找到數據的存放位置,然後直接獲取即可。 以 B-tree 形式存儲: MySQL中常見索引有: 普通索引 唯一索引 主鍵索引 組合索引 1、普通索引 普通索引僅有一個功能:加速查詢 1 cre ...
  • 今年6月畢業,來到公司前前後後各種事情折騰下來,8月中旬才入職。本以為終於可以靜下心來研究技術了,但是又把我分配到了一個幾乎不做技術的解決方案部門,導致現在寫代碼的時間都幾乎沒有了,所以只能在每天下班後留在公司研究一下自己喜歡的技術,搞得特別晚才回,身心俱疲。 唉~以前天天寫代碼時覺得苦逼,現在沒得 ...
  • 事務 事務用於將某些操作的多個SQL作為原子性操作,一旦有某一個出現錯誤,即可回滾到原來的狀態,從而保證資料庫數據完整性。 1 delimiter \\ 2 create PROCEDURE p1( 3 OUT p_return_code tinyint 4 ) 5 BEGIN 6 DECLARE ...
  • 概述 對於二進位安裝,優點是可以安裝到任何路徑下,靈活性好,一臺伺服器可以安裝多個mysql。缺點是已經繹過編譯,性能不如源碼編譯得好,不能靈活定製編譯參數。如果用戶即不想安裝最簡單卻不夠靈活的RPM包,又不想安裝複雜費時的源碼包,那麼已編譯好的二進位包將是最好的選擇。 一.步驟1: 解壓glib包 ...
  • 今天需要在本地建個資料庫,就下載安裝sql,第一次弄,遇到了一些問題,環境添加到 sql的bin目錄,要用管理員命令運行cm,cd到sql/bin的目錄,輸入 net start mysql 運行sql,可能會出現密碼錯誤,到配置文件,在[mysqld]加入skip-grant-tables 可以不 ...
  • 一致性Hash演算法。 Hash演算法是為了保證數據均勻的分佈,例如有3個桶,分別是0號桶,1號桶和2號桶;現在有12個球,怎麼樣才能讓12個球平均分佈到3個桶中呢?使用Hash演算法的做法是,將12個球從0開始編號,得到這樣的一個序列:0,1,2,3,4,5,6,7,8,9,10,11。將這個序列中的每 ...
  • HBase 數據讀寫過程描述 我們熟悉的在 Hadoop 使用的文件格式有許多種,例如: Avro:用於 HDFS 數據序序列化與 Parquet:常見於 Hive 數據文件保存在 HDFS中 HFile HFile 是 HBase 使用的一種柱狀存儲文件,非常適合隨機和實時讀寫。 HFile 文件 ...
  • 是參考 一下兩篇博文整理了下。 Redis: https://www.cnblogs.com/5ishare/p/6492380.html RabbitMq: https://www.cnblogs.com/yangecnu/p/4227535.html 話不多說,下麵直接貼demo 百度雲盤 地址 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...