entries in multiple dates in access - ms-access

I have a access table that has quarterly pricing data starting from 20100131 and goes on as 20100430, 20100731.... 20170131, 20170430. For each pricing date, there are many loans. Some loans stay in the portfolio, some loans are removed and some added for each pricing period. I would like to find the list of loans that exist in all periods and see their price for each period. So i have the "Loan_Number" field and "Price_Date" field. I would like to find the Loan Numbers that exist in all price date points. I appreciate the help.
Thanks!

Would have been nice to see some effort from you but I was intrigued with the challenge so here is what I accomplished.
1 - Need a dataset of all possible combinations of loan numbers and date values. So if you have a LoanNumbers table and a Periods table, create a Cartesian query called AllPairs:
SELECT LoanNumbers.Loan_Number, Periods.Price_Date FROM LoanNumbers, Periods;
If you don't have those tables, generate datasets with queries, assuming the data table has at least one record for every loan number and at least one record for every period:
SELECT DISTINCT Table1.Price_Date FROM Table1;
SELECT DISTINCT Table1.Loan_Number FROM Table1;
2 - Join AllPairs to data table for a 'find unmatched' query called LoanNoPeriod:
SELECT AllPairs.Loan_Number, AllPairs.Price_Date, Table1.Loan_Number, Table1.Price_Date
FROM AllPairs LEFT JOIN Table1 ON (AllPairs.Price_Date = Table1.Price_Date) AND (AllPairs.Loan_Number = Table1.Loan_Number)
WHERE (((Table1.Price_Date) Is Null));
3 - Final query:
SELECT * FROM Table1 WHERE Loan_Number NOT IN (SELECT AllPairs.Loan_Number FROM LoanNoPeriod);
Be aware these type of queries can perform very slowly and with very large datasets might not be practical.

Related

Mysql > get cars and cars-stocks data for specific date but if cars-stocks is not exist on specific date then show only cars data with null stocks

I have created a system where I have two tables namely cars and car stocks.
Cars table stores the list of cars that the dealer sells and Car stocks table stores the no. of cars for each car that has at least one piece in stock.
Please suggest a SQL query which gives two rows namely 'Car name' and 'No. of cars in stock'. car_name and car_stock_qty
For a car that has zero stock , Null should be written on the second column .
I have tried below query, which works fine for smaller data, but takes too much time for larger data.
Query:
SELECT cars.id,
cars.name,
car_stocks.created_at
FROM `cars`
LEFT JOIN car_stocks ON car_stocks.car_id = cars.id and date(car_stocks.created_at) = '2022-01-18';
Example expected result:
Please suggest if you have a more optimized/less time consuming query
You can try creating an index for the car_stock ID and created_at, I think that's as fast as you can get.

MS access inventory database advice, querying

I'm building a small inventory database in MS Access (2007), and I have one big dilemma: Should I store purchased (acquired) products/quantities in the same table as sold, with a field for transaction type, or should I separate them in two tables?
I'm working on second option now, but I'm stuck on querying these two tables.
tblProducts: ProductID (PK), ProductCode, ProductName
tblVendorsCustomers: VndCstID(PK), VndCstName, etc..(Vendors can also be Customers and vice-versa)
tblPurchase: PurchaseID(PK), PurchaseNumber(specific), VndCstID(FK), DatePurchased, DueDate
tblPurchaseDetails:PDetailsID(PK), PurchaseID(FK), ProductID(FK), QuantityPurchased, PricePurchased
tblSale: SaleID(PK), SaleNumber(specific), VndCstID(FK), DateSold, PayDate
tblSaleDetails: SDetailsID(PK), SaleID(FK), ProductID(FK), QuantitySold, PriceSold
Two tables (Purchase, Sale) are updating fine. Now, for example, when I want to show a report for a chosen Product, I need to pull data from these two tables, to see purchased quantity (along with Vendor name, date of purchase, and price) and sold quantity (with same set of data) and to calculate available quantity (at a given date). Maybe I'm missing something, but the only way to do this is create two select queries (for each of these tables), than union of these (to pull all transaction data), then a select query of that union, add an identifier field (for row from purchases and row from sales) and criteria for product selection..and I'm stuck on calculating available quantity..I'm guessing sum IIf, but not sure how to use it..
Sorry for such a long post...
Is this the right approach?
Any help or advice would be appreciated.

How can COUNT be used multiple times when creating a view table?

So, I am currently trying to create a View Table with the following info: EventCode, event name, the number of shows, the screen cost, the total screening cost (number of shows * screening cost), promotion cost, production cost, total cost (total screen cost + promotion cost + production cost), the expected
revenue (use the BaseTicketPrice), the actual revenue, the total discounts given (expected revenue - actual revenue) and the profit (all costs - actual revenue).
So there is a lot of information math involved in this View.
Also, the information comes from multiple tables, about 4.
I have to find the number of shows which would come from the table EventShow and then I have to use the number of shows in a later equation. How do I structure the count of a table and then use it again in an equation/aggregation?
I realize what I have below is really incorrect, but I'm mainly struggling on the COUNT portion right now.
CREATE OR REPLACE VIEW TicketDetails AS
SELECT
E.EventCode,
E.EventName,
E.EventShows Count(*) as NumberOfShows,
(E.ScreeningCost * NumberOfShows) as TotalScreeningCost,
(E.BaseTicketPrice * Ticket(Count(*))) as ExpectedRevenue,
E.PromotionCost,
E.ProductionCost,
(TotalScreeningCost + E.PromotionCost + E.ProductionCost) as TotalCost,
(E.BaseTicketPrice * TS.TicketsSolds) as ActualRevenue,
(ExpectedRevenue - ActualRevenue) as TotalDiscounts
(TotalCost - ActualRevenue) as Profit
FROM Event E, Ticket T, TicketSale TS, EventShow ES
WHERE (E.EventCode = TS.EventCode);
The select list is one the last clauses that get executed in sql. Any aliases defined in the select list can only be referred to in the order by and having clauses, but not in the select list or where clause.
Another issue will be that if you join multiple tables, you cannot count the rows returned from one of the tables using count(*) or count(tablename.*), so you have to use a field from that table which cannot be null if a row is returned from that table - this should be a field you joined the table on. A simplified example based on your tables:
select e.eventcode, e.eventname, count(es.eventcode) as numberofshows, e.screeningcosts*count(es.eventcode) as totalscreeningcosts
from events e left join eventshows es
on e.eventcode=es.eventcode
group by e.eventcode, e.eventname, e.screeningcosts
I assumed that the 2 tables each have an eventcode field.

Pulling different records from multiple tables as one transaction history list

I am working on an employee management/reward system and need to be able to show a single "transaction history" page that shows in chronological order the different events that the employee has experienced in one list. (Sort of like how in facebook you can goto your history/action section and see a chronological list of all the stuff that you have done and affects you, even though they are unrelated to eachother and just have you as a common user)
I have different tables for the different events, each table has an employee_id key and an "occured" timestamp, some table examples:
bonuses
customers
raise
complaints
feedback
So whenever an event occurs (ie a new customer is assigned to the employee, or the employee gets a complaint or raise) a new row is added to the appropriate table with the employee ID it affects and a timestamp of when it occured.
I need a single query to pull all records (upto 50 for example) that include the employee and return a history view of that employee. The field names are different in each table (ie the bonus includes an amount with a note, the customer includes customer info etc).
I need the output to be a summary view using column names such as:
event_type = (new customer, bonus, feedback etc)
date
title (a brief worded title of the type of event, specified in sql based on the table its referencing)
description (verbiage about the action, such as if its event_type bonus display the bonus amount here, if its a complain show the first 50 characters of the complaint message or the ID of the user that filed the complaint from the complaints table. All done in SQL using if statements and building the value of this field output based on which table it comes from. Such as if its from the customers table IF current_table=customers description='A customer was assigned to you by'.customers.assigner_id).
Ideally,
Is there any way to do this?
Another option I have considered, is I could do 5-6 different queries pulling the records each from their own table, then use a mysql command to "mesh/interleave" the results from all the queries into one list by chronological order. That would be acceptable too
You could use a UNION query to merge all the information together and use the ORDER BY clause to order the actions chronologically. Each query must have the same number of fields. Your ORDER BY clause should be last.
The examples below assume you have a field called customer_name in the customers table and bonus_amount in the bonuses table.
It would look something like this:
SELECT 'New Customer' as event_type, date,
'New customer was assigned' as title,
CONCAT('New Customer: ', customer_name, ' was assigned') as description
FROM customers
WHERE employee_id = 1
UNION
SELECT 'Bonus' as event_type, date,
'Received a bonue' as title,
CONCAT('Received a bonus of $', FORMAT(bonus_amount, 2), '.') as description
FROM bonuses
WHERE employee_id = 1
UNION
...
ORDER BY date DESC;

Sql Query Optimization - Remove Not In Operator

I am storing employee attendance on a table 'attendance' having the following structure:
EmpID, CDate
Attendance system insert this table everyday with employee-id of all employees present on that particular day.
I need to find out absent statement of a particular employee. I can do this easily by selecting all distinct date that are not in - dates where the employee is present.
Is there any way I can remove the not in operator on that sql statement. Please help
Here is the sql query for employee with EmpId 01:
select distinct CDate
from attendance
where CDate not in (
Select CDate from attendance where EmpID='01')
The problem isn't in the NOT IN clause, it is the subquery.
You want something more like:
SELECT DISTINCT a1.CDate, if (EmpID=NULL, false,true) as Present
FROM attendance as a1
LEFT JOIN attendance as a2 USING (CDate)
WHERE a2.EmpID='01'
This is a cartesian join which pulls all of the dates, then joins the employee attendance status on that. Should be significantly faster than your subquery.
Updated, with tested code:
SELECT DISTINCT a1.CDate, IF (a2.EmpID IS NULL, false,true) as Present
FROM attendance AS a1
LEFT JOIN attendance AS a2 ON (a1.CDate = a2.CDate AND a2.EmpID='01')
My bad on the previous answer. I should have put the subselection into the ON instead of an aggregate.
You could change your mechanism to store data for each employee, each day.
Yes, it'll add a lot of rows, but how can you be sure that you'll get all dates from logged data? what if theres nobody at work today? no one will have absense?
If you'd go with:
EmpID, CDate, Present
1, {date}, 0|1
then you'd have simpler and faster query traded for table size:
select CDate from attendance where EmpID = 1 and status = 0;