㈠ 我寫了一條sql語句查詢northwind資料庫一年每個季度的銷售情況,可是結果每年每季度的結果都是一樣的
select Year as 年份
, 第一季度= sum(case Q when 0 then amount end)
, 第二季度= sum(case Q when 1 then amount end)
, 第三季度= sum(case Q when 2 then amount end)
, 第四季度= sum(case Q when 3 then amount end)
from (
select year(ShippedDate) as Y, month(ShippedDate) / 4 as Q,
UnitPrice*Quantity*(1-Discount) as amount
from Orders,[Order Details]
where Orders.OrderID=[Order Details].OrderID
and year(ShippedDate) is not null
group by year(ShippedDate) , month(ShippedDate) / 4
) as a
group by Y
order by Y
㈡ Sql server判斷某一日期是在第幾季度
DECLARE @month int
set @month=DATEPART(M,CONVERT(varchar(20),GETDATE(),112 ))
select @month as 當前月份,
CASE when @month between 1 and 3 then '第一季度'
when @month between 4 and 6 then '第二季度'
when @month between 7 and 9 then '第三季度'
else '第四季度'
end as 季度
㈢ SQL 如何獲取當前季度
SELECTDATEPART(QUARTER,GETDATE())
㈣ 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 如何做季度數據統計
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 返回季度的函數
(month+2)/3
㈦ oracle中查詢某一季度數據的sql
一般情況下,你的明細數據表都有個日期欄位來表明你明細的時間,
數據量比較小可以這樣
select sum(...) from tabname where to_char(時間,'q')=你想查詢的季度
如果數據量比較大,建議你根據明細表建立一個快速刷新的物化視圖,物化視圖是根據季度、星期等等的預先統計數據,到時候你要的數據直接從物化視圖中查詢即可
㈧ 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 2005 查詢本周/本月/本季度/本年的數據
本周數據:select * from Keywords where datediff(week, Addtime,getdate())=0
本月:select * from Keywords where datediff(month, Addtime,getdate())=0
本季度:select * from Keywords where datediff(quarter, Addtime,getdate())=0
本年:select * from Keywords where datediff(year, Addtime,getdate())=0
㈩ sql--按照季度統計銷售額 怎麼寫
倆方法
selectyear(訂單.訂購日期)年份,
sum(casewhenmonth(訂單.訂購日期)between1and3then訂單明細.單價*訂單明細.數量else0end)一季度銷售金額,
sum(casewhenmonth(訂單.訂購日期)between4and6then訂單明細.單價*訂單明細.數量else0end)二季度銷售金額,
sum(casewhenmonth(訂單.訂購日期)between7and9then訂單明細.單價*訂單明細.數量else0end)三季度銷售金額,
sum(casewhenmonth(訂單.訂購日期)between10and12then訂單明細.單價*訂單明細.數量else0end)四季度銷售金額
from訂單,訂單明細
where訂單.訂單ID=訂單明細.訂單IDandyear(訂單.訂購日期)between1996and1998
groupbyyear(訂單.訂購日期)
selectyear(訂單.訂購日期)年份,
casewhenmonth(訂單.訂購日期)between1and3then'一季度'
whenmonth(訂單.訂購日期)between4and6then'二季度'
whenmonth(訂單.訂購日期)between7and9then'三季度'
whenmonth(訂單.訂購日期)between10and12then'四季度'end季度,
sum(訂單明細.單價*訂單明細.數量)金額
from訂單,訂單明細
where訂單.訂單ID=訂單明細.訂單IDandyear(訂單.訂購日期)between1996and1998
groupbyyear(訂單.訂購日期),
casewhenmonth(訂單.訂購日期)between1and3then'一季度'
whenmonth(訂單.訂購日期)between4and6then'二季度'
whenmonth(訂單.訂購日期)between7and9then'三季度'
whenmonth(訂單.訂購日期)between10and12then'四季度'end
你看你要用哪個