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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Confused Select Statement

Author  Topic 

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-08-15 : 01:11:02
[code]emp_number exp_no exp_desc amount
E2345 1 Fees 1656
E2345 2 Food 456
E2345 3 Movie 565
E2345 4 Books 567
E2345 5 Utility 566
E2345 6 Gas 340
E2345 7 Elec 1656
E2345 8 CDs 133
E2345 9 Rent 157

E6622 1 Fees 2356
E6622 2 Food 456
E6622 3 Movie 565
E6622 4 Books 2356
E6622 5 Utility 566
E6622 6 Gas 340
E6622 7 Elec 166
E6622 8 CDs 133
E6622 9 Rent 157


E9265 1 Fees 5556
E9265 2 Food 456
E9265 3 Movie 565
E9265 4 Books 567
E9265 5 Utility 556
E9265 6 Gas 340
E9265 7 Elec 656
E9265 8 CDs 133
E9265 9 Rent 157

E7765 1 Fees 6756
E7765 2 Food 456
E7765 3 Movie 565
E7765 4 Books 567
E7765 5 Utility 566
E7765 6 Gas 340
E7765 7 Elec 156
E7765 8 CDs 6756
E7765 9 Rent 157 [/code]


How can I find if there is any amount equal to the amount with the exp_no = 1 with the same emp_number.

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-08-15 : 01:44:30
Somthing like this

Select Emp_Number From
(Select Emp_Number,Amount From TableName Where Exp_No = 1) t
Inner Join
TableName t1 On T1.Emp_Number = t.Emp_Number And
T1.Amount = t.Amount


Chirag
Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-08-15 : 01:57:53
I should get

E2345 1 Fees 1656
E2345 7 Elec 1656
E6622 1 Fees 2356
E6622 4 Books 2356
E7765 1 Fees 6756
E7765 8 CDs 6756

When I use the above query Iam not getting it..
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-08-15 : 02:04:26
try this

Select T1.* From
(Select Emp_Number,Amount From TableName Where Exp_No = 1) t
Inner Join
TableName t1 On T1.Emp_Number = t.Emp_Number And
T1.Amount = t.Amount


Chirag
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-15 : 02:08:02
[code]select a.*
from table t inner join table a
on t.amount = a.amount
where t.exp_no = 1
and t.emp_number = a.emp_number[/code]


KH

Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-08-15 : 02:15:49
When I use the above query Iam getting the below shown value

E2345 1 Fees 1656
E6622 1 Fees 2356
E9265 1 Fees 5556
E7765 1 Fees 6756
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-15 : 02:33:23
quote:
Originally posted by sqllearner

When I use the above query Iam getting the below shown value

E2345 1 Fees 1656
E6622 1 Fees 2356
E9265 1 Fees 5556
E7765 1 Fees 6756



Nope. I am not getting this result


KH

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-08-15 : 02:44:47


Declare @var Table
(
Emp_Number varchar(10),
exp_no int,
exp_desc varchar(10),
amount money
)
Insert @var
Select 'E2345',1,'Fees',1656 Union All
Select 'E2345',2,'Food',456 Union All
Select 'E2345',3,'Movie',565 Union All
Select 'E2345',4,'Books',567 Union All
Select 'E2345',5,'Utility',566 Union All
Select 'E2345',6,'Gas',340 Union All
Select 'E2345',7,'Elec',1656 Union All
Select 'E2345',8,'CDs',133 Union All
Select 'E2345',9,'Rent',157 Union All
Select 'E6622',1,'Fees',2356 Union All
Select 'E6622',2,'Food',456 Union All
Select 'E6622',3,'Movie',565 Union All
Select 'E6622',4,'Books',2356 Union All
Select 'E6622',5,'Utility',566 Union All
Select 'E6622',6,'Gas',340 Union All
Select 'E6622',7,'Elec',166 Union All
Select 'E6622',8,'CDs',133 Union All
Select 'E6622',9,'Rent',157 Union All
Select 'E9265',1,'Fees',5556 Union All
Select 'E9265',2,'Food',456 Union All
Select 'E9265',3,'Movie',565 Union All
Select 'E9265',4,'Books',567 Union All
Select 'E9265',5,'Utility',556 Union All
Select 'E9265',6,'Gas',340 Union All
Select 'E9265',7,'Elec',656 Union All
Select 'E9265',8,'CDs',133 Union All
Select 'E9265',9,'Rent',157 Union All
Select 'E7765',1,'Fees',6756 Union All
Select 'E7765',2,'Food',456 Union All
Select 'E7765',3,'Movie',565 Union All
Select 'E7765',4,'Books',567 Union All
Select 'E7765',5,'Utility',566 Union All
Select 'E7765',6,'Gas',340 Union All
Select 'E7765',7,'Elec',156 Union All
Select 'E7765',8,'CDs',6756 Union All
Select 'E7765',9,'Rent',157


Select t3.* From
(Select t1.Emp_Number,t1.Amount From
(Select Emp_Number,Amount From @var Where Exp_No = 1) t
Inner Join
(Select * From @var ) t1 On
T1.Emp_Number = t.Emp_Number And
T1.Amount = t.Amount
Group by t1.Emp_Number ,t1.Amount
Having Count(1) >1)
as t2
Inner Join
@var t3 on
t2.Amount = t3.Amount and t2.emp_number = t3.emp_number





Chirag
Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-08-15 : 03:09:19
Got it...Thanks a lot.....Excellent responses friends...
Go to Top of Page
   

- Advertisement -