Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to have row data as columns SQL Query

Posted on 2011-02-23
3
347 Views
Last Modified: 2012-05-11
Hi,
I have a table with sample data:

GroupID, TaskID, LaborID, QTY
1, Fix Bulbs, 3, 5
1, Fix Stairs, 3, 2
1, Fix XYZ, 4, 1

I want to write a query that would display the LaborID as columns, and the TaskID as the rows.
I suppose this is Pivot, but I am not clear. Been a long month.
0
Comment
Question by:melegant99
  • 2
3 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 34964332
simple try this
SELECT TaskID, [1],[2],[3],[4],[5],[6]
FROM (

		SELECT TaskID, LaborID 
		FROM yourTable	
	) P
	PIVOT
	(
		MAX(LaborID) FOR LaborID IN ([1],[2],[3],[4],[5],[6])
	) AS pvt
ORDER BY TaskID

Open in new window

0
 

Author Comment

by:melegant99
ID: 34964539
I don't know what the LaborID's will be though in advance, it could be any varying amount and ID #.
0
 
LVL 26

Accepted Solution

by:
tigin44 earned 500 total points
ID: 34964565
then you should create the pivot sql dynamically...

just create this part [1],[2],[3],[4],[5],[6] from the table by using a query that will include all the values.. and concat it twith the given query and execute it...

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
query question 12 32
Help With SQL Query 9 28
SQL View / Qtry 3 8
MS SQL + group by time 4 13
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question