我有一张桌子参观。 我想按工资类型和性别显示访问次数
select jenis_bayar, (select count(id)
from vw_kunjungan
where waktu_kunjungan>='2015-02-12 00:00:00'AND waktu_kunjungan<='2015-02-12 23:59:00'
and gender=1
and puskesmas_id=1
group by jenis_bayar) as total_lk,
(select count(id)
from vw_kunjungan
where waktu_kunjungan>='2015-02-12 00:00:00'
AND waktu_kunjungan<='2015-02-12 23:59:00'
and gender=0
and puskesmas_id=1 group by jenis_bayar) as total_pr,
(select count(id)
from loket
where waktu_kunjungan>='2015-02-12 00:00:00'AND waktu_kunjungan<='2015-02-12 23:59:00'
and puskesmas_id=1 group by jenis_bayar) as total
from loket group by jenis_bayar ;
我希望得到这样的结果:
| type_of_pay | man | woman | total | +-------------+----------+----------+-------+ | askes | 12 | 10 | 22 | | bayar | 53 | 87 | 140 | | bpjs_pbi | 25 | 15 | 40 | | gratis | 10 | 10 | 20 | | jamkesda | 11 | 10 | 21 | | kader | 35 | 10 | 45 | | non_pbi | 60 | 50 | 110 | +-------------+----------+----------+-------+
表结构
+-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | loket_id | int(11) | NO | | 0 | | | no_registrasi | varchar(45) | NO | | NULL | | | nama | varchar(25) | NO | | NULL | | | nama_kk | varchar(25) | YES | | NULL | | | gender | tinyint(1) | NO | | NULL | | | tgl_lahir | date | NO | | NULL | | | waktu_kunjungan | datetime | NO | | NULL | | | jenis_kunjungan | tinyint(1) | YES | | NULL | | | puskesmas_id | int(11) | NO | | NULL | | | jenis_bayar | varchar(45) | NO | | NULL | | | poli_id | int(11) | NO | | NULL | | | _year | bigint(21) | YES | | NULL | | | _month | bigint(21) | YES | | NULL | | | _day | bigint(17) | YES | | NULL | | | kelurahan_id | int(11) | NO | | NULL | | +-----------------+-------------+------+-----+---------+-------+
答案 0 :(得分:0)
没有真正难以理解您的列名:
select jenis_bayer type_of_pay,
sum(case when gender=1 then 1 else 0 end) man,
sum(case when gender=1 then 0 else 1 end) woman,
count(id) total
from vw_kunjungan
where waktu_kunjungan>='2015-02-12 00:00:00'AND waktu_kunjungan<='2015-02-12 23:59:00'
group by jenis_bayer