if exists(select table_name from information_schema.tables
where table_name = 'accountmessage')
drop table accountmessage
gocreate table accountmessage(
ffundcode varchar(6) not null,
fac**ame varchar(20) not null,
fac**um int not null);
if exists(select table_name from information_schema.tables
where table_name = 'accountbalance')
drop table accountbalance
gocreate table accountbalance(
ffundcode varchar(6) not null,
fac**um int not null,
fdate datetime default (getdate()) not null,
fbal numeric(10,2) not null);
insert into accountmessage values('000001','北京存款',1)
insert into accountmessage values('000001','上海存款',2)
insert into accountmessage values('000001','深圳存款',3)
insert into accountmessage values('000002','北京存款',1)
insert into accountmessage values('000002','上海存款',2)
insert into accountmessage values('000002','天津存款',3)
insert into accountmessage values('000003','上海存款',1)
insert into accountmessage values('000003','福州存款',2)
insert into accountbalance(fdate, ffundcode, fac**um, fbal) values ('2004-07-28','000001',1,1000.00)
insert into accountbalance(fdate, ffundcode, fac**um, fbal) values ('2004-07-28','000001',2,1000.00)
insert into accountbalance(fdate, ffundcode, fac**um, fbal) values ('2004-07-28','000001',3,1120.00)
insert into accountbalance(fdate, ffundcode, fac**um, fbal) values ('2004-07-28','000002',1,2000.00)
insert into accountbalance(fdate, ffundcode, fac**um, fbal) values ('2004-07-28','000002',2,1000.00)
insert into accountbalance(fdate, ffundcode, fac**um, fbal) values ('2004-07-28','000002',3,1000.00)
insert into accountbalance(fdate, ffundcode, fac**um, fbal) values ('2004-07-28','000003',1,2000.00)
insert into accountbalance(fdate, ffundcode, fac**um, fbal) values ('2004-07-28','000003',2,1000.00)
go兩種不同的方法
declare @s nvarchar(4000)
set @s=''
select @s=@s+','+quotename(fac**ame)
+'=isnull(sum(case a.fac**ame when '+quotename(fac**ame,'''')
+' then b.fbal end),0)'
from accountmessage group by fac**ame
exec('
select ****=a.ffundcode'+@s+'
from accountmessage a,accountbalance b
where a.ffundcode=b.ffundcode and a.fac**um=b.fac**um
group by a.ffundcode')
goselect * into #t from(select a.*,b.fbal from accountmessage a join accountbalance b on a.ffundcode=b.ffundcode and a.fac**um=b.fac**um)t
declare @sql varchar(8000)
set @sql='select ffundcode'
select @sql= @sql+ ',sum(case when fac**ame = '''
+ tt + ''' then fbal else 0 end) [' +tt+ ']'
from (select distinct fac**ame as tt from #t) a
set @sql=@sql+' from #t group by ffundcode'
exec (@sql)
T SQL基礎及T SQL分頁查詢
資料定義語言 ddl,data definition language 資料操縱語言 dml,data manipulation language 資料控制語言 dcl,data control language 核心作用主要用作用快速的條件查詢 運算子作用 判斷相等 大於 小於 大於等於 小於等於...
Elasticsearch API聚合查詢 筆記
專案需求 需要過濾後聚合某些沒有分詞的字段 注意 因為專案欄位都是string 所以這裡都是terms aggregation 基本字段查詢 query string針對的是沒有分詞的字段,time對應是每秒的時間戳 data histogram time 間隔時間聚合查詢 這裡是針對查詢出來的結果...
python es 聚合查詢
預設是 order 數量查詢 可不填 但是我們可以進行自定義,自定義時,key就是和子aggs的key保持一致 size 10,要統計的前幾名,這個是必須要有的,不然如果有多個分片就會出現資料不對等的情況,導致資料偏差 min doc count 10,該表示統計的字段數如果layers.ip.ip...