Access SQL: How to retrieve sums of multiple values where user IDs are assigned to multiple positions - sql

I'm working on an Access database for assigning tasks to personnel and tracking task status and workload. A single user ID can be assigned to one of many fields associated with a particular task. In this case, the Task table has fields for "TechReviewerID" "DesignerID" "TechReviewerWorkload" and "DesignerWorkload."
I want one query to return one row for each person, with two summary columns totaling all of the workload assigned to them. So if I'm ID1, I want column 3 to return the sum of "TechReviewerWorkload" in all tasks where "TechReviewerID = 1" and column 4 to return the sum of "DesignerWorkload" in all tasks where "DesignerID = 1."
I have successfully written two separate queries that accomplish this:
SELECT MESPersonnel.MESID, MESPersonnel.PersonnelName,
IIF(SUM(DesignerTask.DesignerWorkload) IS NULL, 0, SUM(DesignerTask.DesignerWorkload)) AS
TotalDesignerWorkload
FROM
(MESPersonnel LEFT OUTER JOIN Task AS DesignerTask ON (MESPersonnel.MESID =
DesignerTask.DesignerID
AND DesignerTask.DueDate < CDATE('2020-07-30') AND DesignerTask.DueDate > CDATE ('2020-05-01')))
WHERE MESPersonnel.PositionID = 1
GROUP BY MESPersonnel.MESID, MESPersonnel.PersonnelName;
This query gives the following table:
MESID PersonnelName TotalDesignerWorkload
1 John Doe 40
2 Dohn Joe 20
I can create a near-identical query by replacing all instances of "designer" terms with "tech reviewer" terms.
What I'm looking for is a table like:
MESID PersonnelName TotalDesignerWorkload TotalReviewerWorkload
1 John Doe 40 10
2 Dohn Joe 20 20
My attempts to combine these two via multiple outer joins resulted in wildly inaccurate sums. I know how to solve that for items on different tables, but I'm not sure how to resolve it when I'm using two items from the same table. Is there some kind of conditional sum I can use in my query that Access supports?
EDIT: Sample Raw Data
Task Table
TaskID DesignerID TechReviewerID DesignerWorkload TechReviewerWorkload DueDate
1 1 2 40 20 06-20-2020
2 2 1 20 10 06-20-2020
MESPersonnel Table
MESID PersonnelName
1 John Doe
2 Dohn Joe

Consider:
Query1: TaskUNION
rearranges data to a normalized structure
SELECT TaskID, DesignerID AS UID, PersonnelName, DesignerWorkload AS Data, DueDate, "Design" AS Cat FROM MESPersonnel
INNER JOIN Task ON MESPersonnel.MESID = Task.DesignerID
UNION SELECT TaskID, TechReviewerID, PersonnelName, TechReviewerWorkload, DueDate, "Tech" FROM MESPersonnel
INNER JOIN Task ON MESPersonnel.MESID = Task.TechReviewerID;
Query2:
TRANSFORM Sum(Data) AS SumData
SELECT UID, PersonnelName
FROM TaskUNION
WHERE DueDate BETWEEN #5/1/2020# AND #7/31/2020#
GROUP BY UID, PersonnelName
PIVOT Cat;
An alternative would involve 2 simple, filtered aggregate queries on Task table then join those 2 queries to MESPersonnel. Here as all-in-one statement:
SELECT MESID, PersonnelName, SumOfDesignerWorkload, SumOfTechReviewerWorkload
FROM (
SELECT DesignerID, Sum(DesignerWorkload) AS SumOfDesignerWorkload
FROM Task WHERE DueDate BETWEEN #5/1/2020# AND #7/31/2020# GROUP BY DesignerID) AS SumDesi
RIGHT JOIN ((
SELECT TechReviewerID, Sum(TechReviewerWorkload) AS SumOfTechReviewerWorkload
FROM Task WHERE DueDate BETWEEN #5/1/2020# AND #7/31/2020# GROUP BY TechReviewerID) AS SumTech
RIGHT JOIN MESPersonnel ON SumTech.TechReviewerID = MESPersonnel.MESID)
ON SumDesi.DesignerID = MESPersonnel.MESID;

Related

Filter with SQL Server by Group ID

I have two tables and I need to filter the data by filter id depends on the relation to to filter group id.
For example I have this two tables:
Table 1:
ItemID
FilterID
3
122
3
123
3
4
17
123
Table 2:
FilterID
FilterGroupID
122
5
123
5
4
1
If I search by filter id = 123 than all item id with this filter need to be returned.
If I search two or more different filter id that have different group id I need to get only the item ids that have all filter id and group id.
Desired output:
first input: 123 -> return item id =3 and item id = 17
second input: 123,4 -> return item id = 3 because filter id 123 belong to group id 5 and filter id 4 belong to group id 1 and item id 3 is the only one that has this two filters.
third input: 122,123 -> return item id =3 and item id = 17 because both filter id belong to same group.
I am getting a little lost with this query and I will be glad to get some help.
I’ll try to simplify it: Let’s say we have group filter of size and group filter of color. If I filter by size S or M than I need to get all items with this sizes. If I want to add color like blue than the answer will cut the result by: item with size S or M and Color blue. So filter from different group may cut some results
It seems that you want to get every ItemID which has at least one matching filter from each FilterGroupID within your filter input. So within each group you have or logic, and between groups you have and logic
If you store your input in a table variable or Table-Valued parameter, then you can just use normal relational division techniques.
This then becomes a question of Relational Division With Remainder, with multiple divisors.
There are many ways to slice this cake. Here is one option
Join the filter input to the groups, to get each filter's group ID
Use a combination of DENSE_RANK and MAX to get the total distinct groups (you can't use COUNT(DISTINCT in a window function so we need to hack it)
You can change this step to use a subquery instead of window functions. It may be faster or slower
Join the main table, and filter out any ItemIDs which do not have their total distinct groups the same as the main total
SELECT
t1.ItemID
FROM (
SELECT *,
TotalGroups = MAX(dr) OVER ()
FROM (
SELECT
fi.FilterID,
t2.FilterGroupID,
dr = DENSE_RANK() OVER (ORDER BY t2.FilterGroupID)
FROM #Filters fi
JOIN Table2 t2 ON t2.FilterID = fI.FilterID
) fi
) fi
JOIN Table1 t1 ON t1.FilterID = fi.FilterID
GROUP BY
t1.ItemID
HAVING COUNT(DISTINCT FilterGroupID) = MAX(fi.TotalGroups);
db<>fiddle

How do I stop my query from pulling duplicates?

Yes, I know this seems simple:
SELECT DISTINCT(...)
Except, it apparently isn't
Here is my actual Query:
SELECT
DeclinationReasons.Reason,
EmployeeInformation.ID,
EmployeeInformation.Employee,
EmployeeInformation.Active,
CompletedTrainings.DecShotDate,
CompletedTrainings.DecShotLocation,
CompletedTrainings.DecReason,
CompletedTrainings.DecExplanation,
IIf([DecShotLocation]="MCS","Yes","No") AS YesMCS,
IIf([DecReason]=1,1,0) AS YesAllergy,
IIf([DecReason]=2,1,0) AS YesImmune,
IIf([DecReason]=3,1,0) AS YesAdverse,
IIf([DecReason]=4,1,0) AS YesMedical,
IIf([DecReason]=5,1,0) AS YesSpiritual,
IIf([DecReason]=6,1,0) AS YesOther,
IIf([DecReason]=7,1,0) AS YesAlready
FROM
EmployeeInformation
INNER JOIN (CompletedTrainings
LEFT JOIN DeclinationReasons ON CompletedTrainings.DecReason = DeclinationReasons.ReasonID)
ON EmployeeInformation.ID = CompletedTrainings.Employee
GROUP BY
DeclinationReasons.Reason,
EmployeeInformation.ID,
EmployeeInformation.Employee,
EmployeeInformation.Active,
CompletedTrainings.DecShotDate,
CompletedTrainings.DecShotLocation,
CompletedTrainings.DecReason,
CompletedTrainings.DecExplanation,
IIf([DecShotLocation]="MCS","Yes","No"),
IIf([DecReason]=1,1,0),
IIf([DecReason]=2,1,0),
IIf([DecReason]=3,1,0),
IIf([DecReason]=4,1,0),
IIf([DecReason]=5,1,0),
IIf([DecReason]=6,1,0),
IIf([DecReason]=7,1,0)
HAVING
((((EmployeeInformation.Active) Like -1)
AND ((CompletedTrainings.DecShotDate + 365 >= DATE())
OR (CompletedTrainings.DecShotDate IS NULL))));
This is Joining a few tables (obviously) in order to get a number of records. The problem is that if someone is duplicated on the table with a NULL in one of the date fields, and a date in another field, it pulls both the NULL and the DATE, or pulls multiple NULLS it might pull multiple dates but those are not present right at the moment.
I need the Nulls, they are actual data in this particular case, but if someone has a date and a NULL I need to pull only the newest record, I thought I could add MAX(RecordID) from the table, but that didn't change the results of the query either.
That code:
SELECT
DeclinationReasons.Reason,
EmployeeInformation.ID,
EmployeeInformation.Employee,
EmployeeInformation.Active,
MAX(CompletedTrainings.RecordID),
CompletedTrainings.DecShotDate
...
And it returned the same issue, Duplicated EmployeeInformation.ID with different DecShotDate values.
Currently it returns:
ID
Active
DecShotDate
etc. x a bunch
1
-1
date date
whatever goes
2
-1
in these
2
-1
date date
columns
These are being used in a report, that is to determine the total number of employees who fit the criteria of the report. The NULLs in DecShotDate are needed as they show people who did not refuse to get a flu vaccine in the current year, while the dates are people who did refuse.
Now I have come up with one simple solution, I could add a column to the CompletedTrainings Table that contains a date or other value, and add that to the HAVING statement. This might be the right solution as this is a yearly training questionnaire that employees have to fill out. But I am asking for advice before doing this.
Am I right in thinking I need to add a column to filter by so that older data isn't being pulled, or should I be able to do this by pulling recordID, and did I just bork that part of the query up?
Edited to add raw table views:
EmployeeInformation Table:
ID
Last
First
empID
Active
Termdate
DoH
Title
PT/FT/PD
PI
1
Doe
Jane
982
-1
date
Sr
PD
X
2
Roe
John
278
0
date
date
Jr
PD
X
3
Moe
Larry
1232
-1
date
Sr
FT
X
4
Zoe
Debbie
1424
-1
date
Sr
PT
X
DeclinationReasons Table:
ReasonID
Reason
1
Allergy
2
Already got it
3
Illness
CompletedTrainings Table:
RecordID
Employee
Training
...
DecShotdate
DecShotLocation
DecShotReason
DecExp
1
1
4
date
location
2
text
2
1
4
3
2
4
4
3
4
date
location
3
text
5
3
4
date
location
1
text
6
4
4
After some serious soul searching, I decided to use another column and filter by that.
In the end my query looks like this:
SELECT *
FROM (
(
SELECT RecordID, DecShotDate, DecShotLocation, DecReason, DecExplanation, Employee,
IIf([DecShotLocation]="MCS","Yes","No") AS YesMCS, IIf([DecReason]=1,1,0) AS YesAllergy,
IIf([DecReason]=2,1,0) AS YesImmune, IIf([DecReason]=3,1,0) AS YesAdverse,
IIf([DecReason]=4,1,0) AS YesMedical, IIf([DecReason]=5,1,0) AS YesSpiritual,
IIf([DecReason]=6,1,0) AS YesOther, IIf([DecReason]=7,1,0) AS YesAlready
FROM CompletedTrainings WHERE (CompletedDate > DATE() - 365 ) AND (Training = 69)) AS T1
LEFT JOIN
(
SELECT ID, Active FROM EmployeeInformation) AS T2 ON T1.Employee = T2.ID)
LEFT JOIN
(
SELECT Reason, ReasonID FROM DeclinationReasons) AS T3 ON T1.DecReason = T3.ReasonID;
This may not have been the best solution, but it did exactly what I needed. Which is to get the information by latest entry into the database.
Previously I had tried to use MAX(), DISTINCT(), etc. but always had a problem of multiple records being retrieved. In this case, I intentionally SELECT the most recent records first, then join them to the results of the next query, and so on. Until I have all the required data for my report.
I write this in hopes someone else finds it useful. Or even better if someone tells me why this is wrong, so as to improve my own skills.

How to write a SQL query to calculate percentages based on values across different tables?

Suppose I have a database containing two tables, similar to below:
Table 1:
tweet_id tweet
1 Scrap the election results
2 The election was great!
3 Great stuff
Table 2:
politician tweet_id
TRUE 1
FALSE 2
FALSE 3
I'm trying to write a SQL query which returns the percentage of tweets that contain the word 'election' broken down by whether they were a politician or not.
So for instance here, the first 2 tweets in Table 1 contain the word election. By looking at Table 2, you can see that tweet_id 1 was written by a politician, whereas tweet_id 2 was written by a non-politician.
Hence, the result of the SQL query should return 50% for politicians and 50% for non-politicians (i.e. two tweets contained the word 'election', one by a politician and one by a non-politician).
Any ideas how to write this in SQL?
You could do this by creating one subquery to return all election tweets, and one subquery to return all election tweets by politicians, then join.
Here is a sample. Note that you may need to cast the totals to decimals before dividing (depending on which SQL provider you are working in).
select
politician_tweets.total / election_tweets.total
from
(
select
count(tweet) as total
from
table_1
join table_2 on table_1.tweet_id = table_2.tweet_id
where
tweet like '%election%'
) election_tweets
join
(
select
count(tweet) as total
from
table_1
join table_2 on table_1.tweet_id = table_2.tweet_id
where
tweet like '%election%' and
politician = 1
) politician_tweets
on 1 = 1
You can use aggregation like this:
select t2.politician, avg( case when t.tweet like '%election%' then 1.0 else 0 end) as election_ratio
from tweets t join
table2 t2
on t.tweet_id = t2.tweet_id
group by t2.politician;
Here is a db<>fiddle.

PostgreSQL Return Row if Value Exists in One of Several Columns

Ok, I am stuck on this one.
I have a PostgreSQL table customers that looks like this:
id firm1 firm2 firm3 firm4 firm5 lastname firstname
1 13 8 2 0 0 Smith John
2 3 2 0 0 0 Doe Jane
Each row corresponds to a client/customer. Each client/customer can be associated with one or multiple firms; the numeric value under each firm# columns corresponds to the firm id in a different table.
So I am looking for a way of returning all rows of customers that are associated with a specific firm.
For example, SELECT id, lastname, firstname where 8 exists in firm1, firm2, firm3, firm4, firm5 would just return the John Smith row as he is associated with firm 8 under the firm2 column.
Any ideas on how to accomplish that?
You can use the IN operator for that:
SELECT *
FROM customer
where 8 IN (firm1, firm2, firm3, firm4, firm5);
But it would be much better in the long run if your normalized your data model.
You should consider to normalize your tables, with the current schema you should join firms tables as many times as the number of firm fields in your customer table.
select *
from customers c
left join firms f1
on f1.firm_id = c.firm1
left join firms f2
on f2.firm_id = c.firm2
left join firms f3
on f3.firm_id = c.firm3
left join firms f4
on f4.firm_id = c.firm4
You can "unpivot" using a combination of array and unnest, as specified in this answer: unpivot and PostgreSQL.
In your case, I think this should work:
select lastname,
firstname,
unnest(array[firm1, firm2, firm3, firm4, firm5]) as firm_id
from customer
Now you can select from this table (using either a with statement or an inner query) where firm_id is the value you care about

Sql Query to get number of floors

I am working on a hotel management software and I need to display floors and the rooms on that floor......
I have a wing_master table in the database with following columns -:
wing_id,
wing_name,
floor,
floor_room_count,
status
Its like I have a record for one wing in that hotel which has 4 floors, but when I write a query to get the floors in that wing it just gives me "4" as the result in sql.....
I want the query to return it as follows -:
1
2
3
4
I want it this way so that I can use nested data-list control in asp.net....
My query is "select floors from wing_master where wing_id = 1"
For most databases (not MySQL), you can use a recursive query to obtain all floors:
with all_floors as (
select floors from wing_master where wing_id = 1
union all
select floors - 1 as floors from all_floors
where floors > 1
)
select * from all_floors order by floors;
SQLFiddle example.
In MySQL, the easiest way would be to create a numbers table that has a sequence of numbers up to the highest possible floor. Then join to that table to get all floors:
select num from wing_master
join numbers on
wing_id = 1 and
num <= floors;
SqlFiddle example.
Your query is ok, and also it seems that query and table structure will be fulfilling your requirements. can you show your data, because as per the structure, there should be four rows in the table, showing floor 1, 2, 3, 4
something like this
floor wing_id
1 1
2 1
3 1
4 1
If that is how your data looks, then your query must be ok, else there is some other issue. so do share your structure with few rows of data.