Solved

Need help with sql coding

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Script to find duplicates 16 20
backups - Strategies 1 13
Mssql SQL query 14 28
Slow SQL query 12 26
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 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

19 Experts available now in Live!

Get 1:1 Help Now