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 |
fdtoo
Starting Member
28 Posts |
Posted - 2006-04-25 : 04:23:49
|
I have the following fields in table A:Date | Descrip | Amt Dr ---------------------------------- 01/02/2006 | 740240 |(2,400.00)14/02/2006 | 740241 |(3,000.00)15/02/2006 | 41142 | 1,800.00 20/02/2006 | 41142 | 2,700.00 25/02/2006 | 740245 | 5,200.00I have the following fields in table B:Date | Descrip | Amt Dr ---------------------------------- 02/02/2006 |88258 | 1,400.00 17/02/2006 |740244 | (1,500.00)25/02/2006 |740245 | 5,200.00 There are no referencial key between TableA & TableB, What i want is to extract the date,descrip & Amt data fromTable A where it's Descrip data is not the same as the datain Table B's Descrip column.My sql syntax is as follows:SELECT CASE WHEN TableA.Amt < 0 THEN TableA.[Date] ELSE 0 END, CASE WHEN TableA.Amt < 0 THEN TableA.Descrip ELSE 0 END, CASE WHEN TableA.Amt < 0 THEN TableA.Amt - (TableA.Amt * 2)ELSE 0 END AS [Add Back]FROM TableA,TableBWHERE TableA.Descrip <> TableB.DescripGROUP BY TableA.Date,TableA.Amt,TableA.DescripThe output of the above is as follows:Date | Descrip | Amt Dr ---------------------------------- 01/02/2006 | 740240 |(2,400.00)14/02/2006 | 740241 |(3,000.00)15/02/2006 | 0 | 020/02/2006 | 0 | 025/02/2006 | 740245 | 5,200.00Notice that, descrip with 740245 appearing in both tables is what the sql should filter out, but failed to do so,what i want is as below:Date | Descrip | Amt Dr ---------------------------------- 01/02/2006 | 740240 |(2,400.00)14/02/2006 | 740241 |(3,000.00)Can i achieve this? Please help. |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-04-25 : 04:47:26
|
I guess desired output and your sample data doesnt match with your requirement.. check look at the following query .. Declare @Tbl1 Table ([Date] Varchar(10),Descrip Varchar(100),Amt float)Insert @Tbl1 Select '01/02/2006' , '740240' ,2400.00 Union AllSelect '14/02/2006', '740241' ,3000.00 Union AllSelect '15/02/2006', '41142' ,1800.00 Union AllSelect '20/02/2006','41142' ,2700.00 Union AllSelect '25/02/2006', '740245', 5200.00Declare @Tbl2 Table ([Date] Varchar(10),Descrip Varchar(100),Amt float)Insert @Tbl2Select '02/02/2006 ' , '88258' ,1400.00 Union AllSelect '17/02/2006', '740244' ,1500.00 Union AllSelect '25/02/2006', '740245' ,5200.00 SELECTCASE WHEN Amt > 0 THEN [Date] ELSE '' END,CASE WHEN Amt > 0 THEN Descrip ELSE 0 END,CASE WHEN Amt > 0 THEN (Amt * 2)-Amt ELSE 0 END AS [Add Back]FROM @Tbl1WHERE Descrip Not in (Select Descrip From @Tbl2)GROUP BY [Date],Amt,Descrip If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them. |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-04-25 : 06:20:42
|
Hi fdtoo,chiragkhabaria is right - there seems to be some inconsistencies in your example. Can you please check it and disambiguate. Perhaps an exists statement would be useful? See below for an example...--dataset dateformat dmyDeclare @Tbl1 Table ([Date] Varchar(10), Descrip Varchar(100), Amt float)Insert @Tbl1 Select '01/02/2006' , '740240' ,-2400.00Union All Select '14/02/2006', '740241' ,-3000.00Union All Select '15/02/2006', '41142' ,1800.00Union All Select '20/02/2006','41142' ,2700.00Union All Select '25/02/2006', '740245', 5200.00Declare @Tbl2 Table ([Date] Varchar(10), Descrip Varchar(100), Amt float)Insert @Tbl2 Select '02/02/2006 ' , '88258' ,1400.00Union All Select '17/02/2006', '740244' ,-1500.00Union All Select '25/02/2006', '740245' ,5200.00 --calculationselect * from @Tbl1 a where not exists (select * from @Tbl2 where [Date] = a.[Date] and Descrip = a.Descrip) Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
|
|
|
|
|
|
|