home *** CD-ROM | disk | FTP | other *** search
/ Computer Based Training: …, Windows 95 & NT Basics / a1454501.iso / mo97a03 / AA02.TXT < prev    next >
Encoding:
Text File  |  1997-05-06  |  13.8 KB  |  279 lines

  1. When defining queries you sometimes won't be interested in individual records in your database.
  2. |
  3. Instead you might wish to see, for example, sums or averages of different groups of data.
  4. |
  5. A @Rtotal@r query is a type of select query that you can use to calculate totals in a recordset.
  6. |
  7. Suppose you are interested in the total revenue generated by each of Centurion's products.
  8. |
  9. You decide to define a new query in Design view to create the recordset you require.
  10. |
  11. At this stage you have included the following fields in the query:
  12.  
  13. ò Name (from the Products table)
  14. |
  15. ò Unit Price (from the Products table)
  16. |
  17. ò Units sold (from the Sales table)
  18. |
  19. ò an expression multiplying Unit Price
  20.   with Units sold
  21. |
  22. Notice that you can change a field heading that is shown in the recordset by typing the name you require, followed by a colon, in front of the field name (or expression) in the design grid.
  23. |
  24. This is useful for making recordset column headings more descriptive, or for naming calculated fields.
  25. |
  26. You can see the recordset resulting from the current design of this query by clicking the @UView@u button.
  27. |
  28. The recordset shows individual sales (in Units sold) of each product, and indicates the revenue generated by each sale.
  29. |
  30. You can now group all the occurrences of each Product Name, and view the total number of units sold and the total revenue generated by each group.
  31. |
  32. To do this you click the @UTotals@u button in the Design view of the query.
  33. |
  34. A new row - the Total row - is now shown above the Sort row in the design grid.
  35. |
  36. By default, the Total row contains "Group By" entries in each field.
  37. |
  38. The records will be grouped according to the entries in each field when you run the query.
  39. |
  40. <f>Now the recordset shows one row for each unique set of values - but no totals are calculated.
  41. |
  42. You can replace Group By in any field with a total function that you select from the appropriate drop-down list in the design grid.
  43. |
  44. Alternatively, you can choose the total function you want by typing its name in the Total row in the design grid.
  45. |
  46. Here, you require the Sum function.
  47. |
  48. Each Product Name entry in the recordset now represents a group of records having the same product name.
  49. |
  50. Also shown are the total number of units sold (SumOfUnits sold) and the total revenue generated (SumOfRevenue) for each group.
  51. |
  52. In addition to Group By and the nine total functions, there are two more options available in the drop-down list:
  53.  
  54. ò Expression
  55. |
  56. ò Where
  57. |
  58. Expression allows you to create an expression, in the Field row, that uses one or more of the listed total functions.
  59. |
  60. You can use Where, together with criteria you set in the Criteria row, to filter out certain records from the groups in your query.
  61. |
  62. For example, you might be interested in the number of units sold and the income generated by the products AutoPay and Timescape together.
  63. |
  64. Running the query now returns the information you require, without showing the Product Name field.
  65. |
  66. If you want the product names to appear in the query's results, you must add the field to the design grid again, with the @UShow@u checkbox checked.
  67. |
  68. Now the Product Name groups are shown separately in the datasheet.
  69. @@
  70. A @Rsummary@r query is a total query that you can create using the Simple Query Wizard.
  71. |
  72. You click the @UNew@u button on the Queries tabbed page to open the New Query dialog box.
  73. |
  74. First you choose the fields you want in your query before proceeding to the next step in the wizard.
  75. |
  76. Now you can choose between a Detail and a Summary query.
  77. |
  78. The @USummary Options@u button becomes available when you click the @USummary@u radio button.
  79. |
  80. When you click the @USummary Options@u button, you have a choice of summary values Access can calculate for you for any numerical field.
  81. |
  82. As well as selecting one or more summary values to calculate for a field, you can get Access to perform a count of records in the source table(s).
  83. |
  84. You click @UOK@u to return to the query wizard.
  85. |
  86. Then you click @UNext@u to continue.
  87. |
  88. Now you can give the query a title, and click the @UModify the query design@u radio button to view its design.
  89. |
  90. When you click @UFinish@u, Access will take you to the Design view of the summary query.
  91. |
  92. As you can see, your summary query is just like a regular total query.
  93. |
  94. In the Datasheet view, you can now see the total number of units sold for each product group, and the number of sales of each product.
  95. @@
  96. A @Rcrosstab@r query is a special type of total query that allows you to see calculated values arranged in a spreadsheet-like format.
  97. |
  98. For example, you could use a crosstab query to summarize the number of units of each product you sold to each of your customer companies.
  99. |
  100. You can easily create a crosstab query by double-clicking the @UCrosstab Query Wizard@u in the New Query dialog box.
  101. |
  102. First you choose a table or query that contains the fields you want to use in the crosstab query.
  103. |
  104. If you wish to use fields from more than one table, you should use an existing select query containing the fields you require.
  105. |
  106. You double-click the required query name (or select the query and click @UNext@u) to continue.
  107. |
  108. Now you choose the field(s) that will provide row headings for the crosstab query.
  109. |
  110. To select a field, you highlight it and move it across with the right-arrow button (alternatively you can double-click the field).
  111. |
  112. Then you click @UNext@u to continue.
  113. |
  114. Now you select the field that will provide values for the column headings, before clicking @UNext@u again.
  115. |
  116. Although you can select up to three fields to provide row headings, only one field can be used to provide column headings.
  117. |
  118. Then you decide on a calculated number to be included in the body of the crosstab table.
  119. |
  120. You select a field and a function from the appropriate panes in the dialog box.
  121. |
  122. If you don't need row sums, you can choose not to include them before clicking @UNext@u.
  123. |
  124. A row sum summarizes the calculated entries in each row of a crosstab table by adding them together.
  125. |
  126. Finally you can name the query and click @UFinish@u to view the results.
  127. |
  128. In the Datasheet view, you can see at a glance the number of units of each product sold to each customer company.
  129. |
  130. In Design view, you can identify the query as a crosstab query by the presence of Total and Crosstab rows in the design grid, and by the icon on the @UQuery Type@u button.
  131. |
  132. Since the Crosstab Query Wizard has already saved the query, you can click the @UClose Window@u button to return to the Database window.
  133. @@
  134. You can also create a crosstab query without using a wizard.
  135. |
  136. You can use the design grid to start from scratch, or change an existing query (such as City Sales) into a crosstab query.
  137. |
  138. City Sales is currently a select query that returns this recordset.
  139. |
  140. To convert it to a crosstab query, you choose @UCrosstab Query@u from the @UQuery Type@u button's drop-down list.
  141. |
  142. Alternatively, you can choose @UCrosstab Query@u from the @UQuery@u menu.
  143. |
  144. Now the Total and Crosstab rows are visible in the design grid.
  145. |
  146. You use the drop-down list in the Crosstab row of each field to decide which part of the crosstab table that field's entries will form.
  147. |
  148. Each column heading must have Group By as the setting in the Total row.
  149. |
  150. Any field that provides row headings can have a Group By setting, one of the Total functions, or an expression that includes a total function.
  151. |
  152. For your crosstab query to work, you must specify one field as a value (as well as having one column heading and at least one row heading).
  153. |
  154. The field containing the Value setting in the Crosstab row of the design grid must contain a total function (or an expression using a total function).
  155. |
  156. Since you do not wish to use the calculated field, you can highlight it, and press the @UDelete@u key to remove it from the design grid.
  157. |
  158. Now you can click the @UView@u button to see the results of your crosstab query.
  159. |
  160. Notice that entries in the Name and Unit Price fields provide the row headings, while entries in the City field are the column headings.
  161. |
  162. When you click the @UClose@u @UWindow@u button, you are prompted to save changes to the design of your query.
  163. |
  164. On the Queries tabbed page, the icon next to City Sales indicates that it is now a crosstab query.
  165. @@
  166. Suppose you have a select query (called Products Query) that returns this recordset.
  167. |
  168. You decide to create a crosstab query based on this query to summarize the number of units of each product sold to your customers.
  169. @@
  170. An @Raction@r query is a query that makes changes to many records in one operation.
  171. |
  172. There are four types of action query:
  173.  
  174. ò make-table queries
  175. |
  176. ò append queries
  177. |
  178. ò delete queries
  179. |
  180. ò update queries
  181. |
  182. You might find that you often have to execute a particular select query against data that isn't changing.
  183. |
  184. In such a case, it could be useful to save the recordset as a new table to give you faster access to the data.
  185. |
  186. You can create a new table by changing the select query to a @Rmake-table@r query, and then running the converted query.
  187. |
  188. To convert this select query to a make-table query, you choose @UMake-Table Query@u from the @UQuery@u menu.
  189. |
  190. Alternatively you can choose @UMake-Table Query@u from the @UQuery@u @UType@u button's drop-down menu.
  191. |
  192. Then you type a name for the new table in the Make Table dialog box.
  193. |
  194. You can choose to add the new table to the current database or another one before clicking @UOK@u to return to the Design view of the query.
  195. |
  196. You can recognize this query as a make-table query by the icon on the @UQuery Type@u button.
  197. |
  198. Now you can make the new table by clicking the @URun@u button on the toolbar.
  199. |
  200. You could also select @UQuery -@u @URun@u to create the new table.
  201. |
  202. Access asks you to confirm that you want to create a new table from the selected records by clicking @UYes@u.
  203. |
  204. You can now see the new table if you view the Tables tabbed page in the Database window.
  205. @@
  206. An @Rappend@r query adds a group of records from one or more tables to the end of a table.
  207. |
  208. Suppose you have recently added a number of new records to the table called Sales.
  209. |
  210. You can use an append query to add the new data to the table Sales Jan 97.
  211. |
  212. Again, it is convenient to convert an existing query into the type you require.
  213. |
  214. You can use the @UQuery Type@u button (or the @UQuery@u menu) to select @UAppend Query@u.
  215. |
  216. Now you click @UOK@u in the Append dialog box to accept the name of the table to which you want to add the records.
  217. |
  218. Before you run the query, you must first add a field to the design grid that you can use to select which records to append.
  219. |
  220. You drag the field you want to use for setting criteria from the appropriate list to the design grid.
  221. |
  222. Then you type the condition(s) on which additions will be made in the Criteria cell of the field.
  223. |
  224. You can preview the records that the query will append in Datasheet view before running the query.
  225. |
  226. Then you return to Design view, and choose @UQuery - Run@u (or click the @URun@u button on the toolbar).
  227. |
  228. You click @UYes@u to confirm that you wish to append the new rows to the chosen table.
  229. |
  230. Then you can return to the Database window and open the Sales Jan 97 table to view the appended records.
  231. |
  232. The new records have been added at the end of the table.
  233. @@
  234. Suppose you wish to discontinue one of your products, and recall recent sales of that product to customers.
  235. |
  236. You can use a @Rdelete@r query to remove records relating to that product from one or more of your tables.
  237. |
  238. You begin by creating a select query in Design view, where you include the tables containing records you want to delete.
  239. |
  240. Then you select @UDelete Query@u from the @UQuery Type@u button's menu.
  241. |
  242. Now you insert all the fields from the table with related records in the design grid.
  243. |
  244. Access automatically inserts the word "From" in the Delete row.
  245. |
  246. In the design grid, you also need to select the field(s) from the primary table on which you want to set criteria.
  247. |
  248. Access inserts the word "Where" in the Delete row, and you can type the name of the product you want to delete from the tables at the Criteria row.
  249. |
  250. You can use the Datasheet view to check which data will be deleted when you run the query.
  251. |
  252. When you are satisfied that the query will select the correct records to delete, you can return to Design view and click the @URun@u button.
  253. |
  254. You cannot undo a delete action, so Access asks you to confirm that you want to delete the records.
  255. |
  256. If the related table is open when you run the delete query, you can use the @UWindow@u menu to view it.
  257. |
  258. The table now shows where the deleted records used to be.
  259. |
  260. When you next open the table there is no trace of the deleted records.
  261. |
  262. However, records meeting the criteria you specified in the design grid still remain in the primary table.
  263. |
  264. Access cannot delete records from the primary table and related tables at the same time - you must remove the related table and run the query again.
  265. |
  266. You take away the selected table by choosing @URemove@u @UTable@u from the @UQuery@u menu.
  267. |
  268. Now you click the @URun@u button again to delete the records from the primary table.
  269. |
  270. If you run a delete query on a single primary table (that is, a "one" table in a one-to-many relationship) first, Access automatically deletes related records in other tables if the relationships have referential integrity enforced and cascading deletes enabled.
  271. |
  272. You click @UYes@u to proceed when the confirmation message is shown.
  273. |
  274. The Products table (which was open when you ran the query) now indicates that the record has been deleted.
  275. @@
  276. The fourth type of action query is an @Rupdate@r query - which allows you to make global changes to a group of records in one or more tables.
  277. |
  278. Suppose you wish to modify entries in the Unit Price field of your Products table to reflect a global price increase of five percent.
  279.