473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

Queries in Access using SQL View

MMcCarthy
14,534 Expert Mod 8TB
Rather than using the Access design view change the view to SQL. I am going to attempt to outline the general syntax used for SQL queries in Access.

Angle brackets <> are used in place of some syntax elements you must supply. The description of these elements will be in the contained in the angle brackets.

Square brackets [] are used to show which parts are optional.

Basic SELECT query

SELECT <field list>
FROM <table/query name(s)>
[WHERE <set critieria on field list>]
[GROUP BY <fields to group values on>]
[HAVING <set criteria on grouped fields>]
[ORDER BY <fields to sort by> ASC/DESC];

Parameters

Goes before a SQL statement to define the data types of any parameters you include in a query. This is useful when you are asking the user to input values or matching values in controls on an open form.
Expand|Select|Wrap|Line Numbers
  1. PARAMETERS [Enter Date:] DateTime
  2. SELECT OrderID, OrderDate
  3. FROM tblOrders
  4. WHERE OrderDate = [Enter Date:]
  5.  
Exists

You can use this keyword to test whether you can return values that satisfy selection in a subquery. The following query will return all customer who have been issued an invoice.

Expand|Select|Wrap|Line Numbers
  1. SELECT CustID
  2. FROM tblCustomer
  3. WHERE EXISTS
  4. ( SELECT * FROM tblInvoice
  5.   WHERE tblInvoice.CustID = tblCustomer.CustID)
  6.  
Aggregate Functions

Creating SQL statements which include aggregate functions like Avg(), Count(), Sum(), etc. usually requires the GROUP BY clause.

If you place an aggregate function on a field then all fields returned by the query must be included in an aggregate function or a GROUP BY clause. The following query will return the total amount of fees paid by a member.

Expand|Select|Wrap|Line Numbers
  1. SELECT tblMember.MemberID, tblMember.MemberName, Sum(tblFees.Fee)
  2. FROM tblMember INNER JOIN tblFees
  3. ON tblMember.MemberID = tblFees.MemberID
  4. GROUP BY tblMember.MemberID, tblMember.MemberName;
  5.  
If you include any other fields in the above query (in the GROUP BY clause) then the sum of the fees would be broken out over that field as well.

Crosstab Queries

Crosstab queries can be used to summarise a single value using the values in another column and using other column(s) to define the grouping by rows. The syntax is:

TRANSFORM <expression using total function>
<SELECT Statement>
PIVOT <field to summarise on>
[IN <value list>]

For example, to get the total sales per salesperson by month you can do the following:

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(tblInvoice.InvoiceAmt) As Total Sales
  2. SELECT SalepersonID, FirstName, Surname
  3. FROM tblSalesPeople INNER JOIN tblInvoice
  4. ON tblSalesPeople.SalespersonID = tblInvoice.SalespersonID
  5. WHERE Year(tblInvoice.InvoiceDate) = Year(Now())
  6. PIVOT MonthName(Month(tblInvoice.InvoiceDate), True)
  7. IN ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  8.  
Some other useful articles on SQL Queries

Basic SQL Syntax for Access Queries
SQL JOIN's
Subqueries in SQL
Stored Query vs SQL Statement
Nov 28 '07 #1
0 7618

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
8
by: Mike N. | last post by:
Hello: I am new to T-SQL programing, and relativly new to SQL statements in general, although I have a good understanding of database theory. I'm a little confused as to the fundamental...
4
by: dschl | last post by:
Hi, I'm converting an Access 2000 database to Sql Server and must be missing something obvious. Using the Import utility in Sql Server, the Access queries seem to get executed and the...
6
by: Brad | last post by:
I have an sql query that has specific criteria (like state='PA' or state = 'NJ'...) and would like to be able to have the user specify the criteria dynamically either through the web or from...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
3
by: NeilAnderson | last post by:
I'm a fairly new user of access & I've never had any training, so I'm wondering if I'm doing the right thing here, or if it matter at all. I'm building a database for room booking purposes and I'm...
5
by: Jerry Hull | last post by:
I'm working with a database developed by an untrained person over several years - and on a network that has recently been upgraded with a new server installed and MS office upgraded from 2K (I...
7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
1
by: Kurch | last post by:
Hello, I have an Access file saved on my company server. Is it possible to allow selected people within my network to access a query that I've written and allow them to read and write changes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.