汇总按月分组的多列

时间:2016-07-04 04:03:06

标签: sql sql-server-2008

好的,让我先展示一下我的桌子。

我的专栏:

Outlet | tanggal | cash | cc | dc | flash | piutang | reject | disc50

我所做的是总和per day。我的询问到目前为止

select 
    Outlet, tanggal,
    (cash + cc + dc + flash + piutang + reject + disc50) total
from 
    transaksi 
where 
    outlet = 'K-AEON'

此查询返回此结果:

Outlet  tanggal      total
---------------------------
K-AEON  2016-01-22  2343300
K-AEON  2016-01-23  6091200
K-AEON  2016-01-24  6244700
K-AEON  2016-01-26  3284100
K-AEON  2016-01-25  1904600
K-AEON  2016-01-27  2743500

我想要的是上面的结果是group bymonth(tanggal)

到目前为止,我已尝试过这个

select 
    Outlet, month(tanggal),
    (cash + cc + dc + flash + piutang + reject + disc50) total 
from  
    transaksi 
where 
    outlet = 'K-AEON' 
group by 
    month(tanggal), Outlet, cash, cc, dc, flash, piutang, reject, disc50

和结果

Outlet  month   total
----------------------
K-AEON    1    811100
K-AEON    1    1220850
K-AEON    1    1169700
K-AEON    1    1147500
K-AEON    1    1290000

我想要的结果是

Outlet  month   total
----------------------------------------
K-AEON    1    10000000   // all of January
K-AEON    2    20000000   // All feb
K-AEON    3    30000000   // All march
K-AEON    4    40000000   // All April
K-AEON    5    50000000   // All may

有任何帮助吗?抱歉我的英文不好

1 个答案:

答案 0 :(得分:3)

您离我很近,但您需要使用private function rp_reach_rate_get_data() { $this->load->model('db_model'); $result = $this->db_model->rp_reach_rate_stmt(); return $result; } public function rp_reach_rate() { $this->load->model('db_model'); $data['sqlstmt'] = $this->rp_reach_rate_get_data(); $data['camplist'] = $this->vici_campaigns_get_data(); $data['lists'] = $this->vici_lists_get_data(); $data['shifts'] = $this->vici_shifts_get_data(); $this->load->view('header',$data); $this->load->view('RP_ReachRate',$data); } ,然后从sum中排除这些字段:

group by