|
|
Subject:
Simple SQL question
Category: Computers > Programming Asked by: terrydavis1949-ga List Price: $25.00 |
Posted:
23 Feb 2006 05:11 PST
Expires: 25 Mar 2006 05:11 PST Question ID: 448739 |
All I need is a SQL statement that does the following With SQL, how can you identify the Person with the largest total amount of money spent and return their FirstName, LastName, Address components, and the total amount of all their transactions? your tables Person, address, transactions Person has these fields PKPERSON, PREFIX, FIRSTNAME, LASTNAME, SUFFIX, GENDER Address has these fields PKADDRESS, PKPERSON (FK), ADDRESS1, ADDRESS2, CITY, STATE, ZIP, PLUS4 TRANSACTIONS has these fields PKTRANSACTION,PKPERSON (FK), TRANSACTION_DATE, TRANSACTION_AMOUNT, TRANSACTION_TYPE Where I am running into trouble is totaling each persons total individual purchases,if you could help me out with that part I think I could take it from there. I am in a crunch for time so a tip will be left if this is answered quickly |
|
Subject:
Re: Simple SQL question
Answered By: answerguru-ga on 23 Feb 2006 08:48 PST Rated: |
Hello Terry, The ideal solution to your question involves three joins across your tables using the primary/foreign key relationships that you've indicated. The result is then grouped by the fields that have the same values (ie. everything with the exception of the sum of transactions). To obtain the highest spender, we first sort the result based on the aggregated sum of their transactions, and then retrieve the first record in the result. Here is the query you requested: SELECT TOP 1 p.Prefix, p.FirstName, p.LastName, p.Suffix, p.Gender, a.Address1, a.Address2, a.City, a.State, a.Zip, a.Plus4, t.Transaction_Date, SUM(t.Transaction_Amount) As TotalTransactions, t.Transaction_Type FROM Person p JOIN Address a ON a.PkPerson = p.PkPerson JOIN Transaction t ON t.PkPerson = p.PkPerson GROUP BY p.Prefix, p.FirstName, p.LastName, p.Suffix, p.Gender, a.Address1, a.Address2, a.City, a.State, a.Zip, a.Plus4, t.Transaction_Date, t.Transaction_Type ORDER BY TotalTransactions DESC In general, using JOIN statements to connect tables is preferred rather than using subselect statements. This is to increase performance as well as maintainability. As a side note, depending on your environment, the "TOP 1" keyword may not be recognized. To overcome this, you can remove it and preceed your select statement with: SET ROWCOUNT 1 This will limit the result of the next statement to only one record. Since the query is sorting by total transaction amount in descending order, you will get the record you are interested in. Hopefully this gives you a better understanding of how to perform this type of query. If you are unsure about anything above, please post a clarification and I will respond promptly. Cheers, answerguru-ga |
terrydavis1949-ga rated this answer: |
|
Subject:
Re: Simple SQL question
From: forzan-ga on 23 Feb 2006 06:13 PST |
I do web development in MySQL and PHP, thus I understand a bulk of SQL, but I'm not sure if this solution works across different SQL databases. Regardless, this is how it would be done in MySQL with subselects, and at a minimum, it should offer you a clue. SELECT person.pkperson, person.firstname, person.lastname, (SELECT COUNT(*) FROM transactions WHERE transactions.pkperson = person.pkperson) AS purchasecount, (SELECT SUM(transaction_amount) FROM transactions WHERE transactions.pkperson = person.pkperson) AS totalpurchasevalue FROM person WHERE pkperson = $pkperson Sorry for the PHP variable at the end, but I'm not sure how you plan to specify the ID, and I'm just used to PHP. I could write a solution with joins as well, but that would be really annoying. Hopefully, you can perform subselects. -Frank Crook |
Subject:
Re: Simple SQL question
From: answerguru-ga on 23 Feb 2006 08:50 PST |
That's close forzan-ga, however Terry appeared to be after the single highest spender, not a particular person based on ID. answerguru-ga |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |