How do I create N cant of columns in an sql query? in postgresql

0

In the current query I have static to 2 columns, but I would like to create columns for each shift that is in another table.

Here the 2 fiddle of what I have:

sqlfiddle and dbfiddle

Update 1 (trying to specify a little more):

The sql that I show in the examples generates a list of dates in which it consults in the table "lots" how much "number of workers" are assigned for each date, the problem is that I can not find, with sql , generate N number of columns for the turns. As you can see there is "turn1" and "turn2" with the condition of "id_turnos = 1" and "id_turnos = 2" but I can not make them dynamic, that I generate so many columns for as many turns as there are. As you can see there is a date registered with the "id_turnos = 3".

NOTE: What do they give to close if they do not ask first or request more specification of the question if it is not understood?

Update 2 (image attached):

Current table:

Table to which you want to arrive:

Update 3:

This was my previous question and with which we arrived at the sql that I currently have.

link

    
asked by Pablo Contreras 18.03.2017 в 18:39
source

1 answer

-3

It is not clear why you need them to be in columns, remember that SQL is a language that allows you to obtain information, say "raw", the way information is presented is not part of it. The information you need can be obtained with the following query

Select fecha,id_turnos, sum(cant_operarios) turnosAsignados from lotes group by fecha,id_turnos order by fecha,id_turnos
    
answered by 18.03.2017 в 22:35