Reliable Spreadsheet Solutions - Call Now 281-379-2000 Microsoft Excel - Visual Basic for Applications Consultant   Reliable Spreadsheet Solutions - Call Now 281-379-2000 !
PROFESSIONAL SOFTWARE DEVELOPMENT
Reliable Excel VBA Solutions !
 
Microsoft Office : Excel - Visual Basic for Applications [ VBA ] Software 

Beyond Technology

Insight for Microsoft Excel POWER-Users
Spreadsheet Power-User Tips
  Enable ease and consistency of data entry with a dropdown list of possible entries for a range  dropdown list in cell
  Designing an array formula that functions similar to a VLOOKUP function with multiple criteria and returns a text entry  mult-criteria text lookup
  How Excel workbooks become bloated and what can be done about it  spreadsheet bloat
  Spreadsheet Power-User Tips  more ...
Getting to Know Microsoft Visual Basic for Applications
  Decision Structure that conditionally executes the embedded code  if...then[...else]
  If you are new to VBA and trying to get a handle on programming, this is a MUST READ article for you!  what are variables ?
  Discover a special, more flexible variety of arrays  dynamic arrays
  Getting to Know Microsoft Visual Basic for Applications  more ...
Microsoft Excel Advanced VBA - Just for Geeks
  MUST SEE!  A named range that expands and contracts automatically based on the number of items in the source data table  dynamic named ranges
  MUST SEE! How to identify the last used row in an Excel worksheet  finding real last cell
  Creating charts that adapt as the size of the source data range varies  charting dynamic data
  Microsoft Excel Advanced VBA - Just for Geeks  more ...
Excel Spreadsheet Tools and Tutorials
  Mortgage & Auto Loan Payment Calculator » Amortization with Excel Spreadsheets  amortization guide
  Breakeven Analysis with Excel Spreadsheets Tutorial  breakeven analysis
  Standard Deviation in Excel Spreadsheets Tutorial  standard deviation
  Weighted Average in Excel Spreadsheets Tutorial  weighted average


Microsoft Excel VBA Custom Application Development
  Microsoft Excel VBA Custom Application Development  excel vba customization
  Microsoft Excel VBA Professional Development Experience  professional experience
  Microsoft Excel VBA Custom Application Development  project request form
Microsoft Excel VBA Solutions : Real-World Project Profiles
  How deep is your data?  engineering analysis
  Financial reporting can be a challenging endeavor.  financial reporting
  To anticipate customers needs is to be rewarded with their business.  sales forecasting
  Are time-hungry paperwork tasks building up on you?  automated billing
  An Engineer-in-a-Box  engineering design
  Partial List of Beyond Technology Clients - Past & Present  clients - past & present


Reliable Spreadsheet Solutions - Call Now 281-379-2000 !
  Visitors' Comments  gratuitous kudos
  Add to Favorites 
  Reliable Spreadsheet Solutions - Call Now 281-379-2000 !  e-mail contact

20831 Rosehill Church Rd
Tomball, Texas 77377


SQL for the Rest of Us ( Part 2 of 5 )
Sorting Results and Predicates


Rodney POWELL
Microsoft MVP - Excel
need a developer for
your excel project ?
go here
This is the continuation of a five-part introduction to Structured Query Language (SQL). For your convenience, here is a link back to Part 1.



An ORDER BY clause in SQL uses a comma-delimited listing of field names to designate the order of the result set. The following example would retrieve all employee records using the LastName field as the first sort key then the FirstName field as the second sort key.
	SELECT * FROM Employees
	ORDER BY LastName, FirstName;
The default order is ascending. You can also request the records to be returned in the reverse order by using the DESC keyword after the clause.
	SELECT * FROM Employees
	ORDER BY Salary DESC;
You can also specify the sort by field positions rather than by the field names.
	SELECT LastName, FirstName FROM Employees
	ORDER BY 1, 2;
There are four predicates that can be used in a SELECT statement.

Predicate Description
ALL Returns all records, even duplicates
DISTINCT Returns only unique records based on the fields specified in the SELECT statement
DISTINCTROW Returns only unique records based on all fields, even those not listed in the SELECT statement
TOP Returns the first n records or the top p percentage of records of the selected recordset

The default predicate for a SELECT statement is ALL, returning all records that meet the conditions of the query.

The DISTINCT predicate can be used to limit the result set to unique combinations of the specified fields. The following example returns a list of the unique states to which orders have been shipped.
	SELECT DISTINCT ShipState FROM Orders;
If there is more than one field then only records unique across all designated fields are included in the result set. The following example would return a recordset showing a unique list of which products and states among the orders.
	SELECT DISTINCT ShipState, ProdID FROM Orders;
In contrast, the DISTINCTROW predicate omits data based on entire duplicate records. Let's say you have somehow have duplicate customer records in a table. You can use DISTINCTROW to get a unique list of customers, as in the following example:
	SELECT DISTINCTROW CompanyName FROM Customers;
The TOP predicate is used in conjunction with an ORDER BY clause in two ways:
  • TOP n records
    	SELECT TOP 10 CustID, TtlOrderAmt
    	FROM Orders
    	ORDER BY TtlOrderAmt DESC;
      
    TOP does not necessarily imply "higher" or "more." Think of it as the "first n records" of the result set. For this reason, it's important to sort the result set accordingly.

  • TOP p PERCENT of the entire recordset
    	SELECT TOP 10 PERCENT CustID, TtlOrderAmt
    	FROM Orders
    	ORDER BY TtlOrderAmt DESC;
      
    TOP p PERCENT operates similar to the TOP n predicate, except the number of records isn't fixed.


Reliable Spreadsheet Solutions - Call Now 281-379-2000 !

Something to add? Let me know.

Beyond Technology Custom Application Development
  home Microsoft Excel VBA Custom Application Development  professional consulting Insight for Microsoft Excel POWER-Users  developer tips Visitors Comments  visitor kudos Beyond Technology - Microsoft Solution Provider  e-mail  

Your suggestions and comments are greatly appreciated. Please keep them coming.

Mail to:   rodney@beyondtechnology.com

All terms, product designs, and company names used in this site may be trademarks or registered trademarks of their respective owners, and are hereby acknowledged. © 1996 - 2010 Beyond Technology. All rights reserved.