![]() |
| | #1 |
| Registered User Join Date: Jun 2003
Posts: 89
| For the SQL experts out there I want the SQL instructions to get the names of the customers' latest transaction (decided by date column) for each customer. thanx in advance Boomba |
| Boomba is offline | |
| | #2 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| Two ways: LEFT JOIN table2 ON table2.account=table1.account Thats good if its possible that table1.account doesn't exist and you still want the data from table2. Other way SELECT blah blah blah FROM table1, table2 WHERE table1.account=table2.account |
| Thantos is offline | |
| | #3 |
| Bob Dole for '08 Join Date: Sep 2004
Posts: 618
| related to sql I've been asking everyone for their suggestions on a good really basic intro to SQL tutorial.
__________________ Hmm |
| B0bDole is offline | |
| | #4 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| I did the sink or swim method with a project |
| Thantos is offline | |
| | #5 |
| Registered User Join Date: Aug 2001 Location: Newport, South Wales, UK
Posts: 1,094
| Awwwwwwww yeah, Thantos. Countless hours spent writing the same thing over and over again with minor changes. My colleagues kept glaring at me every time I tried to tidy it all away into a function or two. |
| SMurf is offline | |
| | #6 | |
| Registered User Join Date: Jun 2003
Posts: 89
| Quote:
without trying that i know that would only return all the transactions that have the same account.....not what i'm looking for...what i meant is that i want to get EACH customers's LATEST transaction. ...so there should only be one customer (name and transaction balance) returned for every Account. | |
| Boomba is offline | |
| | #7 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| One way: Code: SELECT customers.account, Max(transactions.transactiondate) AS MaxOftransactiondate FROM customers INNER JOIN transactions ON customers.account = transactions.account GROUP BY customers.account;
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #8 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| heh sorry guess I didn't fully read the post. If you want the full transaction and customer information its gonna be difficult. You could use a subquery to retrieve the transaction information. However I personally would go a different route. I would retrieve the customer data in one query. And then in another query I would get the transaction data. If you are willing to do a little restructuring you could make this a lot easier. Just add another column to your customer data that holds the transaction id for the last transaction. |
| Thantos is offline | |
| | #9 | |
| Banned Join Date: Feb 2003 Location: Australia
Posts: 986
| Quote:
If you want to start with Microsoft SQL Server, SQLTeam.com is a good place to start. | |
| nickname_changed is offline | |
| | #10 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| In addition to my last post, the following will produce a list of all customers with last transaction date, including customers that don't have any transactions. In the latter case, the Max trans date field will be NULL. Code: SELECT customers.account, Max(transactions.transactiondate) AS MaxOftransactiondate FROM customers LEFT JOIN transactions ON customers.account = transactions.account GROUP BY customers.account; ![]() [edit] Oh, on a third re-read, I guess I'm misunderstanding! Does the OP want just the date, or the full transaction record?
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #11 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| I'm assuming full transaction record. |
| Thantos is offline | |
| | #12 | |
| Registered User Join Date: Feb 2005
Posts: 416
| Bob, Here is a foundation SQL tutorial and command / function reference.
__________________ Quote:
| |
| Jaqui is offline | |
| | #13 |
| Bob Dole for '08 Join Date: Sep 2004
Posts: 618
| 06:04 PM Bob, Here is a foundation SQL tutorial and command / function reference. only 2 hrs after my test. Oh well, we'll see how well I guessed on that one.
__________________ Hmm |
| B0bDole is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Speed/size advantage to SQL? | MK27 | Tech Board | 15 | 06-26-2009 11:28 AM |
| OOP Question DB Access Wrapper Classes | digioz | C# Programming | 2 | 09-07-2008 04:30 PM |
| Embedded SQL | sarac | C Programming | 1 | 05-04-2006 09:09 AM |
| functions seems not to exit from a certain code block | jaro | C Programming | 1 | 03-21-2006 07:52 AM |
| Problem with embedded SQL in C/C++ (ECPG) | NeuralClone | C Programming | 4 | 10-21-2005 05:16 PM |