MS Access: Updating column in existing table based on two variables - sql

I would like to update an existing table (table 1) based on two variables. The existing table is a health care dataset that shows patient admit, consult, and discharge dates. I have a separate dataset (table 2) that has more consult dates to add. So, I would like to upload/update the consult date from table 2 into table 1 by matching the patient ID number and admission date.
I have already created a query that merges table 1 and 2 successfully, but I am unable to edit any of the variables manually when I have merged them.
SELECT Inpatient.PatientID, Inpatient.AdmissionDate, Inpatient.Admit, FirstNote.ConsultDate, Inpatient.DischargeDate
FROM Inpatient INNER JOIN
FirstNote
ON (Inpatient.AdmissionDate = FirstNote.Admit) AND
(Inpatient.PatientID = FirstNote.PatientID);
I expect the final table to have the following variables:
Patient ID, AdmissionDate, ConsultDate, DischargeDate
with updated data under ConsultDate from table 2

Related

Bigquery join 2 tables with id concated from 4 columns and create a new table dynamically

I have two tables in Bigquery from two different data sources, lets say x and y. I want to join these two tables on os_name, tracker_name, date, country columns. For that i am using concat function and joining like this:
full outer join x on concat(x.date,x.os_name,x.tracker_name, x.country) = concat(y.date,y.os_name,y.tracker_name,y.country_code)
as a query result common columns also gets duplicated. like in the result there is os_name and os_name_1, country_code, country_code_1 etc. columns. I don't want that. Final columns should be as in the example below in Final Table Schema.
I want to return all records from both sides. For example if there is no match in table y
y_install, and y_purcase will be 0, and vice versa.
X TABLE SCHEMA:
os_name,
tracker_name,
date ,
country
install
purchase
Y TABLE SCHEMA:
os_name,
tracker_name,
date,
country,
y_install,
y_purchase
Final Table Schema required:
os_name,
tracker_name,
date ,
country
install
purchase,
y_install,
y_purchase
I am going to schedule the query and write results to destination table at given interval.
Can you help me out with this query.
Regarding the final table, I don't understand whether you want to return first NON NULL result or whether you want to have e.g. an array which will contain both results from both tables in case both tables a valid value. In my sample table, do you want row 1,2 (actually the same thing) or 3?
row_number
x_install
y_install
final_table_install
1
23
50
23
2
NULL
50
50
3
23
50
[23,50]
It comes out that What I wanted to use was union all. First, I added the non-common columns to the two tables so that the schemas of the two tables are equal. So I was able to vertically merge tables using union all. Thanks for trying to help out anyway.

Get the "most" optimal row in a JOIN

Problem
I have a situation in which I have two tables in which I would like the entries from table 2 (lets call it table_2) to be matched up with the entries in table 1 (table_1) such that there are no duplicates rows of table_2 used in the match up.
Discussion
Specifically, in this case there are datetime stamps in each table (field is utcdatetime). For each row in table_1, I want to find the row in table_2 in which has the closed utcdatetime to the table 1 utcdatetime such that the table2.utcdatetime is older than the table_1 utcdatetime and within 30 minutes of the table 1 utcdatetime. Here is the catch, I do not want any repeats. If a row in table 2 gets gobbled up in a match on an earlier row in table 1, then I do not want it considered for a match later.
This has currently been implemented in a Python routine, but it is slow to iterate over all of the rows in table 1 as it is large. I thought I was there with a single SQL statement, but I found that my current SQL results in duplicate table 2 rows in the output data.
I would recommend using a nested select to get whatever results you're looking for.
For instance:
select *
from person p
where p.name_first = 'SCCJS'
and not exists (select 'x' from person p2 where p2.person_id != p.person_id
and p.name_first = 'SCCJS' and p.name_last = 'SC')

MS Access: Update + Append Simultaneously?

I have two tables of interest - one is my "main" data table which needs to be updated periodically (ARIES_AC_PRODUCT) and the other is a side table that I pull in new data from (Update_AC_Product). My goal is for Access to update the records if they already exist in the main table, otherwise just append the new records (all with one query). Currently I have two queries for completing this goal... One is an update query and the other is an append query. Note that both tables have the exact same structure (5 fields which are PROPNUM, P_DATE, OIL, GAS, WATER). Below is the SQL code for each query:
Append query:
INSERT INTO ARIES_AC_PRODUCT (PROPNUM, P_DATE, OIL, GAS, WATER)
SELECT Update_AC_Product.PROPNUM, Update_AC_Product.P_DATE, Update_AC_Product.OIL, Update_AC_Product.GAS, Update_AC_Product.WATER
FROM Update_AC_Product;
Update query:
UPDATE ARIES_AC_PRODUCT
INNER JOIN Update_AC_Product ON (ARIES_AC_PRODUCT.PROPNUM = Update_AC_Product.PROPNUM) AND (ARIES_AC_PRODUCT.P_DATE = Update_AC_Product.P_DATE)
SET ARIES_AC_PRODUCT.PROPNUM = [Update_AC_Product]![PROPNUM], ARIES_AC_PRODUCT.P_DATE = [Update_AC_Product]![P_DATE], ARIES_AC_PRODUCT.OIL = [Update_AC_Product]!OIL, ARIES_AC_PRODUCT.GAS = [Update_AC_Product]!GAS, ARIES_AC_PRODUCT.WATER = [Update_AC_Product]![WATER];
Note that I have 2 primary keys in each table - PROPNUM and P_DATE. PROPNUM is the item ID and P_DATE is the production date. One PROPNUM can have multiple P_DATE entries (so the combination of PROPNUM & P_DATE makes a unique record in each table).
Is it possible to combine these two queries into one so I don't have to append new records and then update the existing ones separately?
Thank you!!

Copy part of some SQL Server rows into same database

I have a SQL Server database where I want to copy parts of the rows, and the other part of the row needs to be filled with new data, and into the same table. For example:
Table name: budget
ItemID Item Date
1 A 4/22/2012
2 B 4/15/2012
3 C 4/24/2012
When I copy the table, I want to copy the item column, but not the date column into the same table. The date column will get today's date, but Item will copy rows. Like below.
table name: budget
ItemID Item Date
1 A 4/22/2012
2 B 4/15/2012
3 C 4/24/2012
4 A 5/6/2012
5 B 5/6/2012
6 C 5/6/2012
I have tried:
SELECT Item
INTO Budget
FROM Budget
But I get error
There is already an object named 'Budget' in the database.
Also, I'm not sure how after I copy the rows to fill the rest of the column with new date data.
Thank you.
You need to try insert instead. Generate a query that has the new rows, and try something like:
Insert into <table>
Select *
From <q>
You are trying to create a new table called "Budget" which already exists by "SELECT INTO" statement.
Is your ItemID auto increment? Try this.
SELECT * into Budget FROM Budget_Temp
INSERT INTO Budget(Item,Date)
SELECT Item,'5/6/2012' FROM Budget_Temp
DROP TABLE Budget_Temp

How can I make multiple records to be printed as a single row

alt text http://img59.imageshack.us/img59/962/62737835.jpg
This three columns are taken from 3 tables. In other words, these records are
retrieved by joining 3 tables.
It is basically a very simple time sheet that keeps track of shift starts time, lunch time and so on.
I want these four records to show in one row, for example:
setDate --- ShiftStarted --- LunchStarted --- LunchEnded ---- ShiftEnded ----- TimeEntered
Note: discard TimeEntered column. I will deal with this later, once i know how to solve the above issue, it will be easy for me to handle the rest.
How can i do it?
Further Info - Here is my query:
SELECT TimeSheet.setDate, TimeSheetType.tsTypeTitle
FROM TimeSheet
INNER JOIN TimeSheetDetail ON TimeSheet.timeSheetID = TimeSheetDetail.timeSheetID
INNER JOIN TimeSheetType ON TimeSheetType.timeSheetTypeID = TimeSheetDetail.timeSheetTypeID
TimeSheet table consists of the following columns:
timeSheetID
employeeID - FK
setDate
setDate represents today's date.
TimeSheetType table consists of the following columns:
timeSheetTypeID
tsTypeTitle
tsTypeTitle represents shifts e.g. shift starts at, lunch starts at, shift ends at, etc.
TimeSheetDetail table consists of the following columns:
timeSheetDetailID
timeSheetID - FK
timeSheetTypeID - FK
timeEntered
addedOn
timeEnetered represents the time that employee set manually.
addedOn represents the system time, the time that a record was inserted.
I must admit I haven't fully read all but I think you can work out the rest for yourself. Basically you can join the table timesheet with itself.
I did this ...
create table timesheet (timesheet number, setdate timestamp, timesheettype varchar2(200), timeentered timestamp);
insert into timesheet values (1,to_date('2010-08-02','YYYY-MM-DD'),'Shift Started',current_timestamp);
insert into timesheet values (1,to_date('2010-08-02','YYYY-MM-DD'),'Lunch Started',current_timestamp);
insert into timesheet values (1,to_date('2010-08-02','YYYY-MM-DD'),'Lunch Ended',current_timestamp);
insert into timesheet values (1,to_date('2010-08-02','YYYY-MM-DD'),'Shift Ended',current_timestamp);
commit;
select * from timesheet t1
left join timesheet t2 on (t1.timesheet = t2.timesheet)
where t1.timesheettype = 'Shift Started'
and t2.timesheettype = 'Lunch Started'
... and got out this
TIMESHEET SETDATE TIMESHEETTYPE TIMEENTERED TIMESHEET_1 SETDATE_1 TIMESHEETTYPE_1 TIMEENTERED_1
1 02.08.2010 00:00:00.000000 Shift Started 05.08.2010 12:35:56.264075 1 02.08.2010 00:00:00.000000 Lunch Started 05.08.2010 12:35:56.287357
It was not SQL Server but in principle it should work for you too.
Let me know if you still have a question
You might want to check out the PIVOT operator. It basically allows you to use particular row values to create new columns in your result set.
You'll have to supply an aggregate function for combining multiple rows - for instance (assuming you split your data on a per day basis), you'll have to decide how to deal with multiple "shift started" events on the same day. Assuming that such events never occur, you'll still have to use an aggregate. MAX() is usually a safe choice in those circumstances.