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 |
|
Vassago
Starting Member
33 Posts |
Posted - 2005-12-27 : 18:08:25
|
| I am very new to SQL coding, but have a lot of experience in Access. Please bare with me as I try to explain my situation.I have five tables, as follow:ACCOUNTS:accountnum, typeid11111, 0122222, 0233333, 0144444, 0155555, 02TYPES:typeid, typedes01, Type102, Type203, Type3TRANSACTIONS:tranid, accountnum, trandate, trantype001, 55555, 12/22/05, 147002, 22222, 12/23/05, 148003, 11111, 12/22/05, 147004, 11111, 12/22/05, 147005, 33333, 12/23/05, 147006, 22222, 12/22/05, 147007, 11111, 12/23/05, 147008, 11111, 12/22/05, 147009, 22222, 12/22/05, 147010, 11111, 12/22/05, 147011, 11111, 12/23/05, 147012, 55555, 12/22/05, 148013, 22222, 12/23/05, 147014, 33333, 12/25/05, 148ORIGINFOaccountnum, originid11111, 0333333, 0144444, 02ORIGTYPES:originid, origindes01, Origin102, Origin203, Origin3The table named ACCOUNTS has every account listed on it. I want to create a query with the following fields based on the data from the above tables.accountnum, typedes, typeid, origindes, originid, lasttran148dateMy biggest problem is trying to write the last field, lasttran148date. I want sql to look on TRANSACTIONS for the last time a trantype value of 148 appears on the list, and fill in the trandate as lasttran148date where trantype was 148. This is my code so far:Select ACCOUNTS.accountnum, TYPES.typedes, ACCOUNTS.typeid, ORIGTYPES.origindes, ORIGINFO.originidfrom ACCOUNTSleft join TYPES on ACCOUNTS.accountnum = TYPES.accountnumleft join ORIGINFO on ACCOUNTS.accountnum = ORIGINFO.accountnumleft join ORIGTYPES on ORIGINFO.originid = ORIGTYPES.originidCan anyone give me a suggestion for that last field? Also, are my joins correct or should I use a different type of join to get what I'm looking for? All help is greatly appreciated.Vassago |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2005-12-27 : 18:42:11
|
| Check the following:Select ACCOUNTS.accountnum, TYPES.typedes, ..... , lasttran148date = Case When trantype = 148 then trandateEndfrom ACCOUNTSleft join TYPES ..... |
 |
|
|
Vassago
Starting Member
33 Posts |
Posted - 2005-12-27 : 19:00:14
|
quote: Originally posted by Srinika Check the following:Select ACCOUNTS.accountnum, TYPES.typedes, ..... , lasttran148date = Case When trantype = 148 then trandateEndfrom ACCOUNTSleft join TYPES .....
Thanks! That almost gives me what I need, only it doesn't give me that max of that field. It must be something simple, but how can I obtain the max of that field? |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2005-12-27 : 19:49:20
|
| Max of which field?I guess Max of trandate!!TryWhen trantype = 148 then (Select Max(trandate) from .. where trantype = 148) |
 |
|
|
Vassago
Starting Member
33 Posts |
Posted - 2005-12-27 : 20:25:47
|
quote: Originally posted by Srinika Max of which field?I guess Max of trandate!!TryWhen trantype = 148 then (Select Max(trandate) from .. where trantype = 148)
Yes, I meant the trandate. Thanks for the reply. It's still not giving me what I need. This is what it's giving me as the results with this code:11111, Null11111, Null11111, Null11111, Null11111, Null11111, Null22222, Null22222, 12/23/0522222, Null22222, Null33333, Null33333, 12/25/0555555, Null55555, 12/22/05and this is what I want:11111, Null22222, 12/23/0533333, 12/25/0544444, Null55555, 12/22/05Do you have any other suggestions? |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2005-12-27 : 20:41:06
|
| Try Inner Join instead of Left Join |
 |
|
|
Vassago
Starting Member
33 Posts |
Posted - 2005-12-27 : 20:47:10
|
quote: Originally posted by Srinika Try Inner Join instead of Left Join
Inner join gives the same results...here is my code...maybe I'm not following you completely. Please take a look:SELECT ACCOUNTS.accountnum, lasttran148date = Case When trantype = 148 then (Select Max(trandate) from TRANSACTIONS where trantype = 148)Endfrom ACCOUNTSinner join TRANSACTIONS on ACCOUNTS.accountnum = TRANSACTIONS.accountnumI appreciate all of your help so far. Maybe you can find something wrong with the above code to cause it to give me those results. |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2005-12-27 : 21:01:09
|
| Inner join all :.....Inner join TYPES on ACCOUNTS.accountnum = TYPES.accountnumInner join ORIGINFO on ACCOUNTS.accountnum = ORIGINFO.accountnumInner join ORIGTYPES on ORIGINFO.originid = ORIGTYPES.originid |
 |
|
|
Vassago
Starting Member
33 Posts |
Posted - 2005-12-27 : 21:08:49
|
quote: Originally posted by Srinika Inner join all :.....Inner join TYPES on ACCOUNTS.accountnum = TYPES.accountnumInner join ORIGINFO on ACCOUNTS.accountnum = ORIGINFO.accountnumInner join ORIGTYPES on ORIGINFO.originid = ORIGTYPES.originid
Sorry. I left the other stuff off for now and decided to just go with ACCOUNTS and TRANSACTIONS until I get the lasttran148 working. Do you see anything wrong with my code that would make it give me those above results? |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2005-12-27 : 21:23:34
|
| Sorry Vassago,So far I was answering without having any access to a Database!!Hence its very hard for me to try out some other way!!For another week that will be the case. Hope somebody else might help u, from there!!Until u get some help from somebody, keep on experimenting.Good Luck |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2005-12-27 : 21:34:42
|
| Just remembered, check putting Distinct keyword in front of the list of items as :SELECT Distinct ACCOUNTS.accountnum, lasttran148date = Case When trantype = 148 then (Select Max(trandate) from TRANSACTIONS where trantype = 148)End |
 |
|
|
Vassago
Starting Member
33 Posts |
Posted - 2005-12-27 : 21:56:16
|
quote: Originally posted by Srinika Just remembered, check putting Distinct keyword in front of the list of items as :SELECT Distinct ACCOUNTS.accountnum, lasttran148date = Case When trantype = 148 then (Select Max(trandate) from TRANSACTIONS where trantype = 148)End
It's now giving me one record with a null value for the lasttran148date, and one record with what appears to be the lasttrandate, so we are close... any ideas on how to get rid of the null results if a 148 exists for that particular account? |
 |
|
|
yonabout
Posting Yak Master
112 Posts |
Posted - 2005-12-28 : 09:30:27
|
Hi,I've got the results you were after by aliasing the table and getting the max values for each account type. Probably not the best way of doing it, but an option:select accountnum,max(lasttran148date) as date148from(Select distinct(ACCOUNTS.accountnum), TYPES.typedes, ACCOUNTS.typeid, ORIGTYPES.origindes, ORIGINFO.originid,Case When trantype = 148 then (Select Max(trandate) from TRANSACTIONS where trantype = 148)End as lasttran148datefrom ACCOUNTSleft join TYPES on ACCOUNTS.typeid = TYPES.typeidleft join ORIGINFO on ACCOUNTS.accountnum = ORIGINFO.accountnumleft join ORIGTYPES on ORIGINFO.originid = ORIGTYPES.originidleft join transactions on transactions.accountnum = accounts.accountnum) as accountgroup by accountnum which using your test data returns11111 NULL22222 2005-12-25 00:00:00.00033333 2005-12-25 00:00:00.00044444 NULL55555 2005-12-25 00:00:00.000 Cheers,Yonabout |
 |
|
|
|
|
|
|
|