เราจะใส่เงื่อนไข ในการนับได้มั้ยครับ (ใน case when)
Code (PHP)
SELECT DISTINCT f43_person.HOSPCODE as รหัส
,count(DISTINCT f43_person.CID case when f43_specialpp.PPSPECIAL LIKE '1B20%' then f43_specialpp.CID else null end ) as การเคลื่อนไหว
,count(DISTINCT f43_person.CID case when f43_specialpp.PPSPECIAL LIKE '1B21%' then 1 else null end) as กล้ามเนื้อและสติปัญญษ
,count(DISTINCT f43_person.CID case when f43_specialpp.PPSPECIAL LIKE '1B22%' or f43_specialpp.PPSPECIAL LIKE '1B23%' then 1 else null end) as เข้าใจภาษา
,count(DISTINCT f43_person.CID case when f43_specialpp.PPSPECIAL LIKE '1B24%' then 1 else null end) as ช่วยเหลือตัวเองและสังคม
FROM f43_person
INNER JOIN f43_specialpp on f43_specialpp.CID = f43_person.CID and f43_person.TYPEAREA in (1,3)
WHERE f43_specialpp.DATE_SERV >= '20151001'
and TIMESTAMPDIFF(MONTH,f43_person.BIRTH,f43_specialpp.DATE_SERV) <= 12
group BY f43_person.HOSPCODE
SELECT
HOSPCODE as รหัส
,count(case when PPSPECIAL LIKE '1B20%' then 1 else null end ) as การเคลื่อนไหว
,count(case when PPSPECIAL LIKE '1B21%' then 1 else null end) as กล้ามเนื้อและสติปัญญา
,count(case when PPSPECIAL LIKE '1B22%' or PPSPECIAL LIKE '1B23%' then 1 else null end) as เข้าใจภาษา
,count(case when PPSPECIAL LIKE '1B24%' then 1 else null end) as ช่วยเหลือตัวเองและสังคม
from (
select p.HOSPCODE, s.PPSPECIAL
FROM f43_person p
INNER JOIN f43_specialpp s
on s.CID = p.CID
and TIMESTAMPDIFF(MONTH,p.BIRTH,s.DATE_SERV) <= 12
WHERE s.DATE_SERV >= '20151001'
and p.TYPEAREA in (1,3)
group by p.HOSPCODE, s.PPSPECIAL
) AS TMP1
group BY HOSPCODE
select hoscode, count(cid) from (
select hoscode , cid from
FROM f43_person p
where exists( select cid from f43_specialpp where cid = p.cid and DATE_SERV >= '20151001')
group by hoscode, cid
) tmp group by hoscode
SELECT table_person.home
,count(case when table_service.product_id = '1' then 1 else null end ) as p1
,count(case when table_service.product_id = '2' then 1 else null end) as p2
,count(case when table_service.product_id = '3' then 1 else null end) as p3
,count(case when table_service.product_id = '4' then 1 else null end) as p4
FROM table_person
group BY table_person.home
SELECT p.home
,count(case when t.product_id = '1' then 1 else null end) as p1
,count(case when t.product_id = '2' then 1 else null end) as p2
,count(case when t.product_id = '3' then 1 else null end) as p3
,count(case when t.product_id = '4' then 1 else null end) as p4
FROM table_person p
inner join (
คิวรี่จาก 1.2
) as t on p.cus_id=t.cus_id
group BY p.home