SQL query to join 2 tables and get backdated data average based on different timestamps - sql

I have Table 1 which has a timestamp column with random intervals. I have to join this table 1 with table 2 and I should get the 2nd table Cat 1 quantity as backdated 24hrs avg and 18hrs avg.
I'm not sure how I can join these 2 tables and also go back of time based on table 1 time column and get the 24hrs backdated data of Table 2 column and get the avg of Cat 1 column.
Your help is much appreciated. I have been working on this for a while and couldnt figure out a way
Sample inputs and outputs are attached as a image "Data"


Count number of entries per user_id in BigQuery

I have a table consisting of 33 distinct user_id that has a row of data for each day. There should be 31 days (rows) for each user. I want to run a query to confirm this, where the output is a table of unique user_id in column 1 and number of entries in column 2.
I know I can display all rows for each user_id using the following query. But I don't want to do that 33 times just to find out how many entries per user there are in the table.
FROM table
WHERE user_id = xxxx
I'm very new to using SQL and BigQuery. Thank you.

SQL for append rows based on max date

This is more of a logic question as I am having a hard time wrapping my head around it.
Say I have table 1 that is truncated and populated everyday, and a time stamp column is added onto it. Everyday new records would be added to the table.
That table 1 is copied to table 2 initially, however on consequent runs I only want to add the new records from table 1 into table 2.
I know this will be a mixture of matching the columns and only importing the MAX DATES, however confused as to the actual logic of the query.
So in short I want to append only the latest rows from table 1 to table 2 based on the max date.
If you want to sync the tables daily, you may just look for timestamp_column > current_Date.
If you want to get the max dates, you can write something like this:
INSERT INTO table2 (x,y,z, timestamp_column)
SELECT x,y,z, current_timestamp() FROM table1
WHERE timestamp_column >
(SELECT IFNULL(MAX(timestamp_column), '0001-01-01' ) FROM table2);
On the other hand, I think Snowflake streams are a very good fit for this task:
You can create an "Append-only" stream on table1, and use it as a source when synchronizing to table2.

Vba sql table join on matching field where 2nd field not matching

Good day everyone. I'm sure this is a fairly common issue but I trawled the internet and I cannot find an answer that works in my case. I have 2 tables which both have 2 fields and both field names are the same. I need to select all records in table 1 where the 1st field matches table 2 1st field and the 2nd field in table 2 does not match field 2 in table 1. Sorry if I'm not explaining this very well. Table 1 has around 10K records with client-name and country. table 2 is a unique list of client names (around 1K) with client-name and country fields. so I need to select all the records from table 1 with same client-name but where country in table 2 is not the same as in table 1. I've been on this for several hours now so any help would be most welcome.
The data is in 1 Access Database and I'm using ADO connection. What I have is:-
SELECT [client-name], [country] FROM [table1] LEFT JOIN [table2] ON [table1].[client-name]
= [table2].[client-name] WHERE NOT [table1].[country] = [table2].[country];

Insert table1 row values into table2 columns in SQL

I'm about to write a query and was thinking if anybody can help me out.Please have a look at attached image and at the same time let me tell you brief overview of my requirement.
Table1 Screenshot
Table2 Screenshot
In Table1, we can have maximum 4 and minimum 2 records per user per day. If a user have 4 records then FirstRecord of the day In Table1 will go to Time1 column in Table2, second will go to Time2 and so on. If a user have 2 records per day than First record of the day will go to Time2 column in Table2 and second record will go to Time3 column. For references, Tables screen shots are attached.
Any useful link or query would be appreciated
Use ROW_NUMBER() and partition by UserID and order by TransactionDate to generate numbers 1-4 or 1-2 for each user.
Then insert into Table 2 with a CASE expression for each of the four time columns.
The CASE will check a subquery to get the MAX(RowNumber) for that User, and then SELECT the appropriate row from Table 1 based on the logic you gave for the two cases.

Explanation of SQL code

Can anyone explain to me what the following code does? Thanks.
1) this code joins Subscribers table records with Relatiws table records where Subscribers table phone number is equals to Relatives table column 'subscriber' value.
2)Then this code joins resulting table records from first step with Activities table records where Relatives table column 'FamilyMember' value is equal to Activities table column 'Subscriber' value.
Resulting table from step two has 10 columns
1 PhoneNumber
2 Name
3 SubscriptionDate
4 Subscriber
5 FamilyMember
6 ID
7 Subscriber
8 Date
9 Type
10 Content
3)then this code selects column 'Contents' values from resulting table from second step where name is 'Ringo Star' and type is 'Message received'.