Solved

Need help with sql coding

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

696 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