Access Report / SQL DCount function - database

I have the following Tables and Fields in my DB:
tblProductionRecords:
pk_fldProductionRecordID,
fk_fldJobNumber_ID,
fldPartsCompleted,
tblJobs:
pk_fldJobID,
fldJobNumber
tblEmployee_ProductionRecord:
pk_fldEmp_ProdRec,
fk_fldProdRec_ID,
fk_fldEmployee_ID,
fldHours
tblEmployees:
pk_fldEmployeeID,
FldName
So what I am tracking is production records for a given job. The pieces of the production records are:
Part Quantities.
Which employee(s) were in involved with completing the quantities (it could be 1 or more employees, hence the Many to Many relationship between employees and production records)
And how many hours each employee spent completing the quantities.
The problem I am faced with is tracking my total quantities for a job on my report. When a given production record has ore that one employee that worked on those quantities, the quantities are added for each employee record. So lets same 3 employees work on a job and the 3 of them created completed 1000 parts. In my report, it will show a total of 3000 completed.
I understand creating groups, footer, headers, running sums and the Count function (I believe).
What I suspect I need is a field in my Query that is a sum of how many records are in tblEmployee_ProductionRecord where fldProdRec_ID = "the current production record" in the detailed section. With that number I can divide the total completed quantities between the 3 (or how ever many) employees and do a sum of that field.
I hope this is clear enough. I and sincerly appriciate any help!
David

If I understood you correctly, your example (3 employees work on a job and each one created 1000 parts) will look like this in the tables:
tblEmployee_ProductionRecord:
pk_fldEmp_ProdRec fk_fldProdRec_ID fk_fldEmployee_ID fldHours
1 1 1 5
2 2 2 5
3 3 3 5
tblProductionRecords:
pk_fldProductionRecordID fk_fldJobNumber_ID fldPartsCompleted
1 1 1000
2 1 1000
3 1 1000
If yes, try the following query:
SELECT
tblProductionRecords.fk_fldJobNumber_ID,
Count(tblEmployee_ProductionRecord.pk_fldEmp_ProdRec) AS EmployeeCount,
Sum(tblProductionRecords.fldPartsCompleted) AS CompletedSum
FROM
tblEmployee_ProductionRecord
INNER JOIN tblProductionRecords ON tblEmployee_ProductionRecord.fk_fldProdRec_ID
= tblProductionRecords.pk_fldProductionRecordID
GROUP BY
tblProductionRecords.fk_fldJobNumber_ID
HAVING
tblProductionRecords.fk_fldJobNumber_ID=1;
This will return the following result:
fk_fldJobNumber_ID EmployeeCount CompletedSum
1 3 3000
--> for Job number one, three employees created a total of 3000 parts.
Is this what you wanted?

Related

Measure that indicates sales volume per product per day (Google Data Studio)

I need to implement a measure that indicates sales volume per product per day. For the example table below (each line is a record of a sale):
id,create_date,report_date,quantity
329,2019-01-02 08:19:17,2019-01-02 14:34:12,6
243,2019-01-02 09:11:42,2019-01-03 15:30:14,6
238,2019-02-02 08:19:17,2019-03-02 14:36:17,2
170,2019-04-02 02:15:17,2019-04-02 14:37:12,2
238,2019-04-02 08:43:11,2019-04-02 14:41:01,8
238,2019-04-02 08:52:52,2019-04-02 14:39:12,1
238,2019-08-02 08:10:09,2019-08-02 15:02:12,1
238,2019-10-02 08:10:17,2019-10-02 18:34:11,1
170,2020-01-02 08:24:14,2020-01-02 19:31:31,2
170,2020-01-02 08:32:16,2020-01-02 21:52:32,3
The operations to reach the result:
1. Identify total sales and total products for each day.
For 2019-01-02, two sales were carried out, totaling 12 products (6 products for each sale on the day)
2. Divide total products by total sales, resulting in the product/sale ratio for the day (if the result is 2, it indicates that each sale on average corresponds to two products).
In the example table there are 6 different dates (YYYMMDD), for each corresponding date: total products/amount of sales on the day (12/2, 2/1, 11/3, 1/1, 1/1, 5/1) .
3. Average every day's story, resulting in a single value.
(3 + 2 + 3.6 + 1 + 1 + 3)/6 = 2.26 , indicating that on average two products are sold per sale per day.
As it involves many operations, I couldn't get a solution for this problem. If anyone can help me.
note: I accept alternative suggestions to offer the measure to indicate the volume of sales per product per day.
Please check the numbers given in your steps 2 and 3:
12/2=6 not 3
5/1 must be 5/2
I still think that you want to calculate a 'day story' in step 2, see formular below.
Here are the steps for generating such a value:
create a table
add your time as dimension and make it to date not date&time
order by date ascending (optional)
create a field day story with the formula sum(quantity)/count(id)
add this field three times to your table
click on the AUT left to the fieldname and select Running calculation to 'running average`
You have to convince your users to only look at the last line of the table.

Find Whole Duplicated Invoices in SQL

I'm trying to write some SQL to allow me to get result of possible duplicated Invoices that will have the same [same Items, with Same Quantity], That is possible to be Duplicate issued
Invoice items Average around 300 Item
Total Invoice To be Revision around 2500 Invoice
The Following is a invoices sample with only 1 items or so, but in real population items average is 300
Inv_ID Item_Code Item_Q
A-800 101010 24
A-801 101010 24
A-802 202020 9
A-803 101010 18
A-804 202020 9
A-805 202020 9
A-806 101010 18
Hoping The Excepted Result will be
A-800, A-801
A-802, A-804, A-805
A-803, A-806
But the invoice has around 200 item, and the duplicated invoices has to be has the same items and exact same quantity for these.
It's SQL_Server
And The Result need to match the whole Invoices item
Like Invoice A has 300 Different Items line with each one Quantity 2
The Results need to be all invoice has the exact same 300 Item with the Exact Quantity.
The Supplier has issued multiple duplicated invoice to our accounting
Department by mistakes over 4 years, it was discovered by chance, so
we need to find out the duplicated invoice to remove it from payment
schedule.
The issued invoices Need to has the exact different items with exact quantity to be considered duplicated.,,,
You didn't specify your DBMS product, so this answer is for Postgres.
select string_agg(inv_id, ',' order by inv_id) as inv_ids
from the_table
group by item_code, item_q
order by inv_ids;
Online example
Other DBMS products have similar functions to do the aggregation. In Oracle or SQL Server, you would use listagg(), in MySQL you would use group_concat(). The individual syntax would be slightly different (you have to check the manual), but the idea is the same.

SQL syntax to calculate total menu items per meal orderID across multiple meal orderIDs

I am new to SQL and Stack overflow and have a question about SQL Server syntax. i have searched online but I am not finding what I need and I would appreciate your assistance in this matter.
I have data in a source table for meal orders (each with a specific ID (e.g. 12345C) and items of each order (e.g. sandwich, drink, chips), each with an associated number starting with 1. For instance, the sandwich would have an item number of 1, the chips would be item # 2, and the drink would be item # 3 for the same orderID 12345C. The prior example would therefore have 3 rows of data in the source table for orderID 12345C.
My questions are these:
how do I use a SQL expression to determine the number of items per each order (e.g. 3 for the above example, which is also the maximum value for item number for each orderID)
and then add all of these items per order across hundreds of orders per day to determine the daily total number of items ordered.
So, if I had 3 orders in one day - one with 2 items, the second with 3 items, and the third with 4 items, I would like my final number to be 9.
This number is for use in a Sisense dashboard that allows SQL syntax in the field definition. Thank you for your help!
It is a bit difficult to explain but I am not able to use a query from a table because I am working with a dashboard in Sisense so I am adding fields in a pivot display and one of the fields I would like to include is the total count of order items per day (across several dozen orderIDs).
Here is an example of the data in the table: from the example I would like the final answer for orderID 1787588 to be 3 (there are 3 items within the order).

Looking to count the number of times a company name appears in a column, while excluding a duplicate value in another column

Spreadsheet column setup:
A:Name B:Hours C:Client D:Invoice# E:Date
Joe 8 Acme 123 8/21/18
Bill 12 Zorg 456 8/19/18
John 9 Acme 123 8/21/18
Ben 15 Acme 987 8/5/18
I want to count the number of jobs we did for a client for the month.
Each individual job is denoted by the invoice number. A job may have multiple people on it so the same invoice number will be duplicated in column D.
I'd like exclude those duplicates so I get a true count of jobs for a particular client regardless of how many people were actually on the job.
Using the example above, if I ask for total number of "Acme" jobs this month, I need the output to read 2 (since invoice 123 had a couple guys on it) not 3 due to it not excluding the duplicate invoice number.
Nothing I try or search on the internet like function CountIf quite meets my unique parameters.
Please advise, Thank you.

Database schema design for financial forecasting

I need to develop a web app that allows companies to forecast financials.
the app has different screens, one for defining employee salaries, another for sales projections etc..
basically turn an excel financial forecast model into an app.
question is, what would be the best way to design the database, so that financial reports (e.g. a profit and loss statement or balance sheet) could be quickly generated?
assuming the forecast period is for 5 years, would you have a table
with 5 years*12 months = 60 fields per each row? is that performant
enough?
would you use DB triggers to recalculate salary expenses whenever a single employee data is changed?
I'd think it would be better to store each month's forecast in its own row in a table that looks like this
month forecast
----- --------
1 30000
2 31000
3 28000
... ...
60 52000
Then you can use the aggregate functions to calculate forecast reports, discounted cash flow etc. ( Like if you want the un-discounted total for just 4 years):
SELECT SUM(forecast) from FORECASTS where month=>1 and month<=48
For salary expenses, I would think that having a view that does calculations on the fly (or if you DB engine supports "materialized views" should have sufficient performance unless we're talking some giant number of employees or really slow DB.
Maybe have a salary history table, that trigger populates when employee data changes/payroll runs
employeeId month Salary
---------- ----- ------
1 1 4000
2 1 3000
3 1 5000
1 2 4100
2 2 3100
3 2 4800
... ... ...
Then again, you can do SUM or other aggregate function to get to the reported data.

Resources