Solved

Compare column data

Posted on 2007-04-05
193 Views
Hi Experts,

Here is my table:
Account (AccountId, UserId, Currency)
1                   1            USD
2                    1           USD
3                    2           EUR
4                    1           USD
5                    2           USD

I need to compare the currencies because 1 user must only have 1 currency.
So, in my  example, User1 currency is USD (no problem) so I can  return the currency of User1.
But User2 has 2 currencies so I need to throw an  error.

0
Question by:noulouk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 5

Assisted Solution

Yttribium earned 150 total points
ID: 18856814
Why not have the User Table contain the Currency, and join it to this Account table?

Like
TABLE: [User]
ID
Name
Currency

TABLE [Account]
AccountID
UserID

VIEW AccountCurrency
SELECT     Account.AccountID, Account.UserID, User.Currency
FROM         Account INNER JOIN
User ON Account.UserID = User.UserID
0

LVL 6

Accepted Solution

chiragkhabaria earned 350 total points
ID: 18856940
Something like this

PS Not i have added some records in the table for more testing...

The query will return all the user name whoese has more then one currency in the Currency column...

-- Create the Table Varaible for checking..
Declare @Account Table
(
AccountId int,
UserId int,
Currency varchar(10)
)

-- Insert the Values in the TAble VAraible for testing.
Insert @Account
Select 1,1, 'USD' Union All
Select 2, 1,'USD' Union All
Select 3, 2,'EUR' Union All
Select 4, 1,'USD' Union All
Select 6, 2,'USD' Union All
Select 7, 1,'EUR' Union All
Select 8, 3,'USD' Union All
Select 9, 3,'USD' Union All
Select 10, 4 ,'USD' Union All
Select 11, 4,'EUR'

-- Now this is the query which will extract the duplicate records

Select Distinct UserID  From @Account a
Where UserID In
(
Select  UserID From @Account b where a.UserID = b.UserID And a.Currency <> b.Currency
)

0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month6 days, 20 hours left to enroll