MS-Access UPDATE statistics per person - sql

Ive got this update query that uses employee activity stored in two tables (Beldata_filter & Urenlog_filter) to create a performance analysis per employee in another table (Bellers_filter).
however, i cannot get my query to update the performance stats per employee using only the activities of that employee in the other two tables, instead, access updates the stats per employee to the total activities of all employees.
here is the update query that i have written, the '.Beller' and '.Naam' signify columns in the tables with the names of the employees that i would like access to distinguish from one another.
UPDATE (Bellers_Filter INNER JOIN Urenlog_Filter ON Bellers_Filter.Naam=[Urenlog_Filter].Naam) INNER JOIN Beldata_Filter ON Bellers_Filter.[Naam]=[Beldata_Filter].Beller SET
Bellers_Filter.[Num b] = Dcount("[Beller]","[Beldata_Filter]"),
Bellers_Filter.[Num o] = Dcount("[Opgenomen]","[Beldata_Filter]","Opgenomen = 1"), Bellers_Filter.[Num nno] = Dcount("[Actie]","[Beldata_Filter]","Actie = 3"),
WHERE ([Beldata_Filter].Beller=[Urenlog_Filter].Naam);
The source table Beldata_filter looks like this:
Beller
ID
Moment
Opgenomen
Actie
Robert
55
8-11-2022
1
1
Susan
56
8-11-2022
1
1
Robert
55
9-11-2022
1
2
Robert
55
9-11-2022
0
3
Susan
56
9-11-2022
1
1
in this table each observation describes an action conducted by an employee. The other source table Urenlog_filter has got the same format, in this case the significance of the data in the tables is not as important as the observations of the data.
The table that will be updated needs to look like this
Id
Naam
Num b
Num o
Num nno
1
Robert
3
2
1
2
Susan
2
2
0
This is the desired result in its most simple form, the update query recognises 3 observations for robert and 2 for Susan under "Num b". Right now, the table looks like this
Id
Naam
Num b
Num o
Num nno
1
Robert
5
4
1
2
Susan
5
4
1
Who can help me with this problem? If you guys need more info please let me know!

Saving aggregate data is usually unnecessary and bad design. If it can be calculated for UPDATE, it can be calculated when needed.
SELECT Beldata_Filter.ID, Beldata_Filter.Beller,
Count(Beldata_Filter.Moment) AS [Num n],
Count(IIf([Opgenomen]=1,[Opgenomen],Null)) AS [Num o],
Count(IIf([Actie]=3,[Actie],Null)) AS [Num nno]
FROM Beldata_Filter
GROUP BY Beldata_Filter.ID, Beldata_Filter.Beller;
However, if you really must save, I don't see need for JOINing tables.
Include filter criteria in DCount() for names.
UPDATE Bellers_Filter SET
[Num b] = DCount("*","Beldata_Filter","Beller='" & [Naam] & "'"),
[Num o] = DCount("*","Beldata_Filter","Beller='" & [Naam] & "' AND Opgenomen=1"),
[Num nno] = Dcount("*","[Beldata_Filter]","Beller='" & [Naam] & "' AND Actie = 3");

Related

Oracle SQL - Need to eliminate data if at least one of the particular condition is not satisfied

My question is related to Oracle sql. I have a two tables say, study table and another one is study part table. Stdyno is the primary key in study table and (stydyno + sqncno) is the primary key in studypart table.
EG: studypart table has data as below.
studyNo sqnc part approvalIN
--------------------------------
123 1 fgh Y
123 2 jhf N
123 3 rty N
456 1 wer N
456 2 wdg N
456 3 ghg N
I need query in such a way that my output from studypart table gives result
as study number which has all the approvalIn as N. If it has at least one of the approvalIn as 'Y'
then that studyno should be excluded from the result.
Desired output:
studyno: 456
I tried this implementation in stored procedure taking Y and N approvalIn count separately ie,
if a studyno has both the count then exclude it and
if it has only one count say either N or Y the include it.
But i would like to know how to achieve this is query.
You can do it by excluding those rows whose count of "approvalIN = 'N'" does not match the total count of "approvalIN" values.
SELECT STUDYNO
FROM tab
GROUP BY STUDYNO
HAVING SUM(CASE WHEN approvalIN = 'N' THEN 1 END) = COUNT(approvalIN)
Check the demo here.

Select top n (variable) for each criteria in a table based on another table

I want a VBA code to make a query to show Equip with Top ActiveTime for each ModelID (from 1st table) based on TopN for each ModelID (from the 2nd table), I know i have to use QueryDef and Sql VBA but I can't figure how to write the code
Just and Example to illustrate
My 1st table is
EquipID
Equip
ActimeTime
ModelID
1
DT1
10
1
2
DT2
6
1
3
DT3
13
1
4
DT4
15
1
5
DT5
16
2
6
DT6
12
2
7
DT7
6
2
8
DT8
13
2
My 2nd Table is
ModelID
Model
TopN
1
775
3
2
789
2
So the query result should be like (Showing the Top 3 of 775 Model and the Top 2 of 789)
Equip
ActimeTime
Model
DT4
15
775
DT3
13
775
DT1
10
775
DT5
16
789
DT8
13
789
Thanks a lot in advance, I'm really stuck at this one and solving this will help me a lot in my project
[Table1][1]
[1]: https://i.stack.imgur.com/geMca.png
[Table2][2]
[2]: https://i.stack.imgur.com/lMPDP.png
[Query Result][3]
[3]: https://i.stack.imgur.com/cGf6k.png
You can do it in straight SQL - but oooh is it ugly to follow and construct
I created 4 queries with the final one resulting in what you're looking for.
The key was to get a RowID based on the sorted order you're looking for (Model and ActimeTime). You can get a pseudo Row ID using Dcount
Here's the 4 queries - I'm sure you can make one mashup if you're daring
My tables are Table3 and Table4 - you can change them in the first query to match your database. Build these queries in order as they are dependent on the one before them
qListModels
SELECT Table3.Equip, Table3.ActimeTime, Table4.Model, Table4.TopN, "" & [Model] & "-" & Format([ActimeTime],"000") AS [Model-ActTime]
FROM Table3 INNER JOIN Table4 ON Table3.ModelID = Table4.ModelID
ORDER BY Table4.Model, Table3.ActimeTime DESC;
qListModelsInOrder
SELECT qListModels.*, DCount("[Model-ActTime]","[qListModels]","[Model-ActTime]>=" & """" & [Model-ActTime] & """") AS row_id
FROM qListModels;
qListModelStartRows
SELECT qListModelsInOrder.Model, Min(qListModelsInOrder.row_id) AS MinOfrow_id
FROM qListModelsInOrder
GROUP BY qListModelsInOrder.Model;
qListTopNModels
SELECT qListModelsInOrder.Equip, qListModelsInOrder.ActimeTime, qListModelsInOrder.Model
FROM qListModelsInOrder INNER JOIN qListModelStartRows ON qListModelsInOrder.Model = qListModelStartRows.Model
WHERE ((([row_id]-[MinOfrow_id])<[TopN]))
ORDER BY qListModelsInOrder.Model, qListModelsInOrder.ActimeTime DESC;
This last one can be run anytime to get the results you want
Example Output:

Google sheets queries with IF statments

I am trying to write this query for a while and have tried a few sites but not explain really how to use the IF statement.
So I have data in columns that are people scores for films
Jim
Jack
Fred
Film 1
6
6
2
Film 2
7
8
5
Film 3
3
5
8
...
for example and i want a query that when i select a cell with a name in it it will return the scores for that person (make top ten and scores by film genera / person etc.)
So I have this query I know the syntax is not correct I have tried many ways and cant get it to work. (E18 is the cell I with a pick list of the names)
=Query(Sheet1!A4:D,"select A & IF(E18=”Jim”, select B order by B desc Limit 10"")")
What I want to do is when I select a name then it returns the top 10 films for that user + the scores
Any help greatly received.
Thankyou
aaron
Let assume you will select actor name in F1 cell (as per my attached screenshot). They try below QUERY() formula.
=QUERY(A2:D4,"select A, " & CHAR(MATCH(F1,A1:D1,0)+64) & " order by " & CHAR(MATCH(F1,A1:D1,0)+64) & " desc limit 10")

Is there an SQL query that will count the number of occurrences of an event WITHOUT grouping the data by the event being counted?

Note that I'm doing this in MS Access, so a solution using basic SQL operators would be appreciated.
Suppose you have a table where each row represents a coin flip in a series of coin flips.
Disclaimer: I'm using coin flips as an analogy so I don't have to explain my actual data set.
SELECT * FROM CoinFlips;
Id Flip Time
-------------------
1 Heads 1
2 Tails 2
3 Heads 3
4 Heads 4
5 Heads 5
6 Tails 6
How would you write a query that returns all of the rows above with an additional column that counts the number of 'head' flips that occurred up to that row's occurrence. In other words, this is what I want the result to look like:
Desired Output
Id Flip Time NumHeads
--------------------------------
1 Heads 1 1
2 Tails 2 1
3 Heads 3 2
4 Heads 4 3
5 Heads 5 4
6 Tails 6 4
To do this in MS Access, you need a correlated subquery or join/aggregation. Other databases have direct support for this functionality, but not MS Access.
select cf.*,
(select count(*)
from CoinFlips as cf2
where cf2.flip = 'Heads' and cf2.id <= cf.id
) as NumHeads
from CoinFlips as cf;
Well here is a solution
SELECT coinFlips.ID
,coinFlips.Flip
,coinFlips.TIME
,DCount("Flip", "coinFlips", "ID <=" & [Time] & "AND Flip ='Heads'") AS CountHeads
,DCount("Flip", "coinFlips", "ID <=" & [Time] & "AND Flip ='" & [Flip] & "'") AS CountEachOccurence
FROM coinFlips;

access SQL count results using multiple sub queries against one table

I am using Access with a table having over 200k rows of data. I am looking for counts on a column which is broken down by job descriptions. For example, I want to return the total count (id) for a location where a person is status = "active" and position like "cook" [should equal 20] also another output where I get a count (id) for the same location where a person is status = "active" and position = "Lead Cook" [should equal 5]. So, one is a partial of the total population.
I have a few others to do just like this (# Bakers, # Lead Bakers...). How can I do this with one grand query/subquery or one query for each grouping.
My attempt is more like this:
SELECT
a.location,
Count(a.EMPLOYEE_NUMBER) AS [# Cook Total], --- should equal 20
(SELECT count(b.EMPLOYEE_ID) FROM Table_abc AS b where b.STATUS="Active Assignment" AND b.POSITION Like "*cook*" AND b.EMPLOYEE_ID=a.EMPLOYEE_ID) AS [# Lead Cook], --- should equal 5
FROM Table_abc AS a
ORDER BY a.location;
Results should be similar to:
Location Total Cooks Lead Cooks Total Bakers Lead Bakers
1 20 4 15 2
2 45 7 12 2
3 22 2 16 1
4 19 2 17 2
5 5 1 9 1
Try using conditional aggregation -- no need for sub queries.
Something like this should work (although I may not understand your desired results completely):
select location,
count(EMPLOYEE_NUMBER) as CookTotal,
sum(IIf(POSITION Like "*cook*",1,0)) as AllCooks,
sum(IIf(POSITION = "Lead Cook",1,0)) as LeadCooks
from Table_abc
where STATUS="Active Assignment"
group by location