Ⅰ sql語句查詢今年單價總金額
年,季,月,周,天,小時
也就是說你要的是這個六個分別的金額與匯總。
按照從大到小,如果僅僅是按照現有的表來操作,那麼個人覺得毫無疑問的要查6遍,每次匯總都是一遍,如果數據量大的話毫無疑問的卡死。
個人覺得可以先改造一下表,將時間欄位分成這六個部分,然後分組的時候加上rollup,只要注意書寫順序,那麼一次應該就可以了。這是我能想到的減小運算量的一個辦法。
Ⅱ SQL 怎麼按時間匯總累計金額
select 日期,sum(金額) 金額 from 表名 group by 日期
Ⅲ 請問一下,SQL按日期查詢,查詢每一天之前的金額總和
按天累計么?
select 日期, (select sum(金額) from tb where 日期<t.日期) 日累計金額
from tb t
Ⅳ SQL高手前來 怎麼用SQL 語句實現 3個列 金額總和的相加 給我代碼就好 在線等候。
用sum加不就行了?如下
declare @t table (a money,b money,c money)
insert into @t
SELECT 100,300,420 union all
SELECT 43,35,33 union all
SELECT 80,2,1
SELECT sum(a) as a,sum(b) as b,sum(c) as c,sum(a+b+c) as total from @t
Ⅳ sql語句求和
SQL中求和語句分為縱向匯總和橫向匯總語句;
假設數據列為:A、B、C、D、E、F、G
縱向匯總語句:
selectsum(A),sum(B),sum(C),sum(D),sum(E),sum(F),sum(G)from表名
橫向匯總的SQL語句是:
selectA,B,C,D,E,F,G,A+B+C+D+E+F+Gfrom表名
求所有數據總和的SQL語句是:
selectsum(A)+sum(B)+sum(C)+sum(D)+sum(E)+sum(F)+sum(G)from表名
結構化查詢語言是高級的非過程化編程語言,允許用戶在高層數據結構上工作。它不要求用戶指定對數據的存放方法,也不需要用戶了解具體的數據存放方式,所以具有完全不同底層結構的不同資料庫系統, 可以使用相同的結構化查詢語言作為數據輸入與管理的介面。
(5)sql每天金額加總擴展閱讀:
sql參考語句
刪除新表
drop table tabname
增加一個列
Alter table tabname add column col type
添加主鍵
Alter table tabname add primary key(col)
刪除主鍵
Alter table tabname drop primary key(col)
創建索引
create [unique] index idxname on tabname(col….)
Ⅵ sql按照時間查詢金額的總和。如2013-8-14,計算出08-01到這一天總的和。時間是變化的,為datetime 類型。
時間是變化的,為datetime 類型。就相當於是有一個變數的東西吧。
declare @d_date datetime
set @d_date = '2013-08-14' --自己可以設置的時間
declare @d_start datetime
set @d_start = convert(varchar(8),@d_date,120) + '01'表 --計算每月第一天
select sum( 總金額) from 表
where 日期 between @d_start and @d_date
Ⅶ sql怎麼將當月每一天的數據相加,求總和
insertintobartext(time,electricity)VALUES('2/2/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/3/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/4/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/5/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/6/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/7/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/8/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/9/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/10/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/11/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/12/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/13/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/14/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/15/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/16/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/17/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/18/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/19/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/20/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/21/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/22/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/23/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/24/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/25/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/26/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/27/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('2/28/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/1/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/2/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/3/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/4/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/5/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/6/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/7/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/8/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/9/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/10/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/11/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/12/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/13/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/14/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/15/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/16/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/17/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/18/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/19/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/20/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/21/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/22/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/23/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/24/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/25/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/26/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/27/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/28/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/29/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/30/2019',cast(floor(rand()*150)asint))
insertintobartext(time,electricity)VALUES('3/31/2019',cast(floor(rand()*150)asint))
Ⅷ 如何用sql統計總金額
1、如果你是要統計當日銷售總金額,如下:
select
sum(piece*price)
as
'當日銷售總額'
from
當日銷售表
where
date='07/5/16'
2、如果你是要統計各商品當日的銷售額,如下:
select
rq,sno,sname,sum(piece)
as
piece,sum(piece*price)
as
'銷售額'
from
當日銷售表
group
by
rq,sno,sname
Ⅸ sql統計某一表的某欄位的總值(sum)
group
by
後面跟的欄位必須是Select後面不是聚合函數的欄位,少一個都不行。select後面不是聚合函數的欄位都是分組依據(幾個欄位同時相等才會分在一組里統計)
下面這句是按帳戶分組統計(帳戶一樣的消費金額相加):
select
賬戶,sum(消費金額)
as
總金額
from
表
group
by
賬戶
而你給的代碼是按你N個欄位統計,結果肯定不對。
若還想顯示其他欄位,用下面的語句:
select
x.賬戶,消費日期...(你的其他欄位),總金額
from
表
x,(select
帳戶,sum(消費金額)as
總金額
from
表
group
by
賬戶)
as
y
where
x.帳戶=y.帳戶
Ⅹ 能否用一句SQL同時查詢本日和本月金額總值
本月的直接加,本日的用case when來加,腳本實例如下,以oracle為例:
selectsum(金額)本月的金額總數,
sum(casewhen日期欄位>=trunc(sysdate)then金額else0end)今日的金額總數
from表名
where日期欄位>=trunc(sysdate,'mm')
在sqlserver中case when 語法與上面一致:
selectsum(金額)本月的金額總數,
sum(casewhen日期欄位>=cast(convert(varchar,getdate(),112)assmalldatetime)then金額else0end)今日的金額總數
from表名
where日期欄位>=cast(convert(varchar(6),getdate(),112)+'01'assmalldatetime)