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
 General SQL Server Forums
 Script Library
 comapre two column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

neeraj1401
Starting Member

36 Posts

Posted - 04/01/2013 :  10:27:32  Show Profile  Reply with Quote
I’m having one table having two column ID and Segment.

Create table test
(id numeric ,
segment numeric,
created_on datetime)


insert into test values (50708520,1,getdate())
insert into test values (50708520,2,getdate())
insert into test values (50708520,3,getdate())
insert into test values (50708520,1,getdate())
insert into test values (50708521,1,getdate())
insert into test values (50708522,1,getdate())
insert into test values (50708523,1,getdate())
insert into test values (50708523,1,getdate())
insert into test values (50708524,1,getdate())
insert into test values (50708524,2,getdate())
insert into test values (50708524,1,getdate())

I want a query/procedure which will found id which have last and first segment matching for that ID on the basis of created_on.

James K
Flowing Fount of Yak Knowledge

3573 Posts

Posted - 04/01/2013 :  10:59:39  Show Profile  Reply with Quote
Your explanation is not 100% clear to me, but something like this?
select
	ID,
	segment_numeric,
	created_on,
	CASE WHEN FirstId = 1 then 'First' else 'Last' end as RankNumber
from
(
	select
		RANK() over (partition by id order by created_on asc) as FirstId,
		RANK() over (partition by id order by created_on desc) as LastId,
		*
	from
		Test
)s
where FirstId = 1 or LastId = 1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 04/01/2013 :  11:52:54  Show Profile  Reply with Quote
If SQL 2012

SELECT *
FROM (
SELECT *,
FIRST_VALUE(segment) OVER (PARTITION BY id ORDER BY created_on) AS First,
LAST_VALUE(segment) OVER (PARTITION BY id ORDER BY created_on) AS Last
FROM Table
)t
WHERE First=Last



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 04/01/2013 :  13:01:21  Show Profile  Reply with Quote
If below 2012 use this

;With CTE
AS
(
SELECT id
FROM
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY created_on) AS Seq,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY created_on DESC) AS BSeq
FROM Table
)t
WHERE Bseq=1
OR Seq=1
GROUP BY id
HAVING COUNT(DISTINCT segment) =1
)


SELECT t.*
FROM Table t
JOIN CTE c
ON c.id = t.id


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 04/01/2013 13:07:51
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.06 seconds. Powered By: Snitz Forums 2000