I am trying out Access for the first time and I am trying to figure out how to really harness the amazing power of Access.
With that, I want to find the average quantity of each item but do not know how to do that. I set up 2 tables. One is the list of items (sand, water, etc.) then set up a relationship to another table that put out the quantities and price.
Below, Query 1 I set up and sorted it (I don't know why it isn't alphabetical even though I sorted ascending, but thats not why I'm here). So could someone help me try and figure out how to average each items amounts like in Query 2? Ultimately, I really just want the query to find the MaxAve and the MinAve and just show those 2 like in Query 3 below.
Query 1 Query 2 Query 3
fldName fldNum fldName fldAve fldName fldAve
Sand 4 Sand 4 Sand 4
Sand 4 Water 3.8 Computer 3.35
Water 3.7 Soda 3.43
Water 4 Computer 3.35
Water 3.7 Phone 3.43
Soda 3.7 Pencil 3.75
Soda 3.3
Soda 3.3
Computer 3.7
Computer 3
Phone 3
Phone 3.3
Phone 4
Pencil 4
Pencil 4
Pencil 3
Like I said I'm very new to Access and I am just trying things out. If you recommend setting up the query a different way Im open to anything really. The more I learn the better.
To get the average for each fieldname, just do this in a query:
SELECT FieldName, Avg(FieldNum) as AvgQty
FROM MyTable
GROUP BY FieldName
ORDER BY FieldName
This will also sort the fieldname column for you.
EDIT:
I think you can do what you asked for in your comment, but you will need a separate query based on the one above. You'll need a UNION query to do it. Something like:
SELECT Top 1 FieldName, AvgQty
FROM Query1
ORDER BY AvgQty ASC
UNION ALL
SELECT Top 1 FieldName, AvgQty
FROM Query1
ORDER BY AvgQty DESC
I'm doing that off the top of my head, so it may not be perfect.
As for formatting the field, do a google on "VBA Format Number" and you should be able to find some examples. I think it might make the above query look something like:
SELECT Top 1 FieldName, Format(AvgQty, "##.#") as AvgQty1
FROM Query1
ORDER BY AvgQty ASC
UNION ALL
SELECT Top 1 FieldName, Format(AvgQty, "##.#") as AvgQty1
FROM Query1
ORDER BY AvgQty DESC
Related
I need your help in one SQL Query for converting the example table given below. Where I need Facilities as columns.
Seasonid
Product
Facility
Price
Product Type
1
Socks
Montreal
24
Wool
2
Slippers
Mexico
50
Poly
3
Slippers
Montreal
27
Rubber
4
Socks
Mexico
24
Cotton
5
Socks
Montreal
26
Cotton
Below table is how I'm expecting it to look like
seasonid
Product
Montreal
Mexico
Product Type
1
Socks
24
0
Wool
2
Slippers
0
50
Poly
3
Slippers
27
0
Rubber
4
Socks
0
24
Cotton
5
Socks
26
0
Cotton
In the expected result table even though 5th row data can be accommodated in 4th row itself like
seasonid
Product
Montreal
Mexico
Product Type
4
Socks
26
24
Cotton
my requirement requires it in a different row.
I found some pivot examples online, but they only show averaging or summing up the values and won't add the rows to already existing columns and display them all. I couldn't find a relevant post for this question. Please let me know if there is any relevant post.
Is it possible with Sql in the first place? If yes, then how?
I think you're mistaken about the pivot part because there's no pivot happening here. This can be achieved with IF() or CASE expression functions added to a very basic MySQL syntax. So, this:
SELECT * FROM mytable;
Will return you all columns and rows. Then this:
SELECT Seasonid, Product, Facility, Price, ProductType
FROM mytable;
is basically the same as the query before-that will return all columns and rows. Only difference is here we're explicitly defining all the column names from the table. So, from here you only need to modify the Facility and Price part with conditional IF() or CASE() and define it twice in the SELECT section, like this:
SELECT seasonid,
Product,
CASE WHEN Facility='Montreal' THEN Price ELSE 0 END AS 'Montreal',
CASE WHEN Facility='Mexico' THEN Price ELSE 0 END AS 'Mexico',
ProductType
FROM mytable;
Or
SELECT seasonid,
Product,
IF(Facility='Montreal',Price,0) AS 'Montreal',
IF(Facility='Mexico',Price,0) AS 'Mexico',
ProductType
FROM mytable;
In which both queries are able to get your desired result.
Demo fiddle
pnr mnd pris
1 1 600
1 7 900
2 1 600
2 7 600
3 1 40
3 7 40
I have trouble how to sum specific rows on the columns. Looking at the above, the table is called travel and it has 3 columns:
pnr - Personal Number
mnd - Month
Pris - Price
So what I want is to sum total of the price for the a specific month, so in this case, it should be 1240 USD and month 1. For the month 7, it should be 1540 USD.
I have trouble to do the query correct. So far from I have tried is this:
SELECT t.rnr, t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
The result I get is 3720 USD which I have no idea how the SQL managed to calculate this for me.
Appreciate if someone could please help me out!
For this you need to drop the pnr column from the output (it is not relevant and will cause your data to split) and add a GROUP BY:
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
GROUP BY t.mnd
Live demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b34ec2bb9c077c2d74ffc66748c5c142
(The use of an aggregate function without grouping, as you've got now, is not a standard SQL feature and can often be turned off in MySQL. If turned on, you might not always get the result you expected/intended.)
just group your result with mnd column
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
group by t.mnd
The title probably doesn't accurately describe my question, sorry about that.
I have two tables:
2016Athletes:
Name Position 40_dash Year Overall
Jared Goff QB 4.82 2016 1
Carson Wentz QB 4.77 2016 2
Joey Bosa DE 4.88 2016 3
Ezekiel Elliott RB 4.47 2016 4
Jalen Ramsey CB 4.41 2016 5
............
and
2017Athletes:
Name Position 40_dash Year Overall
Myles Garrett DE 4.64 2017 1
Mitchell Trubisky QB 4.67 2017 2
Solomon Thomas DE 4.69 2017 3
Leonard Fournette RB 4.51 2017 4
Corey Davis WR NULL 2017 5
..............
I first want to merge the 40_dash columns and find the average, which I could do a couple ways, but the real problem I'm having is that after I find the average, I then want to group by position. I'm not sure it it's relevant, but the goal is to eventually select one particular athlete I know to be an impressive outlier and compare him to the average for all athletes, all defensive players, and then all linebackers and this grouping is my initial baseline for the following queries in my report.
My best crack at it would be
select avg(40_dash)
from ((select 40_dash from 2016Athletes) union ALL
(select 40_dash from 2017Athletes)
)sl GROUP BY Position;
However, I know that won't work because the group by function is not correct. I'm not sure how to called the 'unioned' column once I have them merged.
Afterwards, I want to find each athlete with the fasted 40_dash time, also grouped by position. A min() function would seem to do the job, but again the group by is what's tripping me up.
Thanks to anyone who can assist!
You need to select the position column in the subquery, so you can group by it in the main query.
SELECT Position, AVG(40_dash)
FROM (
SELECT Position, 40_dash FROM 2016Athletes
UNION ALL
SELECT Position, 40_dash FROM 2017Athletes
) as x
GROUP BY Position
See SQL select only rows with max value on a column for how to find the player with the fastest time in each group. You can then join that query with the above query, to compare the fastest to the average.
I am wondering if any of you would be able to help me. I am trying to loop through table 1 (which has duplicate values of the plant codes) and based on the unique plant codes, create a new record for the two other tables. For each unique Plant code I want to create a new row in the other two tables and regarding the non unique PtypeID I link any one of the PTypeID's for all inserts it doesnt matter which I choose and for the rest of the fields like name etc. I would like to set those myself, I am just stuck on the logic of how to insert based on looping through a certain table and adding to another. So here is the data:
Table 1
PlantCode PlantID PTypeID
MEX 1 10
USA 2 11
USA 2 12
AUS 3 13
CHL 4 14
Table 2
PTypeID PtypeName PRID
123 Supplier 1
23 General 2
45 Customer 3
90 Broker 4
90 Broker 5
Table 3
PCreatedDate PRID PRName
2005-03-21 14:44:27.157 1 Classification
2005-03-29 00:00:00.000 2 Follow Up
2005-04-13 09:27:17.720 3 Step 1
2005-04-13 10:31:37.680 4 Step 2
2005-04-13 10:32:17.663 5 General Process
Any help at all would be greatly appreciated
I'm unclear on what relationship there is between Table 1 and either of the other two, so this is going to be a bit general.
First, there are two options and both require a select statement to get the unique values of PlantCode out of table1, along with one of the PTypeId's associated with it, so let's do that:
select PlantCode, min(PTypeId)
from table1
group by PlantCode;
This gets the lowest valued PTypeId associated with the PlantCode. You could use max(PTypeId) instead which gets the highest value if you wanted: for 'USA' min will give you 11 and max will give you 12.
Having selected that data you can either write some code (C#, C++, java, whatever) to read through the results row by row and insert new data into table2 and table3. I'm not going to show that, but I'll show how the do it using pure SQL.
insert into table2 (PTypeId, PTypeName, PRID)
select PTypeId, 'YourChoiceOfName', 24 -- set PRID to 24 for all
from
(
select PlantCode, min(PTypeId) as PTypeId
from table1
group by PlantCode
) x;
and follow that with a similar insert.... select... for table3.
Hope that helps.
I've setup a FTS on a single field, in a single table.
Field: Name NVARHCHAR(350) NOT NULL
Now, when i search for the following
1 ave
10 ave
i don't get back the results i expect.
Firstly, the search query 1 ave is transformed into "1*" AND "ave*". Now i run my CONTAINS(..) query...
SELECT FooId, Name
FROM [dbo].[Names]
WHERE CONTAINS(Name, #SearchQuery)
Then, along with the correct results, i also get these incorrect results back...
2 Ave (a couple of entries .. but they are all unique entires).
So, how did this get retrieved? there is no 1* in that piece of text? Its like .. the number is ignored?
Also - and this is important - i've removed an reference to a stop list AND rebuilt the catalog.
Hmm. I'm so confused. anyone have any suggestions?
The "1" can occur anywhere within the full text search indexed column it doesn't have to be directly before (or even before) the "Ave", is there a 1 somewhere else in that row?
Full text indexing will find derivitaves of words - like if you search for RUN, it might find RUNNING, RAN, RUN, etc.
I wonder if its deciding that 2 is near 1, and returning that as a near match. You should try switching your query to a CONTAINSTABLE query so that you can also evaluate the RANK to determine which of the answers is a closer match. You could then decide on a threshold and filter out any rows that don't meet your criteria as to how close of a match they are.
EDIT: Its not doing the inflection thinking 1 is near 2. I ran a test query on a sample table that looked like this...
PK Name
1 1 ave
2 10 ave
3 2 ave
4 12 avenue
5 13 avenue
6 100 ave.
7 200 ave
8 210 avenue
Here's the query I ran...
select *
from Table_1
where contains(name, '"1*" and "ave*"')
And here's the results I get...
PK Name
2 10 ave
4 12 avenue
5 13 avenue
6 100 ave.
The interesting thing here is that the first record in the table isn't found. (I ran this on SQL 2008 Dev edition). Based on those results (where nothing starting with 2 was found) - I'd double-check your query. Maybe post the full text of your entire query, including where the search variable is being set.