October 19, 2016 at 10:35 am
Hello,
I have a SQL query which works fine and returns 10 columns and one row of data. What I would like is to pivot this data so that the columns are returned as rows and the row of data is a column.
Is this possible?
I have looked at using the pivot function, but that seems to force me into performing a calculation on the data. I simply want to take the data that is returned and display the columns vertically rather than horizontally.
Thanks in advance.
David
October 19, 2016 at 10:40 am
Aquilagb (10/19/2016)
Hello,I have a SQL query which works fine and returns 10 columns and one row of data. What I would like is to pivot this data so that the columns are returned as rows and the row of data is a column.
Is this possible?
I have looked at using the pivot function, but that seems to force me into performing a calculation on the data. I simply want to take the data that is returned and display the columns vertically rather than horizontally.
Thanks in advance.
David
care to share your sql query?
maybe also would be good to provide some sample data set up scripts ...see here https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
October 19, 2016 at 10:46 am
Check the following article:
http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/
October 19, 2016 at 10:49 am
Thank you for the reply...
The query is this:
SELECT TOP 1000 [LTStringId]
,[LanguageCode]
,[LTStringType]
,[ElementName]
,[SubElementName]
,[TimeAdded]
,[LastModified]
,[DisplayStringId]
FROM LocalizedText
WHERE LTStringId = 'F4752595-BFB9-E3BC-6643-FAFBEC7EA6EF'
Only one row of data will ever be returned even when the LTStringId is changed.
Thanks,
David
October 19, 2016 at 11:00 am
Hi Luis,
I checked out that article but it still suggests that I need to manipulate the data in order to pivot it. Is there no way I can have the data as it is returned now, but rather than the columns be next to each other horizontally, they are underneath each other vertically?
Apologies if I am not explaining this very well, this is all new to me!
Thanks.
October 19, 2016 at 11:33 am
The article that I posted does exactly what you're asking, put the columns as rows. There's no magic way to do it without coding. Here's an example that might have errors due to data types. If you want something tested, read the articles in my signature.
SELECT LT.[LTStringId], u.ColumnName, u.Value
FROM LocalizedText LT
CROSS APPLY (VALUES
([LanguageCode] , 'LanguageCode' )
,([LTStringType] , 'LTStringType' )
,([ElementName] , 'ElementName' )
,([SubElementName] , 'SubElementName' )
,([TimeAdded] , 'TimeAdded' )
,([LastModified] , 'LastModified' )
,([DisplayStringId], 'DisplayStringId'))u(Value,ColumnName)
WHERE LTStringId = 'F4752595-BFB9-E3BC-6643-FAFBEC7EA6EF'
October 19, 2016 at 2:39 pm
Aquilagb (10/19/2016)
Thank you for the reply...The query is this:
SELECT TOP 1000 [LTStringId]
,[LanguageCode]
,[LTStringType]
,[ElementName]
,[SubElementName]
,[TimeAdded]
,[LastModified]
,[DisplayStringId]
FROM LocalizedText
WHERE LTStringId = 'F4752595-BFB9-E3BC-6643-FAFBEC7EA6EF'
Only one row of data will ever be returned even when the LTStringId is changed.
If you can only ever return one row of data, why are you selecting the TOP 1000?
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply