1
Yesterday I posted a question regarding how to return something even if there are no records found, and with the help of @Rovannlinhalis we managed to get the following query
:
with dados as (
SELECT
public.controleimpressoes.codigo,
public.impressoras.patrimonio,
public.impressoramodelo.modelo,
public.impressoralocal.descricao,
data,
diferecacontador,
diferecacontadordigital,
diferencacontadorcolor,
public.empresa.nome,
datainicial,
datafinal
FROM
public.controleimpressoes, public.impressoras, public.impressoramodelo,
public.impressoralocal, public.empresa
where
public.controleimpressoes.codigoempresa = public.empresa.codigo and
public.controleimpressoes.codigoimpressora = public.impressoras.codigo and
public.impressoras.codigomodelo = public.impressoramodelo.codigo and
public.controleimpressoes.codigoimpressoralocal = public.impressoralocal.codigo and
data between '2017-01-01' and '2017-12-31' and
public.impressoras.patrimonio between '1000' and '1882' and
public.empresa.nome between 'A' and 'B' and
public.impressoramodelo.modelo between 'C748' and 'C748'
), meses as (
Select distinct
intervalo,
d.patrimonio,
d.modelo,
d.descricao,
d.nome
FROM generate_series('2017-01-01'::timestamp, '2017-12-31'::timestamp, '1 Month') as intervalo
CROSS JOIN dados d)
SELECT DISTINCT
to_char(intervalo , 'TMMon') dataMes,
m.*,
(case when d.diferecacontador is null then 0 else d.diferecacontador end),
d.diferecacontadordigital,
d.diferencacontadorcolor,
d.datainicial,
d.datafinal
FROM meses m
LEFT OUTER JOIN DADOS d on date_trunc('month',d.data) = m.intervalo and m.patrimonio = d.patrimonio
order by m.nome, m.patrimonio, intervalo
That returned me the data I needed.
But since I’m using this select to generate a report in Jasperstudio, my need would be to turn that feedback to something similar to this:
Pat. | Mod | Setor Inst. | Empresa | M1 |C1 | M2 |C2 | M3 |C3 | M4 |C4 | M5 |C5 | M6 |C6 | M7 |C7 | M8 |C8 | M9 |C9 | M10 |C10| M11 |C11| M12 |C12
1854 | C748 | 4º andar - 401 | A | Jan | 0 | Fev | 0 | Mar | 0 | Abr | 0 | Mai | 2409 | Jun | 551 | Jul | 39 | Ago | 0 | Set | 0 | Out | 0 | Nov | 0 | Dez | 0
select * from crosstab (
'with dados as (
SELECT
public.impressoras.patrimonio,
public.impressoramodelo.modelo,
public.impressoralocal.descricao,
data,
diferecacontador,
diferecacontadordigital,
diferencacontadorcolor,
public.empresa.nome,
datainicial,
datafinal
FROM
public.controleimpressoes, public.impressoras, public.impressoramodelo,
public.impressoralocal, public.empresa
where
public.controleimpressoes.codigoempresa = public.empresa.codigo and
public.controleimpressoes.codigoimpressora = public.impressoras.codigo and
public.impressoras.codigomodelo = public.impressoramodelo.codigo and
public.controleimpressoes.codigoimpressoralocal = public.impressoralocal.codigo and
data between \'2017-01-01\' and \'2017-12-31\' and
public.impressoras.patrimonio between \'1000\' and \'1882\' and
public.empresa.nome between \'A\' and \'B\' and
public.impressoramodelo.modelo between \'C748\' and \'C748\'
), meses as (
Select distinct
intervalo,
d.patrimonio,
d.modelo,
d.descricao,
d.nome
FROM generate_series(\'2017-01-01\'::timestamp, \'2017-12-31\'::timestamp, \'1 Month\') as intervalo
CROSS JOIN dados d)
SELECT DISTINCT
to_char(intervalo , \'TMMon\') dataMes,
m.*,
(case when d.diferecacontador is null then 0 else d.diferecacontador end),
d.diferecacontadordigital,
d.diferencacontadorcolor,
d.datainicial,
d.datafinal
FROM meses m
LEFT OUTER JOIN DADOS d on date_trunc(\'month\',d.data) = m.intervalo and m.patrimonio = d.patrimonio
order by m.nome, m.patrimonio, intervalo') as final_result (Pat text, Modelo text, Descricao text, datas timestamp, diferecacontador numeric, diferecacontadordigital numeric, diferencacontadorcolor numeric, empresa text, datainicial timestamp, datafinal timestamp)
You want to turn the result into pivot table?
– Laerte
knowing the amount of columns until you can do rsrs complicates when the quantity is dynamic
– Rovann Linhalis
@Rovannlinhalis The number of columns would always be the same at first
– R.Santos
as @Laerte said, you need the pivot table, take a look there, when I release here...if I have not managed to help you try to help you
– Rovann Linhalis
@Rovannlinhalis thanks, I will search for pivot table and see if I can implement in my need
– R.Santos
I think it is possible to even do this in the report tool... look for cross tab in it too
– Rovann Linhalis
@Laerte thanks for the tip, I will search how to use the pivot table :)
– R.Santos
@Laerte updated my question as the way I tried to accomplish the
pivot table
but there was a mistake, you know tell me what would be wrong?– R.Santos
you have to make Crosstab in the whole select... not in the first just...rs
– Rovann Linhalis
Let’s go continue this discussion in chat.
– R.Santos
@R.Santos unfortunately I can’t help you now, but Rovann I believe I can help you.
– Laerte