Solved

Need help with sql coding

Posted on 2011-09-09
4
269 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

Suggested Solutions

Title # Comments Views Activity
Need sql in string 2 29
Return Rows as per Quantity of Columns Value In SQL 6 27
how to make geography query faster?  SQL 7 45
Need SSIS project 2 27
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

749 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