* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Introduction to MS Query - Hutchins Central
Survey
Document related concepts
Microsoft Access wikipedia , lookup
Relational algebra wikipedia , lookup
Functional Database Model wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Clusterpoint wikipedia , lookup
Versant Object Database wikipedia , lookup
Transcript
Introduction to MS Query Prepared by Ian MacRae Introduction to MS Query Table of contents 1 2 3 3.1 4 4.1 4.2 4.2.1 4.2.2 4.3 4.4 4.5 5 5.1 5.2 5.2.1 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.2.10 5.2.11 5.2.12 5.2.13 5.2.14 5.2.15 5.2.16 5.3 5.4 6 6.1 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.3 7 7.1 Introduction Learning outcomes Database fundamentals Database concepts The Synergetic system Extracting data from Synergetic The Synergetic databases Tables in the Synergetic databases Database views Finding fields in views Security in Synergetic Activity: The system database views report MS Query basics Extracting data using the Microsoft Query wizard Extracting data without using the Microsoft Query wizard Start a new query Remove tables or views Adding fields to the query Renaming fields Removing fields Sorting records Resizing rows and columns Adding criteria Filtering student data by year and term More on filters Return the Query data to Excel Connection properties Unlink option Returning to MS Query to edit a query Data properties External data connections disabled Parameters Activity: Updating your example Microsoft Word mail merge Creating labels from a spreadsheet Creating letters from a spreadsheet Create the initial mail merge letter Fixing the date format Missing email addresses Using a gender specific pronoun Fix possessive nouns where the name ends in s Other merge field switches Fixing a broken source file More advance MS Query SQL 1 1 1 1 2 2 3 4 6 7 7 8 9 9 15 15 17 17 17 18 18 18 19 20 21 21 21 22 24 24 26 26 28 29 29 35 35 37 39 40 41 43 43 45 45 © The Hutchins School 2016 Introduction to MS Query 7.1.1 7.2 7.3 7.3.1 7.3.2 7.4 7.4.1 7.4.2 7.5 7.5.1 7.5.2 7.5.3 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.6.5 7.7 8 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.1.5 8.2 8.3 9 9.1 9.1.1 9.1.2 9.1.3 9.2 9.3 9.4 10 10.1 Sample SQL statements Using an existing SQL statement in MS Query Using Crystal Report queries Dates in queries The StudentActiveFlag Adding other views and tables Add in additional table to Exercise 1.xlsx Linking tables or views Joining tables Inner joins Outer joins Activity: Joining tables Finance tables/views Using finance views Activity: Joining finance tables Advanced criteria Joining finance and non-finance tables Payroll views Using tag lists in a query Pivot tables and charts Summarising student results Preparation Create a PivotTable Link a PivotChart to the PivotTable The slicer Answering the questions Adding parameters to query Refreshing PivotTables and PivotCharts Future students Future student based pivot tables Create the future student query Create a PivotTable with the summary of statuses Create a PivotTable with the summary of application priorities Enquiries Future student activity Future status changed Appendix – Additional resources Lynda.com 45 46 47 50 50 51 51 52 53 53 55 57 58 58 62 63 64 64 65 66 66 66 67 71 72 73 74 74 75 75 75 75 76 76 78 78 79 79 © The Hutchins School 2016 Introduction to MS Query Table of figures Figure 1: Copy grid contents to Excel ........................................................................................ 3 Figure 2: Synergetic MS Query Application Programing Interface (API) ................................... 4 Figure 3: Extract of StudentYears table..................................................................................... 5 Figure 4: Extract of StudentContacts table ............................................................................... 5 Figure 5: Extract of Community table ....................................................................................... 6 Figure 6: Extract of Addresses table .......................................................................................... 6 Figure 7: Synergetic table linking example ................................................................................ 6 Figure 8: System database views reports .................................................................................. 7 Figure 9: Query wizard with selected columns........................................................................ 11 Figure 10: Query wizard: Filter data ........................................................................................ 12 Figure 11: Manual tray option for laser printers ..................................................................... 31 Figure 12: Gender specific pronouns ....................................................................................... 40 Figure 13: Complex criteria section ......................................................................................... 63 Figure 14: luFutureStatus merge codes .................................................................................. 78 Figure 15: luFutureStatusMerge settings................................................................................ 78 © The Hutchins School 2016 Introduction to MS Query 1 Introduction Microsoft Query is a tool that allows you to retrieve data from databases such as the ones used by Synergetic into Microsoft Office programs like Word and Excel. This data can then be reused by these programs for multiple uses including mail merges and analysis. This course assumes a basic understanding of the Microsoft Office Word and Excel products and a proficient understanding of Synergetic. This session covers how to create simple database queries using MS Query. It covers the basic database concepts of tables, views, joining tables, and how to put them together to construct more complex queries. Finally, this session covers how to retrieve the data for mail merges, pivot tables, and pivot charts. 2 Learning outcomes At the conclusion of this session, participants should be able to: • • • • • • • • • • • • • 3 Understand the benefits and disadvantages of MS Query when compared to other tools like Crystal Reports. Know how Word and Excel integrate with MS Query. Understand the purpose of MS Query to extract data from the database. Recognise views and other base tables. Understand Synergetic naming conventions for views, tables and fields. Know how to create a query using the Query wizard. Know how to create a query without using the Query wizard. Change a query. Understand the basics of an SQL statement. Copy SQL statements from a query to email these statements to IT support. Copy SQL statements from IT support back into a query. Create a mail merge using retrieved data in Microsoft Word. Create PivotTables and PivotCharts from retrieved data in Microsoft Excel. Database fundamentals A database is a set of data that is organised in such a way that related data is linked and organised. These are usually organised into tables and accessed through queries and views. Modern Database Management Systems (DBMS) provide functions that allow for the entry, storage, and retrieval of large quantities of information. When people refer to ‘database’ they usually mean a database management system. For example, when referring to the Synergetic ‘database’, the reference is to the Synergetic application server, the database server, the databases on that server and the management application joining all that together. 3.1 Database concepts If you have ever used a spreadsheet, you have used a simple database. A spreadsheet is a special type of database called a ‘flat file database’. Database fundamentals Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 1 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query Keeping with the spreadsheet analogy, information in a database is stored in tables. A table can be likened to a tab in a spreadsheet. A table consists of ‘fields’ (columns) and ‘records’ (rows). A simple phonebook directory (such as the Hutchins phonebook) is similar to a database table. Table 1: Simple phonebook directory Surname Title Initials Address HomePhone MobilePhone Adams Mr JM 9876 5432 0414 300 300 Adams Mrs AF 21 Minerva Street, Doncaster 5 High Street, 9123 4567 0321 111 111 Eastbourne 303 Burwood Hwy, Burwood East Maintown 2 Main Street, 9000 9800 Adamski Mr G Brown Ms JAD Bruce Mr M 8989 2001 0422 232 232 2/53 Dixon Ave, 9880 2222 045 345 344 Ringwood In the table above each column is a field, and each row (except the header) is a row is a record or entry in that table. If you wanted to store some more information about a person in a spreadsheet, you would probably create a new tab, copy the name and store that information there. This is where databases are very different. The purpose of modern databases is to store the information only once, and then reference it everywhere else. 4 The Synergetic system Synergetic consists of multiple components: 4.1 A database management system storing the Synergetic databases (four main databases). The Synergetic windows application and Synergetic web application; components that allow users to view, and edit the information in the Synergetic databases. Extracting data from Synergetic There are three methods of extracting data from Synergetic 1. Excel spreadsheets may be generated from any grid in Synergetic by right clicking in any grid and selecting Copy Grid Contents to Excel see Figure 1. 2. Crystal reports are generated through one of the Crystal Reports Available menu options. The Synergetic system Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 2 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 3. Using MS Query to formulate a customised query to extract data into a spreadsheet. The first two techniques allow you do generate pre-formatted lists or summary documents quickly and simply, but these fields are fixed. The columns exported by the Copy Grid method may not contain the fields that you need. These fields are determined by the Synergetic developers and cannot be changed. Crystal reports fields may be modified by the IT staff but this is time consuming and requires rigorous testing. Changing these reports affects all users of these reports. If this change is beneficial for all users it is worth doing, but if not, then alternatives should be investigated. This is where using MS Query to create ad-hoc reports is useful. Figure 1: Copy grid contents to Excel 4.2 The Synergetic databases Synergetic is based on three main databases: SynergyOne SynergyOneFinance SynergyOneMedia In Hutchins’ case, there is an additional database for the Foundation, SynergyOneFoundation. The Synergetic client and web client access these databases through an interface to provide the information requested. MS Query operates outside of that interface so it is necessary to understand the underlying workings of the database to be able to build the queries and obtain the correct results. The Synergetic system Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 3 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query Excel Spreadsheet (Copy Grid Contents to Excel) Synergetic Windows Application Crystal Report Excel Spreadsheet (Crystal Reports Available) Synergetic Web Application MS Query Synergetic Databases Figure 2: Synergetic MS Query Application Programing Interface (API) 4.2.1 Tables in the Synergetic databases The Synergetic databases contain thousands of tables, each storing a specific aspect of information. To extract any useful information it is usually necessary to use more than one table. For example, in order to send a letter home to all parents of Year 11 students you would need information from several tables. The StudentYears table would be required to determine if the year level the student was enrolled in in that calendar year. Figure 3 below shows the student’s progression through the year levels and campuses over the calendar years. The Synergetic system Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 4 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query FileYear ID StudentCampus YearLevel House 2006 31398 E 2 ST 2007 31398 J 3 ST 2008 31398 J 4 ST 2009 31398 J 5 ST 2010 31398 J 6 ST 2011 31398 M 7 ST 2012 31398 M 8 ST 2013 31398 S 9 ST 2014 31398 S 10 ST Figure 3: Extract of StudentYears table The StudentContacts table stores information about the contacts for each student and who should receive mail and reports. ID ContactType LinkedID PrimaryOnly Flag Normal MailFlag Reports ParentF LivesWith Flag lag Flag 31398 SC1 31399 0 1 1 1 1 31398 SCT 31399 0 1 1 1 0 31398 SE1 43704 1 0 0 0 0 32029 SC1 24245 0 1 1 1 1 32029 SCT 24245 0 1 1 1 0 32029 SE1 45291 0 0 0 0 0 32029 SE2 47821 1 0 0 0 0 32105 SC1 32106 0 1 1 1 1 32105 SC2 54784 0 0 1 1 0 Figure 4: Extract of StudentContacts table The Community table is needed to determine the names that match the IDs, which people are spouses, and to find which addresses people live at. The Synergetic system Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 5 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query Figure 5: Extract of Community table Finally, the Addresses table is needed as this contains the actual address. Figure 6: Extract of Addresses table To get the data from multiple tables they need to be joined. To join tables a linking field needs to be specified. For example, ID 31398 in the StudentYears table needs to be linked to ID 31398 in the StudentContacts table. Therefore, the StudentYears and StudentContacts tabled are joined by the ID fields. Similar link specifications are required for the Community and Addresses tables. Figure 7: Synergetic table linking example 4.2.2 Database views Linking tables is time consuming and prone to errors. Synergetic have therefore provided a set of views to make querying the database an easier process. The Synergetic system Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 6 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query A view can be considered to be a super table that combines multiple base tables into one big table. The information in these views is from individual fields and from generated fields. Generated fields can be from more base tables that have been combined and manipulated to form more fields. These can become incredibly complex. The views provided by Synergetic have been designed to accommodate to the most common queries. The course will be focusing almost exclusively on the views (with two exceptions). For example, the view vStudentContactAddress combines all the information needed to generate a mailing list to the contacts of Year 11 students. It contains fields that are: 4.3 Extracted directly from the base tables such as FileYear and ID. Fields that are used directly, but renamed to make them cleared in their current context such as StudentContactSurname and StudentContactEmail. Generated fields such as StudentContactAddressFull and StudentContactMailSalutation. o StudentContactAddressFull is a combination of individual fields from the Addresses table but put together in a mailing appropriate format. o StudentContactMailSalutation combines the title of a person with their surname. If this person has a spouse and the address to primary only flag is not selected within Synergetic’s Community Maintenance, their title will be joined with their partner’s, for example Mr and Mrs Johnson. Finding fields in views Synergetic has hundreds of views and thousands of fields. The biggest hurdle is to know where to start and what view to use. Synergetic has provided two helpful reports that can be used to find fields in a view. These can be found in System System Crystal Reports Available Figure 8: System database views reports 4.4 Security in Synergetic As with all parts of Synergetic, access to views are also restricted by security permissions. Not all views will be available to all users. If a particular view is required, please consult with the IT department. Even if the view is restricted by security permission, its existence will be shown in the reports. The Synergetic system Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 7 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 4.5 Activity: The system database views report In Synergetic select System System Crystal Reports Available 1. Search for future in the view name. 2. Search for email in the field name. 3. Search for past in the view name and email in the field name. 4. What view might you use to retrieve all future student applications for the next enrolment year? 5. What view might you use to retrieve a list of all past students for a reunion who live in a particular post code? 6. What view might you use to obtain a list of students who have a public medical alert and include the public medical alert comment? The Synergetic system Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 8 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5 MS Query basics There are two ways that MS Query extracts information from a database Using the MS Query wizard. Directly through MS Query without using the MS Query wizard. Both options will be demonstrated, but the majority of the time we will be using the second option. It is always a good idea to have some expectation of the results of a query, for example, the number of results that should be returned. 5.1 Extracting data using the Microsoft Query wizard All MS Query actions are performed in Microsoft Excel. In this example, the MS Query Wizard will be used to create a query to obtain the students names and their contacts’ address details. 1. Start Microsoft Excel. 2. Ensure you have the first, top left cell (cell A1) selected 3. In Excel choose Data From Other Data Sources From Microsoft Query. 4. On the Choose Data Source screen, select the SynergyOne datasource. Make sure that the Use the Query Wizard to create/edit queries option is checked. Click OK. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 9 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5. Scroll down the list of tables until you get to the views. All views start with the lowercase ‘v’. Click the ‘+’ to the left of vStudentContactAddress to list all the fields in this view. By default, MS query shows all tables, including the information schema. These tables are of no use to us. To filter these out select the Options button and select dbo in the owner field. Click on OK. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 10 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 6. To select fields, highlight them, then click the > button. If you click this button when the view is selected, all fields will be added to the query. You very rarely want this. 7. Select the fields as shown in Figure 9. Figure 9: Query wizard with selected columns 8. When finished selecting, click Next. You can come back to this screen at any time while still in the wizard by selecting the Back button. 9. The next step is to filter the data selection to be returned to Excel. In most cases with current students this will be for the current year and semester. One way is to set the FileYear and FileSemester to the current year and term. Set these as per Figure 10. The terminology ‘semester’ and ‘term’ are interchangeable within Synergetic. Their use is inconsistent. Most of the time, the field FileSemester refers to the school term. The biggest exception to this is in the Sports module. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 11 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query Figure 10: Query wizard: Filter data Dropping down the right hand box will show you a list of all the values that are used for the selected field. When you choose a filter, the column name becomes bolded to let you know that there is a filter applied to that field. Click Next when done. 10. The next step is to set the sort order. You can choose to sort by multiple fields. Under Sort by select Surname. Leave the sort type as ascending. Under Then by select Given1 and ascending to further sort students with the same surname by their given names. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 12 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query When using the query wizard you can sort by a maximum of three fields. If you do not use the wizard, you can sort and group by an unlimited number of fields. Click Next to continue. 11. The last screen in the query wizard asks if you want to return the data to Excel or use MS Query to further edit the query. For this example, choose the option to Return the data to Microsoft Office Excel. Click Finish. 12. MS Query will then query the database based on the parameters you supplied and return back to excel where you will be asked where you want to place the retrieved information. You can position the information anywhere you want on the spreadsheet. For now, click OK. This will place the results on the spreadsheet, starting at the cell you were on when you entered MS Query. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 13 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 13. In Excel, save the workbook. Give the spreadsheet the name Exercise 1.xlsx. Have a look at the results. Where these what you expected? Did you expect this many results? What are all these unknown addresses? MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 14 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5.2 Extracting data without using the Microsoft Query wizard This wizard is only useful in the most basic of cases. Often it can mislead and make it harder to manipulate the data. It does not allow for the full functionality of MS Query. Everything that can be done in the wizard, can be done in MS Query itself, and more. For this reason, most prefer to not use the wizard. We are going to repeat the previous example, but this time we are not going to use the wizard. 5.2.1 Start a new query 1. Start Microsoft Excel or create a new workbook 2. In Excel choose Data From Other Data Sources From Microsoft Query 3. This time uncheck the Use the Query Wizard to create/edit queries checkbox. Ensure that SynergyOne is selected and click OK. 4. In the Add Tables window, set the Owner to dbo. Since we are also only using views, you may wish to click the options button and unselect everything but views as well. Scroll down in the Add Tables window until you get to the Synergetic Views. These all start with a lowercase ‘v’. Double click the view name vStudentContactAddress to add the view to the query. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 15 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5. At this point you may continue to add additional tables/views. We are only using the one view. Click Close to continue. Resize the window to view more fields. Click and drag the middle separator to about half way down, then drag the window to show more fields MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 16 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5.2.2 Remove tables or views If you wish to remove a table or view from the query, click a field in the table or view and press the Delete key on the keyboard 5.2.3 Adding fields to the query There are several ways to add fields to a query. Choose the technique you find the easiest. 6. Add the following fields: ID Given1 Surname YearLevel StudentGender StudentContactAddressFull StudentContactMailName StudentContactMailSalutation FileYear FileSemester CurrentSemesterOnlyFlag StudentContactNormalMailFlag You can add fields by: a) Double clicking the field in the table window. b) Click and drag the field from the table window to the grid column position where you want to place it. c) Click the drop down list in a blank column heading to display a list of all fields then select the field you want. You can also type the field name in the column heading to go straight to the field name you are looking for. d) Click Records Add Column and then select the field you want. Click on Add. 5.2.4 Renaming fields When adding a field through the Records Add Column method you can optionally give it a different heading name by entering it in the column heading field. The heading names of other fields can also be changed by double clicking on the column’s heading (grey area) at the top of the grid. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 17 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5.2.5 Removing fields To remove a field, click the column heading (grey area) so that the column is highlighted and press Delete on the keyboard. 5.2.6 Sorting records 7. Sort the records by clicking on Records Sort. Select the field to sort on (Surname), then click Add. To sort students with the same surname by given name, select Given1 from the Column drop-down and click Add. Multiple sort selections can be made, but the fields must already have been added to the query. 5.2.7 Resizing rows and columns 8. Some cells require more height and width to view the information. This is done in the same manner as an Excel spreadsheet. Click and drag the row and column divider in the grey areas to make them bigger. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 18 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5.2.8 Adding criteria 9. The next step is to add criteria (filters) to the query. Click Criteria Add Criteria. The drop-down for Field here lists all fields in the tables and views added as opposed to the wizard which only lists those that have been added to the query. Selecting Criteria Add Criteria when a cell in the grid is selected will populate the fields in the Add Criteria screen with the column name and value from the selected cell. As with the previous example, we will limit the results to those which we are interested in. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 19 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5.2.9 Filtering student data by year and term When dealing with student data we must first select which year and term’s data we wish to use. You can retrieve only one term’s data in one of two ways: a. Filter on FileYear and FileSemester giving specific values for the term and year you want. This is useful for retrieving information from a past or future reporting period. The criteria being filtered on is shown in the centre of the screen. b. Filter on CurrentSemesterOnlyFlag. The CurrentSemesterOnlyFlag indicates that data is from the current semester. Filtering on it allows the query to always have only the current term’s information without needing to change the year or semester values from one year to the next. 10. Add a filter to limit the records to those where the CurrentSemesterOnlyFlag is equal to 1. 11. Add a filter limiting students to boys only (Hint: Examine the StudentGender field). MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 20 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5.2.10 More on filters So far when adding filters we have left the And button selected. In some cases, you may wish to use the Or for one of the criteria. For example: c. YearLevel = 7 Or YearLevel = 9; will select student from both year 7 and year 9 d. FileYear = 2016 And FileSemester = 1; will select only students who exist in both 2016 and in semester 1 Clicking the Values button will list all values that have been used for this field. Once you have finished adding criteria, click Close. If ever the criteria are not visible, select the View Criteria so that there is a tick next to it 5.2.11 Return the Query data to Excel 12. Click File Return Data to Microsoft Office Excel and save your spreadsheet. Alternatively click on the 5.2.12 button. Connection properties There are several settings that are saved with the query. These can be found under Data Refresh All Connection Properties. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 21 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query You may choose to rename this connection. This is useful when there are many connections within the same workbook. You may also choose to have the data refresh automatically when this file is opened. 5.2.13 Unlink option There are times when you wish to disconnect the table from the server so that any changes made in Synergetic are not reflected in the spreadsheet. This means that you will not be able to refresh the data in the spreadsheet. This is a permanent change and cannot be reversed but is particularly useful if you need to send the spreadsheet to a third party who does not have access to the source table. (The file will open, but with an error indicating the data source is not contactable.) There are two ways to do this. Option 1 Select Data Connections. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 22 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query In the Workbook Connections window select the relevant connection and click the Remove button. You will be prompted with a warning that this will disconnect the query from the server preventing further updates. Click OK to remove the link. Option 2 Select Design Unlink. You will also be prompted that this will permanently remove the query definition from the sheet. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 23 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5.2.14 Returning to MS Query to edit a query There are many ways to do this. Data Connections Properties button Definition tab Edit Query button Design Properties Connection Properties button Definition tab Edit Query button By far, the easiest method is to right click on a cell in the query data and to select Table Edit Query. 5.2.15 Data properties There are several settings that relate to how your data updates in Excel after you make changes to your query. Select Data Properties. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 24 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query These are discussed in detail below. Include row numbers Checking this item causes a column with the row number to be inserted when the data is retrieved. The row number refers to the row number as it was retrieved from the database, not the row that it is in Excel. Unchecking this option will remove the _RowNum column. Adjust column width When this is checked the columns are resized to fit the data as it is retrieved. When the data is retrieved the columns are resized again to fit the data. When the column is unchecked, the columns are not resized and you have to manually adjust the column width to fit the data. Preserve the column sort/filter/layout When this item is checked, any changes to the query do not change the initial layout of the columns, the sort order or filters applied in excel. If you add columns in the query the Excel will maintain the initial order of the columns and add any additional columns to the right hand side of the spreadsheet if this item is checked, regardless of where they are placed in the query editor. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 25 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query When this item is unchecked any filtering, sort order, or column order is not preserved during any refresh. One reason to turn this off is to rearrange or to insert columns into a queried table. Preserve cell formatting When this item is checked, refreshing the data keeps the cell formatting changes made in Excel. When it is unchecked, the formatting reverts to the formatting of the retrieved data. This checkbox applies only to the cell level, not the column level. Number of rows in data range changes There are three options here and there choice depends on what you want to happen when there are changes in the number of rows returned by the query. 5.2.16 Insert cells for new data, delete unused cells. This is the default and is usually what is needed. Any changes are reflected in the data. All old data is removed and the unused cells are removed. Insert entire rows for new data, clear unused cells. Rather than shifting the cells around, entire rows are shifted. This is more time consuming and tends to break the cell formatting if using the Preserve cell formatting option. Overwrite existing cells with new data, clear unused cells. Ignores any content in the cells. This is useful if you are building a model and you need the data to be refreshed in the exact same cells. External data connections disabled When you open an Excel document that contains a query you may see a warning that external data connections have been disabled. You will need to enable content to refresh the data from an external connection. 5.3 Parameters Parameters are used whenever you want to filter your data on a particular field but the value of that filter needs to change whenever you refresh the data. For example, suppose you want to query the data for a particular year level, but the actual year level retrieved is different each time. This can be achieved by adding a parameter in MS Query. To add a parameter, set the filter to square brackets [] as follows: MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 26 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query The next time the data is refreshed Excel will prompt for a value for the filter as below: You can add distinguishing text label to the parameter by entering in text in the square brackets as a description for the prompt. For example: Later versions of Excel will add a description to a parameter if you do not. It is best practice to add one yourself to avoid confusion. MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 27 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5.4 Activity: Updating your example 1. Add a parameter to your example to find year 10 students. 2. Add a parameter to your example to find parents who live within a 10 km radius of Hutchins. Hint: See website: https://www.freemaptools.com/find-australian-postcodes-insideradius.htm MS Query basics Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 28 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 6 Microsoft Word mail merge Synergetic comes with many different label reports, but most of them are cumbersome and preformatted to specific label formats. Since Avery have updated their label format in recent years it has been difficult to get the new format to fit. Combining the power of a query with the power of a mail merge can save you a lot of time when you want to create labels. All you need to do is import and organise your data, set your stationary up, set your printer up and print. We will go through the process of doing this, except for actual printing. 6.1 Creating labels from a spreadsheet 1. Make sure you have closed Excel. Excel locks files when it has them open and prevents other applications, including Word from accessing them. Close all Excel files first. 2. As per the previous example, we will start by using the wizard. Open Word, click Mailings Start Mail Merge Step by Step Mail Merge Wizard. You can reset a merge document back to a normal document by clicking Mailings Start Mail Merge Normal Word Document. 3. In the mail merge wizard, select Labels and then Next: Starting Document. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 29 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 4. Leave Use current document, but click the Label options link. 5. Select the appropriate stationary to use in the Label Options window and click OK. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 30 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query Figure 11: Manual tray option for laser printers If you set the printer tray to use Manual Feed in Tray 1 (for standard laser printers) as per Figure 11, this will cause the printer to wait for paper to be inserted in the manual feed tray before printing. 6. 7. 8. 9. Select Next: Select recipients. Select Use an existing list… then click on Browse…. Browse to Exercise 1.xlsx. Select the query or table in the spreadsheet to use and click OK. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 31 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 10. The following Mail Merge Recipients window is presented: 11. Select Next: Arrange your labels. 12. The Word document now shows blank labels ready for merge fields to be added to them. Add the merge fields to the first (upper left) label. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 32 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 13. Add the recipient information to your labels through either: a. The More items option. Select the field you want to add in the Insert Merge Field window and click Insert. b. Select Mailings Insert Merge Field and then the field you wish to insert. Add the fields StudentContactMailName and StudentContactAddressFull on the first two lines of the first label. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 33 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 14. Select Update all labels to replicate the arrangement from the first label to all labels. Your labels should have the merge fields in all positions. 15. Select Next: Preview your labels. 16. You can preview different results by using the forwards and backwards buttons in the preview section of the ribbon. 17. Select Next: Complete the merge. Your labels are now ready to be printed or saved. 18. Save the file as Exercise 3.docx. If saving the label template, save it with the Excel spreadsheet. Be aware though that Word does not permit a source file to be references from SharePoint. It must be copied back to a local computer the next time you use it. See Fixing a broken source file below. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 34 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 6.2 Creating letters from a spreadsheet 6.2.1 Create the initial mail merge letter 1. Modify Exercise 2.xlsx query to include: StudentEntryDate, StudentContactDefaultEmail, StudentContactEmail, StudentContactOccupEmail 2. Select all students, including girls. 3. Save and close Excel. 4. Open the file Exercise 3a Template.docx file and save as Exercise 3a.docx. This time, rather than using the wizard, we will start from basics. 5. Select Mailings Select Recipients Use an Existing List…. 6. Browse to Exercise 2.xlsx file and select the query from the list. At this point it appears as if nothing has changed, but in the background Word has prepared to mail merge. It has linked to the file and set up the merge fields. To check select Mailing Edit Recipient List. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 35 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 7. Change the filter here for Year 11 students. 8. Inserting the mail merge fields where appropriate, taking extra care with the given name to not remove the apostrophe. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 36 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 9. Preview the mail merge using the Mailing Preview Results button and using the arrows in the Preview Results pane to move backwards and forwards. There are several areas on concern with this mail merge. 6.2.2 The date format is incorrect. There are missing email addresses. It would be better to use the correct pronoun instead of the generic ‘child’. Some student’s names end in ‘s’ and the apostrophe should not be followed by an s. Fixing the date format The temptation is to return to Excel and fix the date format there. It seems logical that the correct date format in Excel would transfer to Word. Unfortunately, this is not the case. Word completely ignores all formatting when performing a mail merge. To format merge fields, you have to modify the merge field itself. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 37 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 1. Ensure you are not in preview mode. If necessary, deselect the Mailing Preview Results button. 2. Right click on the StudentEntryDate field and select Edit Field. 3. The Fields window allows you to make basic changes to the field, but we want to make more advanced changes. Click the Field Codes button to switch to the fields codes view. 4. In the Field codes text box add the text \@ "dd MMMM yyyy" Make sure there is a space after the StudentEntryDate field and click OK. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 38 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query There are multiple field codes and depend on the type of field you want to format. This particular one formats for the full date. See http://www.ashbee.co.uk/index.php/faq/48-word/97-mail-merge-reformatdate-time-and-number-fields-imported-from-excel for a sample of other codes. 5. Preview your results and scroll through some letters to see the changes. 6.2.3 Missing email addresses Rather than having a blank space when there is no email, it would be better to have a message indicating there is no email on file and to contact the school should this be incorrect. This is done by using a mail merge If … then … else rule. 1. Set your cursor immediately to the right of the StudentContactEmail field and select Mailings Rules If … then … else…. 2. Set the Field name to StudentContactEmail and the Comparison to ‘is blank’. Insert an appropriate text to place if the condition is met and a similar text if the condition is not met. In this case, leave the otherwise section blank. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 39 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 3. Preview your results. Notice how the text only shows on those results where there is no email address. 4. Preview your results and scroll through some letters to see the results. You can also apply a different format to this text. Highlight it and set the format to what you want. 5. Repeat the same procedure for the occupational email address. 6.2.4 Using a gender specific pronoun Instead of using the gender neutral pronoun ‘child’ we can use the If…then…else… rule to specify the gender specific pronoun based on the student gender. 1. In the exercise, delete the word ‘child’ and select Mailings Rules If … then … else…. 2. Complete the Insert Word Field: IF dialog for the StudentGender field as per Figure 12. Figure 12: Gender specific pronouns Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 40 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 3. Preview your results. Notice how the pronoun changes depending on the gender of the student. You can find a specific record by using the Mailings Find Recipient button. 4. Save your document. 6.2.5 Fix possessive nouns where the name ends in s The temptation is to use a similar approach. Unfortunately, there is not an option to do this. One option is to ignore the problem, then to complete the mail merge by selecting Mailing Finish & Merge Edit Individual Documents. You can then do a Find and Replace on all “s’s” and replace with “s’”. This will work, but is not very elegant. A different solution is to return to the source and edit this document. We are going to use the power of Excel to calculate whether or not the name ends in an ‘s’ and add the appropriate possessive. 1. 2. 3. 4. Ensure your Word mail merge document is closed to prevent file locking errors. Open Exercise2.xlsx. Insert a column to the between Given1 and Surname. Give it a title GivenWithPossessive. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 41 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 5. In the cell C2 enter the formula: =IF(RIGHT(B2,1)="s",B2&"'",B2&"'s") What’s in a formula? Each part is also a formula in itself. If part: RIGHT returns the specified number of characters from the right side of the string – in our case, return the last character. Then part: If that character is an s, take the cell and append the string containing just an apostrophe. Else part: It the last character is not an s, take the cell and append the string, apostrophe s. & is the append operator. When you click OK, Excel will automatically copy this formula down through the cells. It does this because the table is already formatted. If it were not, you would need to copy the formula down into each cell manually. 6. Observe the results. Notice how those names that end in an ‘s’ do not have the additional ‘s’ after them. 7. Save the file and close it. 8. Open the Exercise 3a.docx file. Whenever you open a file containing a mail merge, you will receive a warning indicating that this file accesses external data. Click Yes to allow this to continue. . 9. In the exercise file, delete the Given1 field and the “’s”. Insert the newly created GivenWithPossessive field. 10. Preview your results. Notice how the correct name with possessive is used. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 42 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 6.2.6 Other merge field switches There are other merge field switches that can be applied to any merge field. Selecting the Options button in the field codes window accesses these options. The general switches may be used to force the field to use a specific type of letter casing whereas the field specific switches are used to insert text before and after the field if it is not blank, or to change the text into vertical text. These are advanced options and are not covered in detail here. 6.3 Fixing a broken source file There are times when either the original mail merge source file is not available or you wish to use an alternative source file. In either case, you will need to relink a source file into your mail merge file. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 43 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 1. Open the mail merge file. You will be prompted with the warning that the file accesses external data and asked if you want to continue. If you want to change the source it is important that you select the No option. 2. Select Mailings Select Recipients Use an Existing List…. 3. 4. 5. 6. Browse to new file and select the query from the list. Select Mailings Preview Results. If any fields are missing or have different names, Word will prompt to identify them You can either remove the field from the main document or replace it with one from the source. When you replace a merge field, any formatting options on that field are also replaced. Be sure to edit that field and set the formatting options again. Microsoft Word mail merge Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 44 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 7 More advance MS Query 7.1 SQL Structured Query Language (SQL) is a computer language that is used to interact with relational databases such as the one used by Synergetic. It is the language used by both MS Query and Crystal Reports to retrieve information from the Synergetic databases. 7.1.1 Sample SQL statements The simplest form of an SQL statement is the SELECT statement that retrieves information from database tables and views. The following statement is the equivalent statement produced by the wizard in section 5.1. SELECT vStudentContactAddress.ID, vStudentContactAddress.Given1, vStudentContactAddress.Surname, vStudentContactAddress.YearLevel, vStudentContactAddress.StudentGender, vStudentContactAddress.StudentContactAddressFull, vStudentContactAddress.StudentContactMailName, vStudentContactAddress.StudentContactMailSalutation, vStudentContactAddress.FileYear, vStudentContactAddress.FileSemester, vStudentContactAddress.CurrentSemesterOnlyFlag, vStudentContactAddress.StudentBirthDate, vStudentContactAddress.StudentContactDefaultEmail, vStudentContactAddress.StudentContactEmail, vStudentContactAddress.StudentContactOccupEmail FROM SynergyOne.dbo.vStudentContactAddress vStudentContactAddress WHERE (vStudentContactAddress.CurrentSemesterOnlyFlag=1) ORDER BY vStudentContactAddress.Surname, vStudentContactAddress.Given1 There are four parts to this statement: The SELECT part specifies the fields to be retrieved. The FROM part specifies the tables or views from which the data is to be retrieved. The WHERE part specifies the criteria (filters) applied to limit the number of records retrieved. The ORDER BY part specifies the sort order for the retrieved records. SQL statements can also be used to manipulated the data being retrieved by calculating totals, averages etc. The following statement reports the totals of all religion codes in the school. SELECT vStudents.StudentCampus, vStudents.StudentReligionCode, Count(*) FROM SynergyOne.dbo.vStudents vStudents WHERE (vStudents. CurrentSemesterOnlyFlag =1) GROUP BY vStudents.StudentCampus, vStudents.StudentReligionCode ORDER BY vStudents.StudentCampus, vStudents.StudentReligionCode More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 45 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query This statement retrieves the campus and religion code together with a counter. The GROUP BY option causes all campuses and the religion codes within those campuses to be grouped together. These are then sorted in order. SQL is not a difficult language to learn, but for most it is not required. Knowledge of generation of the statements can open up many opportunities beyond what the tools offer. Section 10 contains some resources for those interested in learning the basics. 7.2 Using an existing SQL statement in MS Query Sometimes, you may have a query that has been generated by another system, for example Synergetic, Crystal Reports, or IT Support. You can use that statement in your query. 1. Start Excel in the normal manner 2. Start MS Query as you normally would. Do not use the wizard. 3. Close the Add Tables window without adding anything. 4. Open the SQL window by selecting View SQL or click the toolbar. button on the 5. Go to the SQL statement. Highlight it, ensuring you have the entire statement, press <Ctrl-C> on the keyboard to copy it. 6. Return to the SQL window in MS Query and press <Ctrl-V> to paste it in the window. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 46 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 7. Click OK to process the statement. If the statement is correct, MS Query will display the result. 8. You can add additional fields to the query or return to Excel as is. You can also view the SQL statement from your query by using the button and copy the SQL statement from the window to send it to someone else, for example, in an email to support. You can also type the SQL statement directly into the SQL window, but beware of errors. SQL is extremely sensitive to errors. 7.3 Using Crystal Report queries Crystal Reports also uses SQL queries to retrieve the information from the SQL databases. Synergetic allows you to view the SQL query that Crystal Reports uses. You can then copy this to MS Query to get the data in your spreadsheet. 1. In Synergetic open Student Crystal Reports Available. 2. Select the Students Ages Report (STUAGE21). 3. On the report selection criteria screen, choose the filters which will be added to the SQL statement. For example, Gender field. Every field that has an entry will be added to the SQL statement. Those that are empty will be ignored. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 47 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 4. Hold down the <Shift> key while clicking on the Preview button. 5. The query will be displayed in the View SQL Query window. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 48 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 6. Click the Copy to Clipboard button (or highlight the text and press <Ctrl-C, or right click and select Copy). 7. Load the MS Query through Excel without using the wizard. 8. Close the Add Tables window. 9. Open the SQL window by selecting View SQL or click the button on the toolbar. 10. Right click on the SQL statement area and select Paste (or press <Ctrl-V>). 11. The statement will show in the SQL window. 12. Click OK. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 49 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 13. The query will show as a normal MS Query in can be edited or returned to MS Query. Not all Crystal Report queries are supported in this manner. Only those reports that access the views are supported. Hint: the table name starts with a ‘v’. 7.3.1 Dates in queries Many queries copied from Crystal Reports use dates in the criteria. If you are going to save this query to use later, you will need to manually change the dates, FileYear, and FileSemester each time you use the query. Alternatively, you can replace the date with a query function called GetDate() that always returns the current date and time. Instead of FileYear and FileSemester, you can use the CurrentSemesterOnlyFlag. Empty dates return inconsistent results. This is the reason for the use of the isnull() function. This particular function returns the 31 December 9999 if the date is empty or NULL (never used). 7.3.2 The StudentActiveFlag Some views, including vStudents and vStudentsAll have a flag called StudentActiveFlag. This flag can be used to check whether the student is at the school on the current day. That is, they have an entry date before today, and that they are either leaving after today, or have no leaving date. This flag relates to the Active flag on the criteria screen in Synergetic for Current Student Maintenance. It has some limitations: More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 50 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 7.4 It always filters on the current date. It cannot be used to get students who are active as of next week. It is not available in all views. Adding other views and tables In some cases, a single view may not contain all the fields needed. In this case, another table or view will need to be added gather specific fields. In section 5.1 the query wizard was used to extract student contact information. In this example this query will be extended to add the student award descriptions to inform parents about the awards their child has received. The related awards fields are not in vStudentContactAddress, so the view vAwards will be added to the query. 7.4.1 1. 2. 3. 4. 5. 6. Add in additional table to Exercise 1.xlsx Open the Exercise 1.xlsx file. If necessary, select Enable Content at the security warning. If prompted by the security warning to make this a trusted document, select No. Save the file as Exercise 4.xlsx. Edit the query. (Right click on a cell and select Table Edit Query…). Add a criterion for StudentGender = ‘M’. 7. Add a table by selecting Table Add Tables… or by clicking . 8. Scroll through the tables until you find the vAwards view. Double click it to add to the query. When you have finished, click Close. At this point you have two tables in your query. You may have also noted that the records being retrieved have changed. MS Query does not know how the records in one table relate to the records in the other table. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 51 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query You can remove tables by clicking on a field in the table and pressing <Delete> on the keyboard. If you delete a table with fields in the query, those fields will also be deleted from the query. Tables can be added at any time by clicking Table Add Tables…. 7.4.2 Linking tables or views Whenever multiple tables or views are used in queries they need to be joined. To join tables, you need to know which fields match. In this example, the ID field in vStudentContactAddress corresponds to the StudentID fields in vAwards. To link the tables: 9. Resize the tables so that they are sufficiently large and scroll them so that both the fields are visible. 10. Click on the ID field in vStudentContactAddress with the left mouse button and while holding down the button, drag the field over the StudentID field in the vAwards table. Release the mouse button. 11. There should now be a link between the two tables. At this point, the resulting records have also changed to include only those students that have awards. This is due to the type of join between the two tables. Joins are discussed in section 7.5. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 52 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 12. Add the following fields from vAwards to your query: AwardsClassificationCode AwardCode AwardDescription 7.5 Joining tables Whenever tables and views are linked, joins are used to link the data. The type of join is very important in determining the results of the query. Unexpected, or seemingly incomplete results are often the results of an incorrect join type for the query. Before joining tables, it is a good idea to have some expectation of what the result will look like. This will help identify errors early. Too many or two few results can be addressed early. Start off with a small sample. Deliberately filter your results to a limited number (for example a single class) so that the results are easy to work with. Once the results are correct, the filters can be removed. There are many different types of joins, but two main joins are supported by MS Query; Inner Joins, and Outer Joins. Double clicking the join 7.5.1 allows you to change the type of the join. Inner joins An inner join is the default join type when joining one view or table to another in MS Query. Inner joins return results where there is an exact match on the linking field(s) in both the tables. In MS Query, the first type of join listed in under the Join Includes is an inner join. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 53 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query In our example, by linking vStudentContactAddress to vAwards using the default inner join, only the students who have records in both tables will be returned. That is, you will not get a record for a current student who does not have an award. Similarly, you will also not get a record for a student who got an award, but has left the School. The SQL statement that this inner join generates is: SELECT vStudentContactAddress.FileYear, vStudentContactAddress.FileSemester, vStudentContactAddress.Given1, vStudentContactAddress.Surname, vStudentContactAddress.YearLevel, vStudentContactAddress.StudentContactMailName, vStudentContactAddress.StudentContactMailSalutation, vStudentContactAddress.StudentContactAddressFull, More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 54 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query vAwards.AwardClassificationCode, vAwards.AwardCode, vAwards.AwardDescription FROM SynergyOne.dbo.vAwards vAwards, SynergyOne.dbo.vStudentContactAddress vStudentContactAddress WHERE vStudentContactAddress.ID = vAwards.StudentID AND ((vStudentContactAddress.FileYear=2016) AND (vStudentContactAddress.FileSemester=1) AND (vStudentContactAddress.StudentGender='M')) ORDER BY vStudentContactAddress.Surname, vStudentContactAddress.Given1 7.5.2 Outer joins A left outer join returns all records from the left table (the primary table) and links data from the right table (the secondary table) where there is a matching record. A left outer join is also indicated in the tables by an arrow on the link indication directionality. In our example, changing the join to a left outer join (join option 2) will return all records from vStudentContactAddress and only those who have awards and are also in vStudentContactAddress. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 55 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query The SQL statement has been updated to: SELECT vStudentContactAddress.FileYear, vStudentContactAddress.FileSemester, vStudentContactAddress.Given1, vStudentContactAddress.Surname, vStudentContactAddress.YearLevel, vStudentContactAddress.StudentContactMailName, vStudentContactAddress.StudentContactMailSalutation, vStudentContactAddress.StudentContactAddressFull, vAwards.AwardClassificationCode, vAwards.AwardCode, vAwards.AwardDescription FROM {oj SynergyOne.dbo.vStudentContactAddress vStudentContactAddress LEFT OUTER JOIN SynergyOne.dbo.vAwards vAwards ON vStudentContactAddress.ID = vAwards.StudentID} WHERE (vStudentContactAddress.FileYear=2016) AND (vStudentContactAddress.FileSemester=1) AND (vStudentContactAddress.StudentGender='M') ORDER BY vStudentContactAddress.Surname, vStudentContactAddress.Given1 In MS Query, join option 3 is also a left outer join, but switches the views making vAwards the left (primary) table. SQL does allow for a right outer join, but it is considered best practice to use left outer joins where possible. MS Query only allows two tables if using an outer join. If you try to add a third you are prompted to delete the table. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 56 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query The SQL statement may be modified manually to include more outer joins but you will get an error message indicating that the query cannot be displayed graphically. 7.5.3 Activity: Joining tables In this activity we will query the database for information about staff who are also past students. 1. Start a new query. 2. Add the vStaff view. 3. Add the following fields: StaffID StaffSurname StaffGiven1 StaffTitle SchoolStaffCode StaffDepartment StaffExtension StaffCampusDescription StaffGender StaffCategoryDescription 4. Add the vPastStudentAddress view. 5. Link vStaff to vPastStudentAddress on the ID field. 6. Add the following fields from vPastStudentAddress Surname Given1 PeerYear LastCampusDescription LastHouseDescription EntryYearLevel LastYearLevel 7. Double click the link, remove the current link and try each type to see the difference. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 57 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 7.6 Finance tables/views Thus far, all tables and views have been from the SynergyOne database. Synergetic finance tables and views are found in the SynergyOneFinance database. This will need to be selected as the datasource if finance related queries are to be created. You will need to have permission to the SynergyOneFinance database in order to access views and tables in this database. 7.6.1 Using finance views The principles of querying the SynergyOneFinance (or a Foundation) database are exactly the same as querying the Community and Student information in the SynergyOne database. The only difference is the views that are available. Example 1: vEnrolmentDeposits In this example, the vEnrolmentDeposits view will be will be queried to obtain information about student (future, current, and past), the repayee details, and the information on the enrolment deposit (deposit code, the refundable flat, the amount still owing). 1. Start MS Query and connect to the Start MS Query and connect to the SynergyOneFinance database. 2. In MS Query select Table Add Tables…. 3. Add the vEnrolmentDeposits table. 4. Add the following fields: Repayee1MailNameJoint Repayee2MailNameJoint DepositCode DepositAmountOriginal DepositAmountOwed StudentNameExternal StudentTypeDescription ContactMailNameJoint DefinitionRefundableFlag DefinitionStudentBasedFlag 5. Sort the records by DepositCode. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 58 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 6. Return the data to Excel and format the amount columns to currency with two decimal places (for readability purposes). a. Select the columns to be formatted. b. Right click somewhere on the highlighted columns and select Format Cells …. The keyboard shortcut <Ctrl-1> can also be used to format cells c. Select the currency format and click OK. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 59 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query d. The fields will then be reformatted as follows: 7. Save the spreadsheet as EnrolDepost.xlsx . More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 60 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query Example 2: vCashReceiptsEntry Another commonly used finance view is vCashReceiptsEntry. This is often used to analyse receipts processed in a particular posting or date range 1. In MS Query create a new query and add the vCashReceiptsEntry view. 2. Add the following fields: ID MailName PostingDate ReceiptPostingNumber ReceiptAmount ReceiptPaymentType ReceiptPaymentDescription ReceiptPaymentReference 3. Add a criterion for the PostingDate field using the is between operator to select a date range for April 2016. 4. This will appear in the criteria section as below: 5. Return the data to Excel and format the amount column to currency with two decimal places. 6. Save the spreadsheet as CashReceipts.xlsx More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 61 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 7.6.2 Activity: Joining finance tables In this activity we will query the database for information about debtors who do not have an active arrangement in place. 1. In MS Query, create a new query in the SynergyOneFinance database. 2. Add the view vDebtors. 3. Add the following fields from vDebtors to the query: DebtorID DebtorGiven1 DebtorSurname DebtorHomePhone DebtorMobilePhone DebtorNotifiedBalance DebtorLastPaymentAmount DebtorLastPaymentDate DebtorArrangementActiveFlag 4. Add the criterion for DebtorNotifiedBalance > $5000 5. Add another criterion for DebtorLastPaymentDate <= 01/01/2016 but this time ensure that the criterion is set to or not and. 6. The query now retrieves all debtors with an outstanding balance of more than $5,000 and also any debtors who have not made payments since 1 January 2016 regardless of their outstanding balance, which may be zero. 7. Add a new view, vDebtorActiveArrangements and link using the DebtorID as the link field. Note how the number of records reduces from 736 to 185. Why is this? 8. Change the join type to a left join (join type two in the list of options). Note how the number of records returns to 736. Why is this? 9. Add the following fields from the vDebtorActiveArrangements view: DebtorID ArrangementAmount ArrangementCode ArrangementComment ArrangementDate ArrangementDescription ArrangementFrequency OverdueOnArrangedBalanceFlag More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 62 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 10. You may wish to further filter for those who have no active arrangement. There are two possible alternatives. a. Is the DebtorArrangementActiveFlag in the vDebtors view equal to zero? b. Does the DebtorID field in the vDebtorActiveArrangements view have not value (that is, is null)? It that is the case then a record exists in vDebtors for which there is no corresponding record in vDebtorActiveArrangements. 11. Add a criterion (using the and option) to check the vDebtorActiveArrangements DebtorID field for a null value. 12. The total number of records retrieved should be 553 and the criteria should look similar to below: 13. Return the results to Excel and save the spreadsheet. 7.6.3 Advanced criteria Criteria can become very complicated to enter in the MS Query interface. For example, suppose you wanted to find debtors who: have an outstanding balance of $5,000 or more, or who have not made a payment since before 01/01/2016, and those who have not got an arrangement or are overdue on an arrangement. The criteria section would look similar to the Figure 13 below. Figure 13: Complex criteria section There must be a line for each set of people to be included in the retrieved results. That is, these criteria mean the query retrieves; More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 63 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query Debtors who have a balance over $5,000 and who do not have an arrangement in place, Debtors who have not made a payment since 01/01/2016 and do not have an arrangement in place, Debtors who a balance of over $5,000 and are overdue on an arrangement, and Debtors who have not made a payment since 01/01/2016 and who are overdue on an arrangement. The SQL clause for the above criteria is: WHERE (vDebtors.DebtorNotifiedBalance > $5000 OR vDebtors.DebtorLastPaymentDate <='2016-01-01') AND (vDebtorActiveArrangements.DebtorID IS Null OR (vDebtorActiveArrangements.DebtorID Is Not Null AND vDebtorActiveArrangements.OverdueOnArrangedBalanceFlag=1)) If you need to commonly do this type of complex retrieval task it may be worth learning some basic SQL as it is far quicker to define these kinds of criteria in this manner. 7.6.4 Joining finance and non-finance tables When querying the SynergyOneFinance database, you may want to join your finance view or table to a view or table from the non-finance database (SynergyOne). When you add a further table to the query using Tables Add Tables… you can change the database to SynergyOne in the Add Tables window. This allows you to select from the nonfinance tables and views and add them to your query 7.6.5 Payroll views Payroll views may be added to any finance query. The payroll tables have an additional level of encryption applied to them. To decrypt them, the Synergetic client must be running and the Payroll module must be opened and authenticated. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 64 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 7.7 Using tag lists in a query You can create a query that is based on a Synergetic tag list, retrieving only the information for people in the active tag list. Returning to the exercise from section 6.2. Rather than using inbuilt parameters, you want to restrict the list of parents to that of a current tag list. 1. In Synergetic, tag 5 random students. a. Choose the student. b. Right click on the student in the selector grid and choose Tag record. c. Do this for 5 students. 2. Open Exercise3.xlsx. 3. Save the file as Exercise 5.xlsx and edit the query. 4. Add the table vTagsOwn to the query. 5. Link vTagsOwn to vStudentContactAddress by the ID field in both tables using an inner join. 6. Return the data to Excel. 7. Save the result and close Excel. 8. Open the Exercise 3a.docx file and select No to the warning that the file accesses external data. 9. Save the file as Exercise 5.docx. 10. Select Mailing Select Recipients Use an Existing List…. 11. Browse to Exercise 5.xlsx and select the query. 12. Preview your results. Notice how there are only a small set of results. 13. Save your file and close Word. 14. Return to Synergetic. 15. If the Tag List window is closed, open it by selecting Window Show Tag List. 16. Clear the current tag list by clicking the Clear button. 17. Select Yes to confirm the clear. 18. Tag five completely different parents. 19. Open Exercise 5.xlsx. Enable the content if necessary. 20. Select Data Refresh All. 21. Save the file. Close Excel. 22. Open Exercise 5.docx. 23. Select Yes to allow external data access. 24. Use the Mailing Preview Results to view the new letters. Tag lists may be saved for later retrieval. Private tag lists are stored for 30 days. Public tag lists are stored indefinitely. More advance MS Query Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 65 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 8 Pivot tables and charts Pivot tables and charts help summarise large volumes of data and represent it graphically. Pivot tables and charts are best understood through their usage. There are two ways of generating a pivot table or chart from the data retrieved through MS Query: 1. When you return data from MS Query there are options to create pivot tables and pivot charts. 2. Alternatively, with a cell or part of the returned spreadsheet selected, select Insert PivotTable. 8.1 Summarising student results In this activity we will summarise the student results in a pivot table. We want to know: 1. What was different rating levels across each of the mini schools? 2. Which house does the best (academically) in each school? 3. Which year level is most at risk? 8.1.1 Preparation 1. Create a new MS Query. 2. Use the SQL Query from the Exercise 6.txt file in the lab files folder: SELECT vStudentReportsSemesterResults.StudentHouse, vStudentReportsSemesterResults.StudentHouseDescription, vStudentReportsSemesterResults.StudentHouseHeadOfHouse, vStudentReportsSemesterResults.StudentYearLevelDescription, vStudentReportsSemesterResults.StudentYearLevelSort, vStudentReportsSemesterResults.StudentYearLevel, luCampus.Description AS 'MiniSchool', vStudentReportsSemesterResults.ClassCode, vStudentReportsSemesterResults.ClassDescription, vStudentReportsSemesterResults.AssessAreaResultGroup, vStudentReportsSemesterResults.AssessAreaResultType, vStudentReportsSemesterResults.AssessResultsResult Pivot tables and charts Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 66 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query FROM SynergyOne.dbo.luCampus luCampus, SynergyOne.dbo.vStudentReportsSemesterResults vStudentReportsSemesterResults WHERE vStudentReportsSemesterResults.ClassCampus = luCampus.Code AND ((vStudentReportsSemesterResults.FileYear=2015) AND (vStudentReportsSemesterResults.FileSemester=4) AND (vStudentReportsSemesterResults.AssessAreaResultType Like 'CRIT_%') AND (vStudentReportsSemesterResults.AssessAreaResultGroup Like '%SUMMATIVE')) ORDER BY luCampus.Description, vStudentReportsSemesterResults.StudentHouse, vStudentReportsSemesterResults.StudentYearLevelSort 3. Return the data to Excel to a table in cell $A$1. This should return 38,204 records. 4. Rename the tab to Data. 5. Save the file as Exercise 6.xlsx. 8.1.2 Create a PivotTable 6. Click on a cell in the Data table and select Insert PivotTable. 7. Ensure the query is the selected range and leave the New Worksheet selected. 8. The empty PivotTable window is now displayed. Pivot tables and charts Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 67 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query When designing a PivotTable, think about what the final result will look like before you start. Consider the rows and columns you want. In our case, we want to see each result in the columns and each mini school in the rows. We want each mini school to be broken down into houses and each of those houses into year levels. 9. Rename the tab to ‘PivotTable’. 10. From the PivotTable Fields List drag the MiniSchool label to the Rows area. This will make the row labels in your pivot chart the mini schools. 11. From the PivotTable Fields List drag the AssessResultsResult label to the Columns area. This will make the column labels in your pivot chart the mini schools. Pivot tables and charts Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 68 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 12. To count how many of each result in each mini school there are, drag the AssessResultsResult field from the PivotTable Fields list into the Values column. 13. Notice how the column labels have a provision for blank values. We do not want to include these values. There are actually two options for blank - <null> or empty cell. They are interpreted differently by SQL. 14. To exclude these values, click the drop down arrow next to the column labels cell and uncheck the blank and empty cell options. Click OK. 15. After relabelling the headings and resizing the columns the results should be similar to: Pivot tables and charts Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 69 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 16. To further sort by house, drag the StudentHouseDescription field into the Rows area below the MiniSchool field. 17. Repeat the process for the StudentYearLevelDescription field. 18. To display the totals for each house level, click the arrow on the StudentHouseDescription and select Field Settings…. Pivot tables and charts Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 70 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 19. In the Subtotals & Filters tab, set the subtotals to Automatic and the Layout & Print tab set the layout to Show items in tabular form. 20. Relabel the new column to ‘Year Level’ and resize. We now have a pivot table that will help us answer our questions. 21. Expand and contract the various rows to see how they can answer the questions. 8.1.3 Link a PivotChart to the PivotTable Adding a chart to this table will greatly aid understanding of the results. We can either repeat the process and build a PivotChart from the beginning, or we can create a linked PivotChart. To create a linked PivotChart: 1. Click anywhere inside the PivotTable. 2. Select Insert PivotChart PivotChart. Pivot tables and charts Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 71 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 3. On the Insert Chart window click OK. A PivotChart can be almost any type of chart. In our case we want to use a standard Clustered Column chart. 4. Excel automatically inserts a chart that is linked to the PivotTable. Position the chart and resize it. 5. Rename the tab to ‘PivotTable and PivotChart’. 6. Experiment with opening and closing the results of the table. Notice how the chart updates to reflect the results. It is very important to interpret the results in the correct context. At first, it may seem that there is a very high proportion of ‘C’ results in the Junior School when compared to the ELC. However, when observing the relative difference in total of total number of results, these are, on average, very similar. Similarly, when comparing the Junior School to the Middle School and Senior School, the number of C results in the Junior School vastly outweighs the number of C results in the other schools, even when averaged out. However, when the context of these results is considered, it is realised that the meaning of the C result is not the same across the year levels, making this sort of comparison meaningless. 8.1.4 The slicer There is still an overwhelming amount of data to observe here. Excel provides a mechanism to interactively select what data is shown in the PivotTable at any time. 1. Expand all options in the PivotTable. This step is optional but does make things simpler later on. 2. Click anywhere in the PivotTable and select Analyse Insert Slicer. 3. In the Insert Slicers window select the MiniSchool field and click OK. Pivot tables and charts Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 72 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 4. Excel adds a new MiniSchool slicer window. Rearrange the objects so that they all fit. 5. Click on one of the mini schools in the slicer window. Notice how the PivotTable and PivotChart update to include only that mini school. Use <Ctrl> click to select multiple schools. 8.1.5 Answering the questions 1. What was different rating levels across each of the mini schools? 2. Which house does the best (academically) in each school? 3. Which year level is most at risk? Pivot tables and charts Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 73 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 8.2 Adding parameters to query 1. Return to the data tab and edit the query to include a parameter for FileYear and FileSemester. 2. Add a parameter for AssessAreaResultGroup. Be sure to include a wildcard. 3. Add a filter for StudentGender. 4. Return the results to Excel and get the information for 2014 Term 4, with result group 4 8.3 Refreshing PivotTables and PivotCharts If you create a PivotTable or PivotChart on a separate worksheet, it is separated from the data and is not automatically updated. Therefore, when the data is updated, an extra step is required to update the PivotTable. 1. 2. 3. 4. 5. Return to the Data tab. Select Data Refresh All. Select the PivotTable and PivotChart tab. Select either the PivotTable or PivotChart. Select Data Refresh All. Alternatively, select Data Refresh All twice. The first time refreshes the query, the second refreshes the PivotTable and joined PivotChart. If the PivotChart was not joined to the PivotTable, or other PivotTables were on the same worksheet, each would need to be refreshed. Pivot tables and charts Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 74 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 9 Future students 9.1 Future student based pivot tables 9.1.1 Create the future student query 1. In MS Query create a query. 2. Add the view vFutureStudentApplications and the lookup table luFuturePriority. 3. Join vFutureStudentApplications and luFuturePriority by linking FuturePriority in vFutureStudentApplications to Code in luFuturePriority. 4. Add the following fields from vFutureStudentApplications: ApplicationEnrolYear ApplicationYearLevel FuturePriority FutureStatus FutureStatusDescription 5. Add the Description field from luFuturePriority. 6. Rename the fields as follows: ApplicationEnrolYear Application Enrol Year ApplicationYearLevel Year Level FuturePriority Priority Code FutureStatus Status Code FutureStatusDescription Status Description Priority 7. Return the data as a Table to Excel. 8. Rename the tab to ‘Data’. 9. Save your file as FutureStudentApplications.xlsx. 9.1.2 Create a PivotTable with the summary of statuses 1. Create a PivotTable from the future student data in FutureStudentApplications.xlsx on a new tab. 2. Rename the tab to ‘Application Statuses’. 3. Add Status to Columns. 4. Add Application Enrol Year and Year Level to Rows. 5. Add Status to Values. Future students Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 75 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 6. Filter the year level for this year and future years. 9.1.3 Create a PivotTable with the summary of application priorities 1. Create a PivotTable from the future student data in FutureStudentApplications.xlsx on a new tab. 2. Rename the tab to ‘Application Priorities’. 3. Add Priority to Columns. 4. Add Application Enrol Year and Year Level to Rows. 5. Add Priority to Values. 6. Filter the year level for this year and future years. 9.2 Enquiries You can automate a letter to be sent to prospective parents who have lodged an enquiry with the school by creating a mail merge document using a ‘Stored Procedure’. This tip only works if the parents were entered into the enquiries module. Future students Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 76 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 1. Create a new MS Query. 2. Do not use the wizard. 3. Do not add any tables, instead open the SQL window. In the window type the following query exactly: crspEnquiryProspectusPrint 4. Select OK. 5. You will get a warning that the result cannot be displayed graphically. Click OK. 6. Return the data to Excel and save the document. 7. Set up a Propectus letter with the correct mail merge fields select Mailings Finish & Merge Merge to New Document. You will have a set of letters ready to be printed and mailed to prospective parents and contacts. Open Synergetic and load one of the new Enquiries. Under the Enquiry tab you will see a time and date has been populated in the Prospectus Date field. After you have entered more enquiries, you can open the recipient data file and refresh. Then return to the mail merge letter. If you filter on the ProspectusPrintedDate field for the current date, you will have the latest entries to merge to a new document. Future students Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 77 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 9.3 Future student activity You can also set up a mail merge letter for any Activity you have in Future Students. For example, you may wish to do a mail merge to all parties interested in the next Open Day. For this to work, you must first check that the Mail Request flag in luFutureActivity table is checked for that activity. Note, that changing this will only update activities recorded from that point on and will not retrospectively update activities of that type. In a similar manner to enquiries, create a new query with the SQL statement: crspFutureActivityPrint ‘<CODE>’ where the code is the code of the activity required. Once you have executed the query, return to Synergetic. You will note that the activity in Activity tab has been updated. The Mailed Date has been completed to include the current date. 9.4 Future status changed A mail merge can be set up to notify parents that their child’s status has been changed, that is, from waitlisted to confirmed. Before beginning, you will need to check that the status codes have been populated in luFutureStatus. These merge codes are set up in luFutureStatusMerge. Figure 14: luFutureStatus merge codes Figure 15: luFutureStatusMerge settings Follow the steps as above for the enquiries mail merge and in the SQL statement area use crspFutureStatusChangedPrint ‘F’ to select all future students that have had their status changed to Finalised. Future students Prepared by Document Name IT Network Manager: Ian MacRae Introduction to MS Query Page Date Version 2.0 78 of 79 30 April 2017 © The Hutchins School 2016 Introduction to MS Query 10 Appendix – Additional resources 10.1 Lynda.com Lynda.com is an online training facility provided free of charge to all staff and students at Hutchins. It contains thousands of video tutorials on almost every computer related topic imaginable. Much of the material in this guide was influenced by material in Lynda and customised to be more Hutchins friendly. Every staff member should be logging into Lynda and setting up their account. All courses completed go towards your PD. You can choose your own playlist, customise it to your requirements. Every lesson has a transcript of the narration and you can pause and rewind as you need to. You can even speed up if you find them talking too slowly. Courses range from beginner skill level to advanced. Many come with their own course files. Some suggestions are: Excel productivity tips: http://www.lynda.com/Excel-tutorials/Excel-Tips/3630012.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3ams+query%0apage%3a1%0a s%3arelevance%0asa%3atrue%0aproducttypeid%3a2 Mail Merge in Depth with Word 2016: http://www.lynda.com/Word-Onlinetutorials/Mail-Merge-Depth-Word-2016/4170922.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3ams+query%0apage%3a1%0a s%3arelevance%0asa%3atrue%0aproducttypeid%3a2 Word 2016 Essential Training: http://www.lynda.com/Word-tutorials/Word-2016Essential-Training/378044-2.html Excel 2016 Essential Training: http://www.lynda.com/Excel-tutorials/Excel-2016Essential-Training/3769852.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3ams+query%0apage%3a1%0a s%3arelevance%0asa%3atrue%0aproducttypeid%3a2 Unfortunately, Lynda does not cover custom, in-house software such as Synergetic. Appendix – Additional resources Prepared by IT Network Manager: Ian MacRae Document Name Introduction to MS Query Page Date Version 2.0 79 of 79 30 April 2017 © The Hutchins School 2016