I am trying to pull three items from a table that have no relation to each other (other than the fact that they are sequential) and get them on one row.
So the table might look like this:
time show
12PM smallville
1PM House
2PM walking dead
I know how to get three items based on time but I need the answer to look like this:
col1 col2 col3
smallvile House walking dead
Ev开发者_开发技巧erything else I have seen is based on some sort of association to each other using multiple columns in the select, I have no such association I just want the three things I select to show up in one row of three columns.
If this question is in relation to SLQ Server and not Excel, then something like the following would be what you're looking for:
Select [12PM],[1PM],[2PM],...
from (
select time, show from tablename
) as sourcetable
PIVOT (
Max(Show) for Time in [12PM],[1PM],[2PM],...
) as PivotTable
See MSDN for further reference on Pivot Tables.
Using dynamic pivot mysql tables (transform rows to columns).
Just put an identity to manipulate the pivot, below code is my sample. Hope it helps.
CREATE TABLE #Temp (ID INT IDENTITY NOT NULL, [Time] VARCHAR(20), Show VARCHAR(100))
INSERT INTO #Temp ( [Time] , Show )
SELECT '12PM', 'smallville' UNION SELECT '1PM', 'House' UNION SELECT '2PM', 'walking dead'
SELECT [1], [2], [3]
FROM (
SELECT Show, ID FROM #Temp
) As src
PIVOT
(Max(Show) FOR ID IN ([1], [2], [3])) AS pvt
精彩评论