MS access calculated field sub table - sql

I would like to add a calculated field to many of my tables and will be able to use this new technique many times.
I have sub records such as values for SalePrice and I would like to have these totals show in an employees record.
I would appreciate a sample query and how to implement considering the following data:
Table 1 (Employees): ID, EmployeeName, [Calculated Field]
Table 2 (Sales): ID, InventoryItem, SalePrice, QuantityOrdered
I would like to fetch [SalePrice] x [QuantityOrdered] in 2 scenarios:
Total Sales to date
Total Sales within a date range (where these 2 values can be entered
on a form) for administration purposes as our employees are paid
commission only.
I'm used to adding table fields in both Layout and Design view.
Kind regards, Mikey.

This should get you started: MS Access SQL: http://office.microsoft.com/en-nz/access-help/access-sql-basic-concepts-vocabulary-and-syntax-HA010256402.aspx. Calculated fields in queries: http://office.microsoft.com/en-nz/access-help/create-a-calculated-field-in-a-query-mdb-HP005188023.aspx.

Related

Filtering table based on customerID and closest transaction date to unique dates

Using Microsoft SQL Server Management Studio I am accessing a SQL Server table (TransHistory) that contains CustomerID and TransactionDate as my two fields of interest. Separately I have a list of CustomerIDs and Dates of Interest in an excel spreadsheet. I am trying to query the list of customers from excel spreadsheet in TransHistory where it outputs the closest past TransactionDate to the Date of Interest for that CustomerID.
TransHistory has hundreds of thousands of customers+transactions for which I'm querying for about a thousand customers and only need 1 unique row per customer. The Dates of Interest are unique to each customer (example entries from spreadsheet below) and won't always match the TransactionDate in TransHistory table, which is why I am querying for the closest past date to the Date of Interest.
CustomerID
Date of Interest
11111
1/21/2022
22222
2/28/2013
33333
5/13/2018
The thing is I know how to do it in a multistep process of first querying all data in TransHistory for my CustomerIDs and then doing a subsequent query to then filter for the Date of Interest. When trying to simply/combine it into 1 query I get stumped by the fact that Date of Interest is unique to each CustomerID.
Would it be possible to filter for CustomerID while also filtering for CustomerID specific Dates of Interest in the same query? I am thinking it will require a two-dimensional array as a where statement, but I've never worked with them and not sure how to begin setting this up.
Would appreciate being pointed in the right direction. Thanks!
I would provide the server with "Date of Interest" data and let the SQL Sever do the job
select top(1) with ties t.*
from Dates_of_Interest d
join TransHistory t on t.CustomerID = d.CustomerID
order by row_number() over(partition by d.CustomerID order by abs(datediff(d, t.TransactionDate, d.Date_of_Interest)))

Issue creating calculated field column in SQL

This is the SQL I have so far:
SELECT RESERVATION.RESERVATION_ID, RESERVATION.TRIP_ID, CUSTOMER.CUSTOMER_NUM,
RESERVATION.NUM_PERSONS,RESERVATION.TRIP_PRICE
FROM RESERVATION, CUSTOMER
WHERE NUM_PERSONS > '4';
I need to create a column named TOTAL_PRICE that calculates the price for all the reservations with more than 4 people. I've tried multiple different ways and I'm still really confused.
The code I have works and it shows all reservations that have more than 4 people attending. But I need the price for each reservation to be calculated in a separate column.
I think you just want an aggregation query:
SELECT SUM(r.TRIP_PRICE)
FROM RESERVATION r
WHERE NUM_PERSONS > 4;

Crystal 2016 pull from 2 unrelated tables in one report

I have searched and the only answers I found were for cross joining.
I have 3 tables that are related by 1 field only. I'm trying to pull data from 2 tables that are linked to the other table.
The first table contains salesman data IDnumber, name, address, phone number, hire date, wage, etc.
There is a sales table that contains salesmanIDnumber, date of sale, object sold, and price.
There is a purchases table that contains salesmanIDnumber, date of purchase, object purchased, and price.
The date fields in sales and purchases are unrelated. I know the easiest solution would be to have the sales and purchase table combined with a column for buy/sell, but I didn't create the database and I'm working with what I've got. basically I want to pull all purchases or sales by salesmanID in one report.
I have linked the salesman table to the sales table and the purchases table with left outer joins by the salesman ID. What I'm getting in results is cross join with each result from the purchase table displayed once for each result in the sales table, which gives me multiplied results instead of added. for example, 4 sales and 6 purchases would be 10 entries, but I'm getting 24 results.
I tried entering an example but the site stripped the spacing and pushed everything together basically making it unreadable.
how can I get it to show data from both tables independently?
I do have access to create views in the database if that's the best solution, but I'm not proficient at it.
Create 2 views (one for sales, the other for purchases), each Grouped By SalesMan.
Since each SalesMan would have only one row in each view, you can join them without record inflation.
Or use a UNION to append Purchase records to Sales Records, taking care of including a 'Type' column ('Sales' as Type, or 'Puurchases' as Type) and/or reverse sign on quantities to allow summarizing things in a logical.

Can't get sub query to work

I have 2 queries that work fine by themselves but I've been struggling to get them to work together. I have a table of audits and I am trying to count the audits by auditor. I am using a form to get the date range and the auditor. The auditors are in a table named tblUser. The main table is tblParatransitPullOutAudit. There is a field in this table named AuditId which tells me what Audit each record belongs to. There may be many records with the same Audit ID, these count as 1 audit. I want to count how many audits by date range and Auditor. Any help will be greatly appreciated.
SELECT
t.Contractor,
Count(t.PK_ParapullOut) AS Audits
FROM
tblUser
INNER JOIN
tblParatransitPullOutAudit AS t
ON tblUser.PK_User = t.Auditor
WHERE ((t.AuditDate) Between forms!frmTotalAuditsDateRangeAuditor!txbStartDate.value And forms!frmTotalAuditsDateRangeAuditor!txbEndDate.value)
And ((tblUser.PK_User)=Forms!frmTotalAuditsDateRangeAuditor!cboAuditor.value)
GROUP BY Contractor;
SELECT
p.Contractor,
Count(p.AuditID) AS Audits
FROM
(
SELECT DISTINCT
p.Auditor,
p.AuditDate,
p.contractor,
p.auditid
FROM tblParatransitPullOutAudit AS p
) AS Total
WHERE ((p.AuditDate) Between forms!frmTotalAuditsDateRange!txbStartDate.value And forms!frmTotalAuditsDateRange!txbEndDate.value)
GROUP BY p.Contractor;

SQL query (in ZOHO reports) to join two tables by subtotas :-O

if you are out there, please help me:
I have 2 different tables, with different columns and no primary key:
1 - daily actual sales data
2 - monthly budget sales data
I need:
- one consolidated table with monthly actual sales (counted) and montly budget sales, to compare monthly sales vs budget.
Is it possible?
Of course.
You can aggregate the numbers for every month from the actual sales data table using sum() and group by and can join into this the budget table via the month column.