Solved

Need help with sql coding

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

929 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now