Solved

Compare column data

Posted on 2007-04-05
2
188 Views
Last Modified: 2010-03-19
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.

Thanks in advance for your help.
                 
0
Comment
Question by:noulouk
2 Comments
 
LVL 5

Assisted Solution

by:Yttribium
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

by:
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now