Question

Considérez le tableau suivant et les lignes:

Liste A.

ID, name, event, type
1, 'John Doe', '2010-09-01 15:00:00.000', 'input'
1, 'John Doe', '2010-09-03 11:00:00.000', 'input'
1, 'John Doe', '2010-09-04 17:00:00.000', 'input'
1, 'John Doe', '2010-09-02 15:00:00.000', 'output'
1, 'John Doe', '2010-09-03 16:00:00.000', 'output'
1, 'John Doe', '2010-09-06 17:00:00.000', 'output'

Ce que je veux est de convertir les lignes en colonnes, donc je peux avoir deux colonnes différentes, événement d'entrée et de sortie événement. Comme:

Liste B.

ID, name, input event, output event
1, 'John Doe', '2010-09-01 15:00:00.000', '2010-09-02 15:00:00.000'
1, 'John Doe', '2010-09-03 11:00:00.000', '2010-09-03 16:00:00.000'
1, 'John Doe', '2010-09-04 17:00:00.000', '2010-09-06 17:00:00.000'

J'ai été en mesure d'obtenir quelque chose comme suit:

Liste C.

ID, name, input event, output event
1, 'John Doe', '2010-09-01 15:00:00.000', null
1, 'John Doe', '2010-09-03 11:00:00.000', null
1, 'John Doe', '2010-09-04 17:00:00.000', null
1, 'John Doe', null, '2010-09-02 15:00:00.000'
1, 'John Doe', null, '2010-09-03 16:00:00.000'
1, 'John Doe', null, '2010-09-06 17:00:00.000'

, mais le problème est de savoir comment plat les lignes, puisque le double tuples ID nom sont pertinents. Pour convertir des lignes dans les colonnes I quelque chose comme code ceci:

select ID, name, max(case when type = 'input' then event else null end) as 'input event', max(case when type = 'output' then event else null end) as 'output event' from events group by ID, name

, mais bien sûr, le GROUP BY va quitter les doublons, et c'est ce que je ne veux pas.

Toutes les idées comment y parvenir avec une requête?

Il serait agréable d'avoir une solution sql portable ou pour postgresql, mais toute idée est très apprécié.

EDIT: désolé pour réponse tardive. Les deux solutions de AlexRednic et Mark Bannister accomplir ce que je voulais. J'ai finalement opté pour le second, car il semble plus clair pour moi. Merci à tous pour vos réponses!

Était-ce utile?

La solution

Essayez ce qui suit:

select ID, name, event as 'input event', 
       (select min(o.event) 
        from events o 
        where o.type = 'output' and 
              i.ID = o.ID and 
              i.name = o.name and 
              i.event < o.event) as 'output event' 
from events i
where i.type = 'input'
group by ID, name, event

Autres conseils

select t1.id,t1.name,t1.event,t2.event from test t1
    inner join test t2 on t1.event <= t2.event 
        and t1.type = 'input' 
        and t2.type = 'output'
                   and t1.id = t2.id
                   and t1.name = t2.name

La chose est que vous devez en quelque sorte de relier les sessions d'entrée / sortie. Dans cette requête, je l'ai fait en utilisant la colonne d'événement d'horodatage. Pourriez-vous donner plus d'informations si ce n'est pas ce que vous vouliez?

Mise à jour: maintenant, à vous mordit post-processus pourrait faire

with a as
(
select t1.id,t1.name,t1.event as in_event,t2.event as out_event from test t1
    inner join test t2 on t1.event <= t2.event 
                   and t1.type = 'input' 
                   and t2.type = 'output'
                   and t1.id = t2.id
                   and t1.name = t2.name
)
select id,name,in_event,min(out_event)
       from a
group by id,name,in_event

Je vous écris depuis le début:

create table #a(
ID int,
name varchar(30),
event datetime,
type varchar(10)
)

insert #a
select  
1, 'John Doe', '2010-09-01 15:00:00.000', 'input'
union select 1, 'John Doe', '2010-09-01 16:00:00.000', 'input'
union select 1, 'John Doe', '2010-09-01 17:00:00.000', 'input'
union select 1, 'John Doe', '2010-09-02 15:00:00.000', 'output'
union select 1, 'John Doe', '2010-09-02 16:00:00.000', 'output'
union select 1, 'John Doe', '2010-09-02 17:00:00.000', 'output'
  

- voici la solution sql

select 
    ID, 
    name, 
    case when type = 'input' then event else null end "input event",
    case when type = 'output' then event else null end "output event"
 from #a
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top