Previous icon

Next icon

Advanced Microsoft Query and SQL examples

Microsoft Query is a tool that is used to create SQL (Structured Query Language) statements that will extract and graphically display virtually any data that you want from the Synergetic database. Crystal Reports allows you to produce similar results but allows a lot more manipulation of the data from a graphical perspective.

Outlined below are some intermediate to advanced SQL statements that you can use with Microsoft Query. For more detailed information, refer to:

If you have access to SQL Server Management Studio (your network administrator most likely would), you can load either:

Standard Synergetic fields used for filtering data

The following table lists fields useful for filtering data:

Field

Value

Additional information

DeceasedFlag

0

Only retrieve records of people not deceased.

0 = False.

ValidAddressFlag

1

1 = True

CurrentSemesterOnlyFlag

1

1 = True

FileYear

2013

 

FileSemester

1, 2, ...n

 

StudentStatus

<> 'LFT'

See luStudentStatus in the System Maintenance manual.

StudentContactType

'SC1', 'SC2' or 'SCT'

See luContact in the System Maintenance manual.

StudentContactNormalMailFlag

1

1 = True

Example 1 - Use SUBSTRING with a character string

This example shows how to return only a portion of a character string.

From the vStudentClasses view, this query returns records filtered based on a portion of the class code.

Query

SELECT

  vSC.StudentID, vSC.StudentSurname, vSC.StudentPreferred,

  vSC.StudentCampus, vSC.StudentHouse, vSC.ClassCampus, vSC.ClassCode,

  vSC.ClassDescription

FROM vStudentClasses vSC

WHERE (substring(vSC.ClassCode,1,2)='MA')

ORDER BY

  vSC.StudentSurname,

  vSC.StudentPreferred

The WHERE substring statement selects MA because there is a character match starting from position one which includes two characters of the class code.

Microsoft Query window

The following example shows you how to display the third and fourth characters of the string constant abcdef. The result set is cd.

SELECT x = SUBSTRING('abcdef', 3, 2)

Note: Click the link to display more details about the SUBSTRING function.

Example 2 - Mail names and addresses for current parents and staff

Query

SELECT

  MailNameJoint, AddressFull

FROM Community

  LEFT JOIN Addresses ON (

    Community.AddressID = Addresses.AddressID)

WHERE

  EXISTS (SELECT * from Constituencies

          WHERE Community.ID = Constituencies.ID

            AND ConstitCode = '@PC')

  AND EXISTS (SELECT * from Constituencies

              WHERE Community.ID = Constituencies.ID AND ConstitCode = '@STF')

  AND DeceasedFlag = 0

  AND ValidAddressFlag = 1

  AND SpouseFlag = 0

Points to note:

Example 3 - All parents who are also staff members (primary or spouse)

Query

SELECT DISTINCT

  StudentContactMailName, StudentContactAddressFull

FROM vStudentContactAddress

  LEFT JOIN Community ON (

    StudentContactID = Community.ID)

WHERE EXISTS (SELECT * FROM Constituencies

              WHERE vStudentContactAddress.StudentContactID = Constituencies.ID

                AND ConstitCode = '@STF')

  OR EXISTS (SELECT * from Constituencies

             WHERE SpouseID = Constituencies.ID

               AND ConstitCode = '@STF')

Points to note:

Example 4 - Selecting records in Microsoft Query on the basis of date fields

Select enquiries in the last 7 days:

Query

SELECT

  vEnquiryContactAddress.EnquiryID, vEnquiryContactAddress.EnquiryDate

FROM vEnquiryContactAddress

WHERE

  {fn TIMESTAMPDIFF(SQL_TSI_DAY, {fn CURDATE()}, vEnquiryContactAddress.EnquiryDate)} > -6

Points to note:

Example 5 - Selecting students who have siblings at the school

What factor determines siblings? Answer: Primary Contact

The primary contact is the person or primary person of a couple who is the main responsible person for the child, as far as your organisation is concerned.

The child does not necessarily need to live at the address. That is the meaning of the term contact and it is normally the case.

The accounts do not need to be sent to the primary contact. That is the meaning of the debtor on the Debtor tab and again, it is normally the case.

One meaning which is attached to the primary contact is that it is the means by which Synergetic tries to work out who are siblings. That is, two students who share the same primary contact are assumed to be siblings. It does not differentiate between normal and step-siblings - that is the purpose of relationships. It is used for the following purposes:

The following query can be used in Microsoft Query to find all future students who have a sibling who is currently enrolled, using the definition above.

Sample query

Query

SELECT * FROM vFutureContactAddress

WHERE FutureContactID IN

  (SELECT StudentContactID from vStudentContactAddress

   WHERE StudentContactType = 'SC1'

   AND CurrentSemesterOnlyFlag = 1

   AND vStudentContactAddress.ID <> vFutureContactAddress.ID)

AND FutureContactType = 'SC1'

Example 6 - Select enrolment changes in the last seven days

You can also use the following statement to select records changed during the previous day(s). This statement will only work in the current year – that is, you cannot select records changed before the current year. The query uses the SQL DATEPART function to calculate the date range required.

Query

SELECT vFCA.FutureSurname, vFCA.FuturePreferred, vFCA.FutureGender, vFCA.FutureContactType, vFCA.FutureContactDescription, vFCA.FutureContactMailName, vFCA.FutureContactMailSalutation, vFCA.FutureContactAddressFull, vFCA.FutureApplicationDate, vFCA.FutureEnrolYear, vFCA.FutureStatus, vFCA.FutureCampus, vFCA.FutureYearLevel

FROM SynergyOne.dbo.vFutureContactAddress vFCA

WHERE

(DATEPART(Year,FutureStatusChangeDate) >= DATEPART(Year,GETDATE())) AND
(DATEPART(DayofYear,FutureStatusChangeDate) >=
(DATEPART(DayofYear,GETDATE() -6))

Last modified: 3/09/2014 2:24:34 PM

See Also

Extracting data from the Synergetic database

What's new to Extracting data

Synergetic views

Extracting data using the Microsoft Query wizard

Editing a Microsoft Query using the wizard

Common duplication problems

Inserting an existing SQL statement

Extracting data using Microsoft Query directly

Extracting data for Crystal Reports

Using stored procedures to retrieve Synergetic data

© 2014 Synergetic Management Systems. Published 12 September 2014.

Top of page