Function

Soumi Chakraborty
Soumi Chakraborty used Ask the Experts™
on
/ *9.      Write a SQL query to retrieve loan number, customer first name, customer last name,
 property address, and bankruptcy attorney name.  I want all the records that have the
 same attorney name to be together, then the customer last name in order from Z-A
 (ex.Customer last name of Wyatt comes before customer last name of Anderson)*/

 Select  LoanNumber,CustomerFname,CustomerLname,Propertyaddress,BankruptcyAttorneyName
 From Loan
 Group BY BankruptcyAttorneyName,LoanNumber,CustomerFname,CustomerLname,Propertyaddress
Order by
LTrim(Reverse(Left(Reverse(CustomerLname), CharIndex(' ', Reverse(CustomerLname))))) Desc,
CustomerLname Desc -- Fall-over for when there isn't a last name

Is it right comment?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Contract ERP Admin/Consultant
Commented:
Since the last and first names are already separate, you probably don't need to reverse them or us any functions. And since you're not SUM'ing or doing any grouping functions, you don't need the GROUP BY, either. Ordering by the BankruptcyAttorneyName will group them in order:

SELECT  LoanNumber,
        CustomerFname,
        CustomerLname,
        Propertyaddress,
        BankruptcyAttorneyName
FROM    loan
ORDER BY BankruptcyAttorneyName,
         CustomerLname DESC,
         CustomerFname DESC,
         LoanNumber

Open in new window

Commented:
Select  LoanNumber,CustomerFname,CustomerLname,Propertyaddress,BankruptcyAttorneyName
  From Loan
  Group BY BankruptcyAttorneyName,LoanNumber,CustomerFname,CustomerLname,Propertyaddress
 Order by  CustomerLname Desc 

Open in new window

Author

Commented:
I've requested that this question be deleted for the following reason:

simple
dsackerContract ERP Admin/Consultant

Commented:
Question asked, question answered. Recommend splitting the points between me and Deepak (250 each) with a grade of A (each).

Author

Commented:
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial