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.
| Author |
Topic |
|
stharish
Starting Member
43 Posts |
Posted - 2012-06-26 : 18:10:33
|
| 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
15732 Posts |
Posted - 2012-06-26 : 18:24:26
|
| SELECT * FROM myTableWHERE ContractName IN(SELECT ContractName FROM myTable GROUP BY ContractName HAVING COUNT(*)>1) |
 |
|
|
stharish
Starting Member
43 Posts |
Posted - 2012-06-26 : 18:39:22
|
| I should have mentioned that duplicate ContractName is okay as long as ContractID is different. Thanks |
 |
|
|
stharish
Starting Member
43 Posts |
Posted - 2012-06-26 : 19:39:26
|
| I had this wrong.I have a table ACCOUNTS with columns AccountName and AccountIDI have another table CONTRACTS with columns ContractName and ContractIDEach 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 |
 |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2012-06-27 : 18:42:27
|
quote: Originally posted by stharish I had this wrong.I have a table ACCOUNTS with columns AccountName and AccountIDI have another table CONTRACTS with columns ContractName and ContractIDEach 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? |
 |
|
|
stharish
Starting Member
43 Posts |
Posted - 2012-11-02 : 00:41:30
|
| 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 |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-11-02 : 04:00:12
|
[code]--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 ondeclare @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 @Accountsselect 1, 'Account A' union allselect 2, 'Account B' union allselect 3, 'Account C'insert @Contractsselect 1, 'Contract A' union allselect 2, 'Contract B' union allselect 3, 'Contract C' union allselect 4, 'Contract A'insert @AccountContractsselect 1,1 union allselect 1,4 union allselect 2,1 union allselect 2,2 union allselect 3,3--I need to find contracts with matching names but under different accounts.select a.AccountID, a.AccountName, c.ContractID, c.ContractNamefrom @Accounts ajoin @AccountContracts ac on ac.AccountID = a.AccountIDjoin @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 AIn this example (if it isn't wrong) - what is the wanted result?[/code] Too old to Rock'n'Roll too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-11-02 : 04:03:56
|
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. |
 |
|
|
|
|
|
|
|