Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Get Distict First Records

Author  Topic 

kyadav
Starting Member

2 Posts

Posted - 2013-02-21 : 02:26:47
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-02-21 : 02:42:09
[code]
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[/code]

--
Chandu
Go to Top of Page

kyadav
Starting Member

2 Posts

Posted - 2013-02-21 : 02:49:54
Thank you
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-02-21 : 02:51:46
quote:
Originally posted by kyadav

Thank you


Welcome

--
Chandu
Go to Top of Page

harwani.neha
Starting Member

1 Post

Posted - 2013-03-01 : 04:18:37
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

52326 Posts

Posted - 2013-03-01 : 04:42:49
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
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-03-01 : 13:20:48
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

52326 Posts

Posted - 2013-03-01 : 13:23:59
yes Lamprey you're right on that

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

Go to Top of Page
   

- Advertisement -