5
I need to select in my database all printers compatible with a selected supply, for example, the code supply 155 who is called 50FOZ00. I do the query
below to select all compatible printers:
SELECT suprimento
,modelo
FROM public.suprimento
,public.modelosuprimento
,public.impressoramodelo
WHERE public.suprimento.codigo = public.modelosuprimento.codigosuprimento
AND public.modelosuprimento.codigomodelo = public.impressoramodelo.codigo
AND public.suprimento.codigo = 155
And my return is coming correctly, being shown me that:
Is it possible that this return is displayed differently? Where in the first column is the supply and for each compatible printer model a new column is created? Getting something similar to this:
SUPRIMENTO | MODELO | MODELO | MODELO | MODELO | MODELO | MODELO | MODELO | MODELO | MODELO | MODELO |
50FOZ00 | MS310 | MS312 | MS315 | MS410 | MS415 | MS610 | MX310 | MX410 | MX511 | MX611 |
there’s a guy named PIVOT
– Marco Souza
You could give an example @Marconciliosouza??
– R.Santos
With PIVOT, would have to pass the
query
the codes of the fixed supply, can be R.Santos?– David
It may be, because in the future I will need a parameter (code) to select only a certain supply
– R.Santos
You would have some example to give me @David?
– R.Santos
@David, @Marconciliosouza
group by
to display all printers by supply? I tried so:select suprimento, modelo from public.suprimento, public.modelosuprimento, public.impressoramodelo where public.suprimento.codigo = public.modelosuprimento.codigosuprimento
and public.modelosuprimento.codigomodelo = public.impressoramodelo.codigo and public.suprimento.codigo = 155 group by public.suprimento.suprimento
but failed to execute– R.Santos
@Marconciliosouza I was researching now the subject you suggested "PIVOT" but this way I do not have to determine the columns dynamically
– R.Santos
@Renan I could not understand the example you suggested as a possible duplicate, and the author of the question also did not select any of the answers as correct for it and for this reason I created my
– R.Santos
There is a way to do this dynamically, creating a function that returns a table (I don’t know if Postgresql has this feature), and within the function does the
query
that returns the supply, which then stops for the pivot this data, but gives a good job, until today I did not have time to do it, using oracle, but it is the tip ;)– David