Answer the question
In order to leave comments, you need to log in
How to make SQL columns from rows?
I use SQL Server 2010.
For such a table,
create a query, the result of which will be something like this,
i.e. for each row with a pass number (Permit_Number), the time value in the rows marked enter will become the corresponding column. The same for exit.
Answer the question
In order to leave comments, you need to log in
I don’t know the subtleties of MS SQL, but the general essence is this:
SELECT t1.Permit_Number, t1.Pass_Time as Enter, t2.Pass_Time as Exit FROM table as t1
LEFT JOIN table as t2 ON (
t1.Permit_Number=t2.Permit_Number AND t2.Permit_Kind='exit'
)
WHERE Pass_Kind='enter'
SELECT t1.Permit_Number, t1.Pass_Time as Enter,
(
SELECT MIN(t2.Pass_Time)
FROM table as t2
WHERE t2.Pass_Kind='exit' AND t2.Pass_Time>t1.Pass_Time
AND t1.Permit_Number=t2.Permit_Number
) AS Exit
FROM table as t1
WHERE Pass_Kind='enter'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question