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
 Finding Duplicate Data
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stharish
Starting Member

41 Posts

Posted - 06/26/2012 :  18:10:33  Show Profile  Reply with Quote
I have a table containing ContractName(varchar) and ContractID(int). I'm looking for help creating a query to identify duplicate contract names.

Thanks

robvolk
Most Valuable Yak

USA
15559 Posts

Posted - 06/26/2012 :  18:24:26  Show Profile  Visit robvolk's Homepage  Reply with Quote
SELECT * FROM myTable
WHERE ContractName IN(SELECT ContractName FROM myTable GROUP BY ContractName HAVING COUNT(*)>1)
Go to Top of Page

stharish
Starting Member

41 Posts

Posted - 06/26/2012 :  18:39:22  Show Profile  Reply with Quote
I should have mentioned that duplicate ContractName is okay as long as ContractID is different. Thanks
Go to Top of Page

stharish
Starting Member

41 Posts

Posted - 06/26/2012 :  19:39:26  Show Profile  Reply with Quote
I had this wrong.

I have a table ACCOUNTS with columns AccountName and AccountID
I have another table CONTRACTS with columns ContractName and ContractID

Each account may contain multiple contracts with the same name but different contract ID's. I need to find contracts with matching names but under different accounts.

Thanks
Go to Top of Page

bitsmed
Posting Yak Master

Denmark
100 Posts

Posted - 06/27/2012 :  18:42:27  Show Profile  Reply with Quote
quote:
Originally posted by stharish

I had this wrong.

I have a table ACCOUNTS with columns AccountName and AccountID
I have another table CONTRACTS with columns ContractName and ContractID

Each account may contain multiple contracts with the same name but different contract ID's. I need to find contracts with matching names but under different accounts.

Thanks


How do you link a contract to an account?
Is there perhaps a third table linking contractid to accountid?
Go to Top of Page

stharish
Starting Member

41 Posts

Posted - 11/02/2012 :  00:41:30  Show Profile  Reply with Quote
Yes, I have a table called AccountContracts that contains both an AccountID and ContractID that I can use to join the Accounts and Contracts tables
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 11/02/2012 :  04:00:12  Show Profile  Visit webfred's Homepage  Reply with Quote


--I have a table ACCOUNTS with columns AccountName and AccountID
--I have another table CONTRACTS with columns ContractName and ContractID
--I have a table called AccountContracts that contains both an AccountID and ContractID 

set nocount on
declare @Accounts table(AccountID int, AccountName varchar(30))
declare @Contracts table(ContractID int, ContractName varchar(30))
declare @AccountContracts table(AccountID int, ContractID int)

--Each account may contain multiple contracts with the same name but different contract ID's. 

insert @Accounts
select 1, 'Account A' union all
select 2, 'Account B' union all
select 3, 'Account C'

insert @Contracts
select 1, 'Contract A' union all
select 2, 'Contract B' union all
select 3, 'Contract C' union all
select 4, 'Contract A'

insert @AccountContracts
select 1,1 union all
select 1,4 union all
select 2,1 union all
select 2,2 union all
select 3,3

--I need to find contracts with matching names but under different accounts.

select a.AccountID, a.AccountName, c.ContractID, c.ContractName
from @Accounts a
join @AccountContracts ac on ac.AccountID = a.AccountID
join @Contracts c on c.ContractID = ac.ContractID

--AccountID   AccountName                    ContractID  ContractName
------------- ------------------------------ ----------- ------------------------------
--1           Account A                      1           Contract A
--2           Account B                      1           Contract A
--2           Account B                      2           Contract B
--3           Account C                      3           Contract C
--1           Account A                      4           Contract A

In this example (if it isn't wrong) - what is the wanted result?




Too old to Rock'n'Roll too young to die.
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 11/02/2012 :  04:03:56  Show Profile  Visit webfred's Homepage  Reply with Quote
btw. post your questions like I did it for you above and you will get good answers...


Too old to Rock'n'Roll too young to die.
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.08 seconds. Powered By: Snitz Forums 2000