Query SQL use count + distinct unique values

0

I have this query:

 SELECT count(*) as total, * FROM ( select case when pd_edad <=0 then '01'
when pd_edad >=65 then '15' 
END as age_range,pd_sexo,pd_diagnostico from paciente
where PD_CFEC between '2017/01/12' and '2017/31/12') t
group by PD_SEXO,age_range,pd_diagnostico
order by PD_SEXO desc,age_range asc, pd_diagnostico asc

Take for example, a model here to build the query,

I need to count unique values of the DNI field found in this table, but I only want to see only these fields by age range , I hope your help, thanks.

    
asked by jjss86 30.01.2018 в 15:46
source

4 answers

0

You must include the%% change% of the Count () , next to the attribute of the subquery for which you want to ensure uniqueness of the count.

The query would be like this:

SELECT Count(DISTINCT DNI) as total, PD_SEXO, age_range, pd_diagnostico
FROM ( 
Select DNI, 
CASE 
WHEN pd_edad >=0 and pd_edad <= 20 then '01'
WHEN pd_edad >=21 and pd_edad <= 32 then '02'
WHEN pd_edad >=33 and pd_edad <= 42 then '03'
WHEN pd_edad >=65 and pd_edad <= 100 then '15' 
   --O Tantos rangos como quieras
ELSE
pd_edad --Para los que no estan en los rangos
END as age_range,
pd_sexo,pd_diagnostico 
FROM paciente
WHERE PD_CFEC between '2017/01/12' and '2017/31/12') t
GROUP BY PD_SEXO,age_range,pd_diagnostico
ORDER BY PD_SEXO desc,age_range asc, pd_diagnostico asc

Pd: This assuming that your database engine is SQL-Server.

    
answered by 01.02.2018 / 14:02
source
0

Probably add it within the count (distinct + CampoAver).

select count (distinct DNI) from zzz

You also have something like this:

SELECT COUNT (*)FROM TABLA1 (SELECT DISTINCT DNI FROM TABLA);
GROUP BY ALGO
    
answered by 30.01.2018 в 15:58
0

Sorry, it's not clear to me, yet. Let's see if I can help you with this (below SQL query)

select top 1(
    select COUNT(1)
    from adpadir where PD_EDAD =16
    ) AS igual16,
    (select count (1) as n
    from adpadir where PD_EDAD <=22
      ) AS igual22
 FROM adpadir
    
answered by 30.01.2018 в 20:19
0

I send the final query:

SELECT Count(DISTINCT pd_hcli) as total, pd_sexo, age_range, pd_diag
FROM ( 
Select pd_hcli, 
CASE 
WHEN pd_edad <=0  then '01'
WHEN pd_edad between 1 and 4   then '02'
WHEN pd_edad between 5 and 9   then '03'
WHEN pd_edad between 10 and 14 then '04'
WHEN pd_edad between 15 and 19 then '05'
WHEN pd_edad between 20 and 24 then '06'
WHEN pd_edad between 25 and 29 then '07'
WHEN pd_edad between 30 and 34 then '08'
WHEN pd_edad between 35 and 39 then '09'
WHEN pd_edad between 40 and 44 then '10'
WHEN pd_edad between 45 and 49 then '11'
WHEN pd_edad between 50 and 54 then '12'
WHEN pd_edad between 55 and 59 then '13'
WHEN pd_edad between 60 and 64 then '14'
WHEN pd_edad >=65 then '15' 
ELSE pd_edad END as age_range,
pd_sexo,pd_diag 
FROM adpadir
WHERE PD_CFEC between '2017/01/12' and '2017/31/12') t
GROUP BY PD_SEXO,age_range,PD_DIAG
ORDER BY PD_SEXO desc,age_range asc, PD_DIAG asc

The result shows, total of unique patients (pd_hcli) found by each diagnosis (pd_diag), in turn ordered by sex (pd_sexo) and age range (pd_edad), thank you for your community support.

    
answered by 02.02.2018 в 19:37