Skip to content

Instantly share code, notes, and snippets.

@przbadu
Last active July 12, 2017 07:58
Show Gist options
  • Select an option

  • Save przbadu/cecc67e725f00c2f63866579fb6ea76c to your computer and use it in GitHub Desktop.

Select an option

Save przbadu/cecc67e725f00c2f63866579fb6ea76c to your computer and use it in GitHub Desktop.
Merge different sql into one big giant sql query to improve performance
select * from (
-- Male patients age between(0-9)
(select count(distinct patients.id) as total_male_patients_0_9
from patients
INNER JOIN patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.age BETWEEN 0 AND 9) as t1,
-- Female patients age between(0-9)
(select count(distinct patients.id) as total_female_patients_0_9
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.age BETWEEN 0 AND 9) as t2,
-- Male patients age between(10-19)
(select count(distinct patients.id) as total_male_patients_10_19
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.age BETWEEN 10 AND 19) as t3,
-- Female patients age between(10-19)
(select count(distinct patients.id) as total_female_patients_10_19
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.age BETWEEN 10 AND 19) as t4,
-- Male patients age between(20-59)
(select count(distinct patients.id) as total_male_patients_20_59
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.age BETWEEN 20 AND 59) as t5,
-- Female patients age between(20-59)
(select count(distinct patients.id) as total_female_patients_20_59
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.age BETWEEN 20 AND 59) as t6,
-- Male patients age ≥ 60
(select count(distinct patients.id) as total_male_patients_gte_60
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.age >= 60) as t7,
-- Female patients age ≥ 60
(select count(distinct patients.id) as total_female_patients_gte_60
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.age >= 60) as t8,
-- Total Male Patients
(select count(distinct patients.id) as total_male_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male") as t9,
-- Total female patients
(select count(distinct patients.id) as total_female_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female") as t10,
-- ---------------------------------
-- NEW PATIENTS
-- condition:
-- where patient_histories.count > 1
-- ---------------------------------
-- Total New Male Patients age between (0-9)
(select count(*) as total_new_male_patients_0_9 from (
select count(distinct patients.id) as total_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.age BETWEEN 0 AND 9
group by patients.id
having count(patient_histories.patient_id) > 1
) t) as t11,
-- Total New Female Patients age between (0-9)
(select count(*) as total_new_female_patients_0_9 from (
select count(distinct patients.id) as total_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.age BETWEEN 0 AND 9
group by patients.id
having count(patient_histories.patient_id) > 1
) t) as t12,
-- Total New Male Patients age between (10-19)
(select count(*) as total_new_male_patients_10_19 from (
select count(distinct patients.id) as total_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.age BETWEEN 10 AND 19
group by patients.id
having count(patient_histories.patient_id) > 1
) t) as t13,
-- Total New Female Patients age between (10-19)
(select count(*) as total_new_female_patients_10_19 from (
select count(distinct patients.id) as total_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.age BETWEEN 10 AND 19
group by patients.id
having count(patient_histories.patient_id) > 1
) t) as t14,
-- Total New Male Patients age between (20-59)
(select count(*) as total_new_male_patients_20_59 from (
select count(distinct patients.id) as total_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.age BETWEEN 20 AND 59
group by patients.id
having count(patient_histories.patient_id) > 1
) t) as t15,
-- Total New Female Patients age between (20-59)
(select count(*) as total_new_female_patients_20_59 from (
select count(distinct patients.id) as total_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.age BETWEEN 20 AND 59
group by patients.id
having count(patient_histories.patient_id) > 1
) t) as t16,
-- Total New Male Patients age >= 60
(select count(*) as total_new_male_patients_gte_60 from (
select count(distinct patients.id) as total_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.age >= 60
group by patients.id
having count(patient_histories.patient_id) > 1
) t) as t17,
-- Total New Female Patients age >= 60
(select count(*) as total_new_female_patients_gte_60 from (
select count(distinct patients.id) as total_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.age >= 60
group by patients.id
having count(patient_histories.patient_id) > 1
) t) as t18,
-- Total New Male Patients
(select count(*) as total_new_male_patients from (
select count(distinct patients.id) as total_patients
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
group by patients.id
having count(patient_histories.patient_id) > 1
) t) as t19,
-- Total New female patients
(select count(*) as total_new_female_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
group by patients.id
having count(patient_histories.patient_id) > 1
)t) as t20,
-- Total dalit male patients
(select count(*) as total_dalit_male_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.race="dalit"
group by patients.id
) t ) as t21,
-- Total dalit female patients
(select count(*) as total_dalit_female_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.race="dalit"
group by patients.id
) t ) as t22,
-- Total janjati male patients
(select count(*) as total_janjati_male_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.race="janjati"
group by patients.id
) t ) as t23,
-- Total janjati female patients
(select count(*) as total_janjati_female_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.race="janjati"
group by patients.id
) t ) as t24,
-- Total madhesi male patients
(select count(*) as total_madhesi_male_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.race="madhesi"
group by patients.id
) t ) as t25,
-- Total madhesi female patients
(select count(*) as total_madhesi_female_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.race="madhesi"
group by patients.id
) t ) as t26,
-- Total muslim male patients
(select count(*) as total_muslim_male_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.race="muslim"
group by patients.id
) t ) as t27,
-- Total muslim female patients
(select count(*) as total_muslim_female_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.race="muslim"
group by patients.id
) t ) as t28,
-- Total brahman male patients
(select count(*) as total_brahman_male_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.race="brahman"
group by patients.id
) t ) as t29,
-- Total brahman female patients
(select count(*) as total_brahman_female_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.race="brahman"
group by patients.id
) t ) as t30,
-- Total others male patients
(select count(*) as total_others_male_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
AND patients.race="others"
group by patients.id
) t ) as t31,
-- Total others female patients
(select count(*) as total_others_female_patients from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
AND patients.race="others"
group by patients.id
) t ) as t32,
-- Total male patients in terms of race
(select count(*) as total_male_patients_in_terms_of_race from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="male"
group by patients.id
) t ) as t33,
-- Total female patients in terms of race
(select count(*) as total_female_patients_in_terms_of_race from (
select count(distinct patients.id)
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patient_histories.day=1
AND patient_histories.month=1
AND patient_histories.year=2074
AND patients.gender="female"
group by patients.id
) t ) as t34
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment