home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / TI2961.ZIP / TI2961.ASC < prev   
Encoding:
Text File  |  1996-06-17  |  2.1 KB  |  55 lines

  1.    NUMBER  :  2961
  2.   PRODUCT  :  Delphi
  3.   VERSION  :  All
  4.        OS  :  Windows/Win32
  5.      DATE  :  June 14, 1996                           
  6.  
  7.     TITLE  :  SQL: Sorting on a Calculated Column
  8.  
  9. At times, a given data schema will require that a data set will need to be
  10. ordered by the result of a calculation. In Delphi applications using SQL,
  11. this is possible, but the methodlogy varies slightly depending on the
  12. database type used.
  13.  
  14. For local SQL involving Paradox and dBASE tables, the calculated field
  15. would be given a name using the AS keyword. This allows the calculated
  16. field to be referenced for such purposes as setting a sort order with an
  17. ORDER BY clause in an SQL query. For example, using the sample table
  18. ITEMS.DB:
  19.  
  20.   SELECT I."PARTNO", I."QTY", (I."QTY" * 100) AS TOTAL 
  21.   FROM "ITEMS.DB" I
  22.   ORDER BY TOTAL
  23.   
  24. In this example, the calculated field is designated to be referred to as
  25. TOTAL, this column name then being available for the ORDER BY clause for
  26. this SQL statement.
  27.  
  28. The above method is not supported for InterBase. It is still possible,
  29. though, to sort on a calculated field in InterBase (IB) or the Local
  30. InterBase Server tables. Instead of using the name of the calculated
  31. field, an ordinal number representing the calculated field's position in
  32. field field list is used in the ORDER BY clause. For example, using the
  33. sample table EMPLOYEE (in the EMPLOYEE.GDB database):
  34.  
  35.   SELECT EMP_NO, SALARY, (SALARY / 12) AS MONTHLY
  36.   FROM EMPLOYEE
  37.   ORDER BY 3 DESCENDING
  38.  
  39. While IB or LIBS tables require this second method and cannot use the
  40. first method described, either of the two methods can be used with local
  41. SQL. For example, using the SQL query for the Paradox table and adapting
  42. it to use the relative position of the calculated field rather than the
  43. name:
  44.  
  45.   SELECT I."PARTNO", I."QTY", (I."QTY" * 100) AS TOTAL 
  46.   FROM "ITEMS.DB" I
  47.   ORDER BY 3
  48.  
  49.  
  50.  
  51. DISCLAIMER: You have the right to use this technical information
  52. subject to the terms of the No-Nonsense License Statement that
  53. you received with the Borland product to which this information
  54. pertains.
  55.