SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Get Distict First Records
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kyadav
Starting Member

2 Posts

Posted - 02/21/2013 :  02:26:47  Show Profile  Reply with Quote
I have the below table:
[Code]
WorkRequest WorkSegment ChildSegment Status Equipment
IAM2001 FK19929100101 311 Complete 001VALID
IAM2001 FK19929120501 111 Pending 005VERBO
IAM2001 FK19929120501 121 Pending 005VERBO
IAM2001 FK19929141001 111 Pending 010CONTU
IAM2001 FK19929141001 121 Pending 010CONTU
IAM2002 FK19929100101 311 Complete 001VALID
IAM2002 FK19929120501 111 Complete 005VERBO
IAM2002 FK19929120501 121 Complete 005VERBO
IAM2002 FK19929141001 111 Pending 010CONTU
IAM2002 FK19929141001 121 Pending 010CONTU
[/Code]
I need to get the below two records
IAM2001 FK19929120501 111 Pending 005VERBO
IAM2002 FK19929141001 111 Pending 010CONTU

Any Immediate help ?

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 02/21/2013 :  02:42:09  Show Profile  Reply with Quote

DECLARE @tab TABLE(WorkRequest VARCHAR(10), WorkSegment VARCHAR(20), ChildSegment int, Status VARCHAR(10), Equipment VARCHAR(15))
INSERT INTO @tab
SELECT 'IAM2001', 'FK19929100101', 311, 'Complete', '001VALID' union all
SELECT 'IAM2001', 'FK19929120501', 111, 'Pending', '005VERBO' union all
SELECT 'IAM2001', 'FK19929120501', 121, 'Pending', '005VERBO' union all
SELECT 'IAM2001', 'FK19929141001', 111, 'Pending', '010CONTU' union all
SELECT 'IAM2001', 'FK19929141001', 121, 'Pending', '010CONTU' union all
SELECT 'IAM2002', 'FK19929100101', 311, 'Complete', '001VALID' union all
SELECT 'IAM2002', 'FK19929120501', 111, 'Complete', '005VERBO' union all
SELECT 'IAM2002', 'FK19929120501', 121, 'Complete', '005VERBO' union all
SELECT 'IAM2002', 'FK19929141001', 111, 'Pending', '010CONTU' union all
SELECT 'IAM2002', 'FK19929141001', 121, 'Pending', '010CONTU'

--I need to get the below two records
SELECT WorkRequest, WorkSegment, ChildSegment, Status, Equipment
FROM (SELECT *, ROW_NUMBER() OVER( PARTITION BY WorkRequest ORDER BY (select 1)) RN
		FROM @tab
		WHERE Status = 'pending'
	 ) t
WHERE RN=1


--
Chandu
Go to Top of Page

kyadav
Starting Member

2 Posts

Posted - 02/21/2013 :  02:49:54  Show Profile  Reply with Quote
Thank you
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 02/21/2013 :  02:51:46  Show Profile  Reply with Quote
quote:
Originally posted by kyadav

Thank you


Welcome

--
Chandu
Go to Top of Page

harwani.neha
Starting Member

India
1 Posts

Posted - 03/01/2013 :  04:18:37  Show Profile  Reply with Quote
We can do the same using DISTINCT clause also above... Right?
Why to use ROW_NUM ?

njh..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/01/2013 :  04:42:49  Show Profile  Reply with Quote
you cant use DISTINCT as it looks for distinct values of entire combination. so it will still return all as rows have unique values for entire field combination due to ChildSegment,Equipment etc fields

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4611 Posts

Posted - 03/01/2013 :  13:20:48  Show Profile  Reply with Quote
I'm not sure if it matters, but that solution is just getting "lucky" as there is no true order by applied to the data. Just thought I'd mention it incase that solution doesn't work on a larger/real data set.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/01/2013 :  13:23:59  Show Profile  Reply with Quote
yes Lamprey you're right on that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000