Solved

Need help with sql coding

Posted on 2011-09-09
4
274 Views
Last Modified: 2012-05-12
Project     |    PONum            |    PODesc
200078        3152000745      PTNumber
200078        3153000135      CANumber
200078      3153000235          CANumber
200090      3153000234          PTNumber

For the above give sample table, I should get a result as below: For every project number if there are multiple PONum, I need to consider only the first two and display them as PONum1(3152000745), PONum2 ,PODesc1 and PODesc2 resp.
ex the above table should show as below:

project   |  PONum1      |   PODesc1 |  PONum2       |   PODesc2
200078  | 3152000745 | PTNumber | 3153000135 |  CANumber
200090  | 3153000234 | PTNumber |                       |  

Thanks!
Please note that the third PONumber for the first project number is ignored. only the first two are considered.


0
Comment
Question by:sqlcurious
[X]
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
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
tim_cs earned 500 total points
ID: 36512003
Give this a try
;WITH CTE AS (
SELECT	
	Project
	,PONum
	,PODesc
	,ROW_NUMBER() OVER (PARTITION BY Project ORDER BY PONum) RN
FROM
	TAbleName
)


SELECT
	Project
	,B.PONum PONum1
	,B.PODesc PODesc1
	,C.PONum PONum2
	,C.PODesc PODesc2
FROM
	CTE a
	LEFT JOIN CTE b
		ON a.Project = b.Project
		AND b.RN = 1
	LEFT JOIN CTE c
		ON A.Project = c.Project
		AND c.RN = 2
WHERE
	a.RN = 1

Open in new window

0
 

Author Comment

by:sqlcurious
ID: 36513500
Thanks a lot it worked!!! Can you please tell me the exact meaning of the code though, would like to understand it better.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36514816
Start by reading up on CTEs in SQL Server BOL.  Let me know if you need a link.
0
 

Author Closing Comment

by:sqlcurious
ID: 36523460
thnks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 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