MS Access 2010: Extract unique values - ms-access

first off: I know virtually nothing about MS Access but now I'm in a situation where I have to use it (dataset is too big for Excel). The data has column names like Customer_Name, Product Name, Amount, Date
Date refers to the last day of a month, so for example for February it's 28/02/2013. Now I want to compare the amount a customer bought in February to the amount he/she bought in January and calculate the difference. So far, I've been able to this by prompting the user to enter the date.
SELECT Data.Customer_Name,
Sum(IIf(Format(Date,"yymm")=[Startdate (yymm)?],Amount,0)) AS Amount_Startdate,
Sum(IIf(Format(Date,"yymm")=[Enddate (yymm)?],Amount,0)) AS Amount_Enddate,
Amount_Enddate-Amount_Startdate AS Difference
FROM Data
GROUP BY Data.Customer_Name;
This works but is it possible for Access to recognize which dates are in the column "Date" (there are only two distinct dates) so the user does not have to enter anything? Also, I tried to replace "Amount_Startdate" with a field that has the respective date in its name (e.g. "Amount_Feb2013") and played around with ampersand but it didn't work.

If you create a new table called tblValues with just 2 fields; ID and TDate (always try to avoid using reserved words like "Date", "System" or other words that Access already assigns a function to), you can fill it like this:
ID TDate
-- ---------
ST 1/31/2014
EN 2/28/2014
Then you could use the DLookup function to make this code generic:
SELECT Data.Customer_Name,
Sum(IIf(Format(Date,"yymm")=DLookup(Format(TDate, "yymm"), tblValues, "ID = 'ST'"),Amount,0)) AS Amount_Startdate,
Sum(IIf(Format(Date,"yymm")=DLookup(Format(TDate, "yymm"), tblValues, "ID = 'EN'"),Amount,0)) AS Amount_Enddate,
Amount_Enddate-Amount_Startdate AS Difference
FROM Data
GROUP BY Data.Customer_Name;
Then you could just update the table with the values you want to use as start and end dates whenever you want.

Related

Access Select query where source field is based on another column

I am using MS Access for FX rate data. I have a table which lists currencies and their associated FX rate. Each column has the rate for a different month. e.g. "January 2019", "February 2019" etc
I have another table which has a list of dates and currencies, and I want to create a select query which returns the FX rate for the relevant currency for the relevant month.
I have created a field which converts the date to the format which matches the field name in the source data table, so for example the date 12/01/2019 is shown as January 2019 (and defined this as FX_period)
Basically, I want the source field used to be based on another field in that query.
For example:
January dates: source field is TBL10_FX_BS_Rates.[January 2019] AS FX_Rate
February dates: source field is TBL10_FX_BS_Rates.[February 2019] AS FX_Rate
So in the end I just have 3 columns: Date, Currency and FX rate
I have tried lots of things, including:
SET "[tablename]!["&FX_period&]" as FX_RATE
Hope this makes sense!
A table with a field for each mon/yr is not a normalized structure. Normalize TBL10_FX_BS_Rates structure then join tables in query. Actually, normalized table might be enough - depends if there are other fields in currencies table. Otherwise, use DLookup() - something like:
SELECT CurrencyDate, Currency,
DLookUp(Format([CurrencyDate],"mmmmyyyy"),"TBL10_FX_BS_Rates","Currency='" & [Currency] & "'") AS Rate
FROM Currencies;
Advise not to use spaces nor punctuation/special characters (underscore only exception) in naming convention.

is there a way to update multiple colomn with spesific part?

I have a table and inside there's a birth date, one of the worker serial number is almost the same as the birth date and since some of the serial number data is not the same as their birth date I wanted to update those date as the same as their serial number.
I use this kind of code
UPDATE worker
SET birthdate = MID(NIP,1,8)
where (MID(NIP,1,8)
<>concat(MID(TGLLHR,1,4),MID(TGLLHR,6,2),MID(TGLLHR,9,2)));
the 3rd line is for searching birthdate that not the same as serial number
the 2nd line is the one I wanted to change instead of getting output like 1996-08-01 the one that I got just like 19960801... I wanted to change it like date format
For example if one of them have serial number 19961101013 and their birth date is 1995-11-03 I wanted to change their birth date as same as the serial number like 1996-11-01
You can use couple or REPLACE/CONVERT method to check data matching between birthdate and NIP and then UPDATE data accordingly. The following script will help you to update your data accordingly.
Note: UPDATE is a risky command. Please try with your test data first.
UPDATE worker
SET birthdate = CONVERT(LEFT(CONVERT(NIP,CHAR),8) , DATE)
WHERE REPLACE(CONVERT(birthdate, CHAR),'-','') <> LEFT(CONVERT(NIP,CHAR),8)
The above script will only UPDATE records that has mismatch between birthdate and NIP value as you explained.

Mysql Function multiple entries in one cell

So I'm working on a schedule system for my job a basically i wanted to know if there is a way where mysql can do something like:
|Monday |tuesday|wendsday|total
|Dan |5am-7am |7am-6pm|6am-11am|
11am-2pm| |2pm-7pm |
5pm-12am|
where i can enter multiple shifts on 1 day for each person in the same cell if needed instead of the name repeating several times like:
Dan|5-4|
Dan|6-8|
and if there is a function to calculate total time in one cell with multiple shifts
There is a way (representing the data as string), but you wouldn't want to do this - you will loose all calculations, searches etc.
You should not try to represent the data in the database exactly as how it looks on paper.
I would make a table like this:
ShiftID|Person|StartTime|EndTime
Making StartTime & EndTime columns of type DATETIME, you will store not only the HH:mm of a shift's start, but also the day. This is helpful when you have a shift which starts on one day and ends in the next, like starting on Monday 2017-05-15 23:00 and ending on Tuesday 2017-05-16 02:00.
You can extract the date only from this filed using MySQL DATE() function and select only those entires which start OR end on this day.
To calculate the shift's duration you can use MySQL function TIMESTAMPDIFF()
You can even use DAYOFWEEK() to get if it is Monday, Tuesday, etc.
About duplicating the person's name - I would make another table, which will match users with their data to IDs an use ID in the column Person, but for a starter and if your data is not big and if speed is not an issue and if typo errors (like Den instead of Dan) are not a problem ... you could use the name directly in this table.
After storing the data in a table like this you could represent it as you wish in HTML (or print).
You can create a third table with the following columns:
person_id int,
start_time datetime,
end_time datetime
Where person_id would be foreign key to Person table and start_time and end_time would be datetime columns. You can then store multiple records for a person in this table and use MySQL's date functions with GROUP BY to generate the report similar to the one in question.

How to make a report with count of type of cases in each month in Acess 2010

I have a database is access with each record having a date and yes/no type columns for each record which shows which category the record comes under. I want to create a report which shows the types of cases in each month by taking a date range as a parameter through prompts. I have done the prompt part but I'm not sure how the query should be to show values for each month in that date range. Can someone please help me with this?
Without knowing the details of your tables, you can Group by the Year and month either as separated columns or with a combined expression like this:
Year(Orders.OrderDate) & '-' & Right('0' & Month(Orders.OrderDate), 2)
If you need more specific help, provide an example of what you have and what you want to accomplish.
Edit:
With the additional info, if i understand correctly you have something like this:
Case Date Threat Stalking Bullying ...
1234 24/12/12 Yes No No
...
And you want something like this:
Date Threats Stalking Bullying
12/12 3 2 10
Then, you can do more/less it like this:
Select Year(Cases.Date) & '/' & Right('0' & Month(Cases.Date), 2) As Date, ThreatQuery.Cases As Threats, StalkingQuery.Cases, ...
From Cases, (Select Count(*) From Cases Where Cases.Threats == 'Yes' And Cases.Date Between #Param And #Param2) ThreatQuery, (Select Count(*)...) StalkingQuery, ...
Where Cases.Date Between #Param And #Param2
Note that this query uses multiple subqueries (one for each type of case), so you may want to be sure that the subqueries are right first, for this, you can just check one, given that all are the same, only change depending of the case's type.

Sum a Subtotal in MS Access by Date

I'm using MS Access 2010
My query has the fields, Date, Market Price, Actual Price
I need to separate out within the query by date Actual Price/Market Price in order to achieve a percentage which I can then average (for that day only, one day at a time)
Each day will have anywhere from 10-50 items.
You'll want to use a standard query, but a totals to the query. On the Home tab, in the Records group, click Totals.
Once you have that field displayed you can put a Sum (in the Totals field) for Market Price and Actual Price; and then make sure the Date field is set to GroupBy.
Then you should be getting totals for the Price fields grouped by Date.
btw., I'd label that date field something else other than just Date (e.g., DateSold, PurchaseDate, etc.) just to avoid confusion and possible conflicts with the Date type.