How to join two tables by dates? - ms-access

I have two tables imported into Access from Excel workbooks.
They are narrow tables:
INRMaster: MastDate Date/Time (Short Date)
CIInput: CIDate Date/Time (Short Date)
INRTestResult Number
Dose Number
OutOfRange Short Text
CIInput table was downloaded and the date was date/time with date and time of test. I reformatted that date field to mm:dd:yyyy to match the table I created, INRMaster.
There is no primary key on either table. I tried the join with primary keys of date in both tables and it returned nothing as well. Creating the query using the QBE Grid.
The generated SQL is as follows:
SELECT INRMaster.MastDate, CIINput.[INR test result], CIINput.Dose, CIINput.OutofRange
FROM INRMaster INNER JOIN CIINput ON INRMaster.MastDate = CIINput.CIDate
Office 365 Access, Windows 10.

Setting Format property does not change data. Unless you actually modify saved values, time part is still there and since it is unlikely values will agree to the second, a join will fail. Don't apply formatting in table - view full saved value.
Extract date part with expression in query. If both fields were saved with date and time components, then extract date portion from both. Consider:
SELECT INRMaster.MastDate, CIINput.[INR test result], CIINput.Dose, CIINput.OutofRange
FROM INRMaster INNER JOIN CIINput ON Int(INRMaster.MastDate) = Int(CIINput.CIDate);
DO NOT OPEN QUERY IN DESIGN VIEW. Query Designer cannot resolve this join. Would have to join nested subqueries to enable Design View.

Related

SQL Inner Join on same table

I currently have a SQL table of timestamps, station_code, temperature, humidity, pressure.
The timestamp and station_code are unique keys.
The data is gathered at regular 5 minute intervals.
I am trying to restructure the data into 3 tables, temperature, humidity, and pressure that would have unique timestamps for rows, and station_code for columns.
I am currently at a loss on how the join statement would go.
Any help would be appreciated.
With data from Excel,
Can easily produce a pivot table.
The end goal here is to pull out the specific sensor data for analysis.
As long as the timestamps match exactly, you would just join on timestamp and station_code...
... but since MySQL does not have a true FULL outer join, that would mean you would either have to guarantee all 3 tables always have a match, or specific tables always had data in a certain order (so that a LEFT join could be used). Both of which suggest you probably should not decompose the original table.
It's unlikely to save you much space either as the redundant timestamp and station_code values are likely to take as much space as you would save by avoiding storing NULL measurements.

how to query the same columns from all tables in the schema with given criteria in mysql?

I have a database "warehouse" including tables of daily inventory records, one table for each day.
Now, I need to check the historic change of the inventory level. The output will print the inventory of each day given certain criteria.
I am not sure how to describe it, so I created a simplified sample of the schema, its tables and the expected output.
The schema "warehouse" has a list of tables:
Each table contains the same columns for product ID and inventory, below is table 101
For each table, I need to do a query:
select count(*) as num_of_product_with_inventory from [table name]. After I have the query result from each table, I should have an output like in below:
Can anyone show me how the query should look like to get the final output? I only know the basic queries and have no clue how to put these together. Thank you!
The data model you have is making your work harder than it should be.
If you must keep it, you will need to use a stored procedure or do the loop in your code (not in sql).
But you should really do is change the data model.
It is not recommended at all to create a table per day!
It's a mix of DATA with METADATA. The table structure should represent different types of data that you store, while the fact that you had different inventory on date X vs date Y should be in your data.
So, recommend to create one table with columns date, product_id and warehouse_inventory. If it gets too big, you can partition it by date (week/month/..). Then you can easily get your data with something like:
SELECT date, count(*) AS num_of_products_with_inventory
FROM daily_inventory i
WHERE i.date BETWEEN '<some date>' and '<some date>'
GROUP BY date

BOBJ 4.1 Use values from two not joined tables in one pre-defined condition

I use a number of pre-defined conditions to compare the Date field from Trade table against GETDATE() function to work out the trades for this month, this year, previous year etc.
Now I need to create an additional table, where I will have a set of dates representing the start and the end dates for reporting periods, e.g. the start and end dates of current year reporting, the start and the end dates for previous year reporting etc.
This additional table and Trade table are not joined. In fact the additional table is not linked to anything.
I need to create a new set of pre-defined conditions where Date from Trade table will be compared from the values from the new dates table, i.e. I would like the new conditions to look like the following
Trade.Date < ReportingDates.CurrentYearEndDate
AND
Trade.Date > ReportingDates.CurrentYearStartDate
The condition validates fine, but unfortunately I get the "The query cannot run because it contains incompatible objects. (IES 00008)" error" when I try to execute the condition.
You could use subselects in the predefined condition like:
trade.date between (select currentyearstartdate from reportingdates)
and (select currentyearenddate from reportingdates)
Since reportingdates is not actually joined to anything, it does not need to be in the universe structure. This does have one disadvantage -- it's not obvious from the list of tables that it is actually needed in the universe.
Alternatively, you can create a derived table that joins trade and reportingdates as a cartesian product. The derived table would include all columns from both tables, so your condition would simply be (assuming the derived table is named DT):
dt.date between dt.currentyearstartdate and dt.currentyearenddate
The downside to this approach is that reportingdates is included in all queries, whether it's needed or not, and the SQL can be a little harder to read.

Comparing DOB's from different tables

SO I have to list the names of anyone in the tenant family who is older than the tenant itself. There are two tables. Tenant and tenant_family. I have tried comparing the two dates to see which DOB in the tenant_family table is less than (meaning they would be older) the DOB in the tenant table.
This is what I have so far but it appears to be wrong. Can someone direct me towards a way of getting the right output?
SELECT DISTINCT tenant_family.name
FROM TENANT_FAMILY, tenant
WHERE tenant_family.dob < tenant.TENANT_DOB;
Use the DATE function to extract the date part of your query for comparison like:
select distinct tenant_family.name from TENANT_FAMILY, tenant where DATE(tenant_family.dob) < DATE(tenant.TENANT_DOB);
In case your dob and TENANT_DOB are not stored as the DATE data type in your database or they are stored as DATETIME then you might extract only the date part using the function:
select distinct tenant_family.name from TENANT_FAMILY, tenant where date_format(tenant_family.dob,"%y-%m-%d") < date_format(tenant.TENANT_DOB,"%y-%m-%d);
This should give you the correct results after comparison.

Difficult MS Access Query - how to combine them?

I am trying to build an access report based on data from multiple different tables within the database.
I have 3 columns which perform calculations, and I am wondering how to put this query together. All 3 columns deal with dates, but calculate them differently.
The first column retrieves the most recent date of action for a userid if the type of action is "B":
select pid, Max(date) as most_recent
from actions
where ref = 'B'
group by pid;
The second column performs a calculation based on 2 fields, one is a date and one is a number in months. I am unsure how to add these two fields so that the number is added to the date as a number of months.
what i have so far is:
select nummonths,Max(lastvisit) from users
the third column I need to select the first date thats in the future for each user (next appointment date), there will be dates before and after this date so its a little difficult:
select uid,date from visits
The code for the last 2 queries needs to be slightly modified, and I was wondering what the best approach would be to join these all together? A type of join?
If you need to build a report with data from the 3 queries, you will need related data to join them. In that case, please send the structure of the tables.
If you need to show 3 lists in one report, you can use subreports: create a new empty report. In design mode, you can add 3 subreports from the toolbox bar. To each of the subreport assign the record source property to the corresponding sql.
regards
I am unsure how to add these two fields so that the number is added to the date as a number of months.
Use the DateAdd() function:
SELECT DateAdd("m", 2, LastVisit) FROM ...
Results in a date two months from the LastVisit date.