I have the following (simplified) SQL Query:
select
sum(case when gender=1 then 1 else 0 end) as Male,
sum(case when gender =2 then 1 else 0 end) as Female from
( select distinct pp.person_id, gender
from PERSON_VISITS p, PERSON pp
where p.visit_date between &p_start_date and &p_end_date
and p.person_id=pp.person_id)
Basically I have the table PERSON_VISITS which consists of a VISIT_DATE Column, a PERSON_ID column and some other columns (which are not needed in this context).
Then I got the PERSON table, which stores the Customer Data like name, gender and so on.
So I want to generate a list of how many of our visitors where male and female.
A Customer can visit us many times , so there can be multiple entries for one person_id in PERSON_VISITS.
The above is a very simplified version of my actual query so I wonder if there is a “better” or simpler way in achieving this ?