1

Using Microsoft SQL Server 2012.

I have a table called Loan with columns CustomerFname, CustomerLname, PropertyAddress, City, State, BankruptcyAttorneyName, UPB, and LoanDate.

For the sake of this question, I have to write a query to retrieve loan number, customer first name, customer last name, property address, and bankruptcy attorney name.

All of the records that have the same attorney name have to be together, then the customer last name in order from Z to A.

Here's the code I have so far:

SELECT LoanNumber, 
       CustomerFname, 
       CustomerLname, 
       PropertyAddress, 
       BankruptcyAttorneyName
FROM Loan
GROUP BY BankruptcyAttorneyName
ORDER BY CustomerLname DESC

Basically getting the error for all the columns not named BankruptcyAttorneyName.

invalid in select list because not contained in either agg function or GROUP BY clause.

I understand the error, but am unsure of how to go about it. I'm sure there's an easy way, but any help would be appreciated.

2
  • GROUP BY is always (almost) combined with aggregate functions, like SUM, COUNT and MAX/MIN. I you don't need those, you probably don't need GROUP BY either. Commented Jan 18, 2016 at 8:18
  • I guess that just may be it honestly. I took out the GROUP BY and seems to be working OK. Thank you. Commented Jan 18, 2016 at 8:22

1 Answer 1

1

It sounds as though you don't need grouping at all - just order the result by the attorney name first and then by the customer's last name:

SELECT     LoanNumber, 
           CustomerFname, 
           CustomerLname, 
           PropertyAddress, 
           BankruptcyAttorneyName
FROM       Loan
ORDER BY   BankruptcyAttorneyName ASC, CustomerLname DESC -- Here!
Sign up to request clarification or add additional context in comments.

3 Comments

I basically still keep getting that same error. "Column 'Loan.LoanNumber' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. And then the error repeats with CustomerFname, CustomerLname, and PropertyAddress.
Without a group by clause? impossible. Are you sure you copied the query correctly?
Never mind, you were right. My apologies. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.