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
 New to SQL Server Programming
 compare previous to current record row
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dbonneau
Yak Posting Veteran

50 Posts

Posted - 03/03/2014 :  07:18:03  Show Profile  Reply with Quote
Hi, My current code returns account_number with multiple start_date regardless of the value is same or not.
However, I would like to get only the account number when the value on start_date is different within same account_number. Could you please help me how to do that?
Thank you!


select
acct_number
count(start_date) from table_A
group by acct_number, start_date
having(count(start_date) > 1)

VeeranjaneyuluAnnapureddy
Posting Yak Master

India
168 Posts

Posted - 03/03/2014 :  07:59:29  Show Profile  Reply with Quote
plz provide sample data. what is the expected result.....


Veera
Go to Top of Page

dbonneau
Yak Posting Veteran

50 Posts

Posted - 03/03/2014 :  08:08:52  Show Profile  Reply with Quote
Hi, Here is the sample data set.
I just want to extract 12345 and 23456 ( when there is different start_date), but not 98765 ( start_date are same ). Thanks
acct_number,start_date
12345,02/01/2000
12345,02/01/2000
12345,09/01/2003
98765,10/11/2002
98765,10/11/2002
98765,10/11/2002
23456,6/26/2011
23456,7/20/2011
Go to Top of Page

kennejd
Starting Member

USA
11 Posts

Posted - 03/03/2014 :  08:49:38  Show Profile  Reply with Quote
Are you looking for something like this?

/*create table #a(acct varchar(10),sdate date)
insert into #a(acct, sdate)
values

('12345','02/01/2000')
,('12345','02/01/2000')
,('12345','09/01/2003')
,('98765','10/11/2002')
,('98765','10/11/2002')
,('98765','10/11/2002')
,('23456','6/26/2011')
,('23456','7/20/2011')*/

with a as(
select
acct
,sdate
from #a
group by
acct
,sdate
having count(*) > 1
)

select acct from a
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/03/2014 :  13:16:17  Show Profile  Reply with Quote
quote:
Originally posted by dbonneau

Hi, Here is the sample data set.
I just want to extract 12345 and 23456 ( when there is different start_date), but not 98765 ( start_date are same ). Thanks
acct_number,start_date
12345,02/01/2000
12345,02/01/2000
12345,09/01/2003
98765,10/11/2002
98765,10/11/2002
98765,10/11/2002
23456,6/26/2011
23456,7/20/2011


just this small tweak

select
acct_number
count(start_date) from table_A
group by acct_number, start_date
having(count(distinct start_date) > 1)


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

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 Posts

Posted - 03/03/2014 :  13:47:22  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

quote:
Originally posted by dbonneau

Hi, Here is the sample data set.
I just want to extract 12345 and 23456 ( when there is different start_date), but not 98765 ( start_date are same ). Thanks
acct_number,start_date
12345,02/01/2000
12345,02/01/2000
12345,09/01/2003
98765,10/11/2002
98765,10/11/2002
98765,10/11/2002
23456,6/26/2011
23456,7/20/2011


just this small tweak

select
acct_number
count(start_date) from table_A
group by acct_number, start_date
having(count(distinct start_date) > 1)


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




quote:

However, I would like to get only the account number when the value on start_date is different within same account_number.




A little change to what Visakh propose, I think this is what you're looking for .. note: excluded start_date from the group by clause.

select
acct_number
from @table
group by acct_number
having count(distinct start_date) > 1

I believe, including Start_Date in group by would result in 0 records

Cheers
MIK
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