Ⅰ sql 如何做季度數據統計
3.按季度分組
select to_char(exportDate,'yyyy-Q'),sum(amount) from table1 group by to_char(exportDate,'yyyy-Q')
order by to_char(exportDate,'yyyy-Q');
試試這個吧
Ⅱ sql server如何按季度分組統計所有的數據
和按月份組的原理是一樣的吧!
按月分組
按季度分組和按月分組的區別應該就是時間段的區別吧!
selectcasewhenmonth(date)=1ormonth(date)=2
ormonth(date)=3then'一季度'
whenmonth(date)=4ormonth(date)=5
ormonth(date)=6then'2季度'
whenmonth(date)=7ormonth(date)=8
ormonth(date)=9then'3季度'
whenmonth(date)=10ormonth(date)=11
ormonth(date)=12then'4季度'
else''end,sum(數量)
fromtable
groupby
casewhenmonth(date)=1ormonth(date)=2
ormonth(date)=3then'一季度'
whenmonth(date)=4ormonth(date)=5
ormonth(date)=6then'2季度'
whenmonth(date)=7ormonth(date)=8
ormonth(date)=9then'3季度'
whenmonth(date)=10ormonth(date)=11
ormonth(date)=12then'4季度'
else''end
Ⅲ 已知 ,資料庫兩個欄位,年,月。現在要按照季度分組查詢,怎麼寫sql語句
先case when將月份變成季度,然後再group by即可,SQL如下:
select year, quarter, count(1)
from (
select year,
case
when month < 4 then 1
when month < 7 then 2
when month < 10 then 3
else 4
end as quarter
from table
) as result
group by year, quarter
Ⅳ sql季度匯總
不清楚 年月是按照什麼格式寫的
所以就說下思路
select ORGCODE,
case when month in(1,2,3) then '第一季度'
when month in(4,5,6) then '第二季度' end ji,
sum(TBJIAF) TBJIAF
where year=條件年份
group by ORGCODE,
case when month in(1,2,3) then '第一季度'
when month in(4,5,6) then '第二季度' end
-----------------------------------------------------
差不多這個格式
Ⅳ sql季度匯總
我沒有Oracle環境,這是用sql server做的,可以達到你的要求,這個問題處理的關鍵在於怎樣通過年月來獲得季度的匯總,所以要先把ny給分解成年和季度欄位(case when 那部分,oracle也支持),剩下的就是簡單的group by了。可以參考一下下面的代碼。
select 部門編號,年,季度,sum(值)
from
(
SELECT
ORGCODE 部門編號
,SUBSTRING(ny,1,4) 年
,case when 0<SUBSTRING(ny,5,2) AND SUBSTRING(ny,5,2)<4 THEN 1
WHEN 3<SUBSTRING(ny,5,2)AND SUBSTRING(ny,5,2)<7 THEN 2
WHEN 6<SUBSTRING(ny,5,2)AND SUBSTRING(ny,5,2)<10 THEN 3
WHEN 9<SUBSTRING(ny,5,2)AND SUBSTRING(ny,5,2)<13 THEN 4
ELSE 0
END 季度
,JHRWFZ 值
FROM JXKH_YDH
) tmp
group by 部門編號,年,季度
order by 部門編號
Ⅵ sql語句實現分組統計
方法和詳細的操作步驟如下:
1、第一步,創建一個測試表,詳細代碼見下圖,轉到下面的步驟。
Ⅶ 有1到12月的記錄值,怎樣用sql分組查出每季度的值
SELECT 欄位 from tabel where date1 BETWEEN '2014-01-01 0:00:00' AND '2014-03-31 23:59:59'
SELECT 欄位 from tabel where date1 BETWEEN '2014-04-01 0:00:00' AND '2014-06-30 23:59:59'
SELECT 欄位 from tabel where date1 BETWEEN '2014-07-01 0:00:00' AND '2014-09-30 23:59:59'
SELECT 欄位 from tabel where date1 BETWEEN '2014-10-01 0:00:00' AND '2014-12-31 23:59:59'