correct use of having by [duplicate]

0

Hi, I wanted to know if I'm doing the right thing with the following tables

NUMOBJ | IDIOMA 
1      | E
1      | I
2      | E
2      | I
3      | E
3      | I

table1: each object with all possible languages (2)

NUMOBJ | IDIOMA 
1      | E
1      | I
2      | I
3      | E

table2: each object with the languages it actually has

create view V as

select numobj, 
case 
when count(*) < 2 then 0
else 1
end as Tag
from Tabla1 as T1
left join Tabla2 as T2
on T1.numobj = T2.numobj
where T1.idioma = T2.idioma
GROUP  BY T1.numobj, T2.numobj

This query adds a column Tag that will be 1 if the object of table 2 has 2 idiomas and will be 0 if it only has one, like this:

NUMOBJ | TAG
1      | 1
2      | 0
3      | 0

Now what I want to do is have another column that tells me what language the object is missing if it is 0, like this:

NUMOBJ | TAG | FALTA(N)
1      | 1   | 
2      | 0   | E
3      | 0   | I

Does anyone know if this can be done? I have tried something but with the group by it does not let me use the columns that are not grouped ( IDIOMAS ). I appreciate any help or hint, thank you very much.

R.

    
asked by Rob 12.08.2018 в 19:42
source

1 answer

0

Hello, I hope it serves you ... greetings ....

mysql

select numobj, tag, faltan from (select t1.*,t2.idioma, if(t1.tag=1,'',if(t2.idioma='E','I','E')) as faltan from (select T1.numobj, case 
when count(*) < 2 then 0
else 1
end as Tag
from Tabla1 T1 left join Tabla2 T2 on T1.numobj = T2.numobj
where T1.idioma = T2.idioma
GROUP  BY T1.numobj, T2.numobj ) as t1  left join Tabla2 t2 on t1.numobj = t2.numobj GROUP  BY t1.numobj) as tabla
    
answered by 13.08.2018 в 04:17