WEBVTT

1
00:00:00.180 --> 00:00:06.780
Hello, welcome to Knowledge Hitties course on Microsoft Access 2021 Intermediate.

2
00:00:06.780 --> 00:00:11.880
My name is John Veron and in these lessons you will learn about advanced queries.

3
00:00:11.880 --> 00:00:16.540
In this lesson, I will show you how to query with calculated values.

4
00:00:16.540 --> 00:00:21.000
Querying with calculated values means writing queries that return values which are not in

5
00:00:21.000 --> 00:00:23.060
the tables being queried.

6
00:00:23.060 --> 00:00:25.940
They will be calculated when the query runs.

7
00:00:25.940 --> 00:00:30.100
There are a couple of different ways we can do this in Microsoft Access, but before we

8
00:00:30.100 --> 00:00:35.120
do that, we need to create a table which we can use for the examples.

9
00:00:35.120 --> 00:00:54.070
Let's go ahead and create a table called products and let's give it names, field names of

10
00:00:54.070 --> 00:01:02.380
name which will be a product name and that will be short text.

11
00:01:02.380 --> 00:01:13.750
And then we'll have one called quantity type number, unit price type currency.

12
00:01:13.750 --> 00:01:23.490
Let's go to data sheet view and let's populate a few.

13
00:01:23.490 --> 00:01:26.130
Brose data maybe like four.

14
00:01:26.130 --> 00:01:32.810
We have product a at quantity 10 unit cost $87.

15
00:01:32.810 --> 00:01:35.130
This should be enough for examples.

16
00:01:35.130 --> 00:01:43.000
Let's make sure and save this table and then we can close it.

17
00:01:43.000 --> 00:01:47.840
The first way we can write a query using calculated values is with the query designer.

18
00:01:47.840 --> 00:01:52.940
So let's go to the create menu and go to the query designer and let's add our product

19
00:01:52.940 --> 00:01:56.980
table.

20
00:01:56.980 --> 00:02:02.240
In the first column, let's just add the name column so we can see the name of the product.

21
00:02:02.240 --> 00:02:06.840
For the second column, instead of choosing a column from the list, we're simply going

22
00:02:06.840 --> 00:02:09.000
to type in a formula.

23
00:02:09.000 --> 00:02:20.180
So we will do quantity times unit price.

24
00:02:20.180 --> 00:02:24.260
This will tell access to multiply the values in both of those columns and then show the

25
00:02:24.260 --> 00:02:25.260
result.

26
00:02:25.260 --> 00:02:29.220
Let's click run and see if that works.

27
00:02:29.220 --> 00:02:30.220
That looks good.

28
00:02:30.220 --> 00:02:35.740
The calculated part seems to work, but let's give that result column a better name.

29
00:02:35.740 --> 00:02:42.340
Switch back to design view and where it put in a default name, expr1, which stands for

30
00:02:42.340 --> 00:02:48.980
expression 1, let's change it to total price.

31
00:02:48.980 --> 00:02:50.900
Make sure you keep that colon after it.

32
00:02:50.900 --> 00:02:52.460
That's required.

33
00:02:52.460 --> 00:03:01.540
Now let's save this and then let's run it.

34
00:03:01.540 --> 00:03:03.940
It looks a little better.

35
00:03:03.940 --> 00:03:07.340
The second way we could do this query is directly in SQL.

36
00:03:07.340 --> 00:03:17.780
So let's create another query and we'll close this and switch the SQL view.

37
00:03:17.780 --> 00:03:21.420
First let's write the query without the calculated portion and make sure it works.

38
00:03:21.420 --> 00:03:24.300
Then we can add the calculated part in.

39
00:03:24.300 --> 00:03:32.730
So let's start with select the name of our product from product.

40
00:03:32.730 --> 00:03:33.810
Nice and simple.

41
00:03:33.810 --> 00:03:34.810
We run it.

42
00:03:34.810 --> 00:03:35.810
We get our products.

43
00:03:35.810 --> 00:03:36.810
That seems to work.

44
00:03:36.810 --> 00:03:40.170
Let's go back to SQL view.

45
00:03:40.170 --> 00:03:45.770
Now we're going to add another field to select after name so we need a comma.

46
00:03:45.770 --> 00:03:48.090
Then we put in what field we're going to select.

47
00:03:48.090 --> 00:03:50.330
This works just like in the query designer view.

48
00:03:50.330 --> 00:03:58.830
So we do quantity times unit price.

49
00:03:58.830 --> 00:04:11.050
And then we want to rename this so we use the as keyword as total here.

50
00:04:11.050 --> 00:04:14.850
If you use a column name that has a space in it, we have to put it in brackets in the

51
00:04:14.850 --> 00:04:15.970
query designer.

52
00:04:15.970 --> 00:04:18.490
We don't because it has a colon after it.

53
00:04:18.490 --> 00:04:21.250
So make sure you keep that difference in mind.

54
00:04:21.250 --> 00:04:25.370
I forgot to put a space in our unit price.

55
00:04:25.370 --> 00:04:28.610
And we run it and you can see that it works.

56
00:04:28.610 --> 00:04:32.330
These are the basics for how you would create calculated values and queries.

57
00:04:32.330 --> 00:04:37.890
Of course, your formulas can get more complicated, but the general principle stays the same.

58
00:04:37.890 --> 00:04:39.210
Thanks for watching.

59
00:04:39.210 --> 00:04:43.010
Stay tuned for the next lesson where I will show you how to work with sub queries.

