Count number of entries per user_id in BigQuery - sql

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.
SELECT *
FROM table
WHERE user_id = xxxx
I'm very new to using SQL and BigQuery. Thank you.

Related

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

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"
data

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 values into column when the two corresponding column values are not there

I am creating a table with having user profiles associated with various events. Various variables of user profile change at various intervals ranging from daily to more than a year. Hence, once the user is associated with an event, his profile cannot change for that event.
While Inserting the values into the table through a Select query I need to ensure that there is no more than one record for one user ID and Event ID. How can I achieve this in the best optimal way? Thank You in advance.
e.g.
event_id
user_id
age
gender
abc
1
24
M
xyz
5
31
F
xyz
5
31
F
abc
5
31
F
In the above table, the event_id = xyz and user_id = 5 has been entered twice. I need to ensure that the these type of value do not get inserted again from the query that I am retreiving the data.
You would use a unique constraint or index:
alter table t add constraint unq_t_event_id_user_id
unique (event_id, user_id);

How to Find a Higher Duplicate ID in SQL Server?

I need a help with a specific scenario I ran into.
I have a table structure like this.
In this table(lets call it a user table) ID is the primary key. I want to find the higher value of the ID if the data is duplicated in the username column.
For an example, John is listed 4 times and the highest ID of that data is 10. So i need that in my result.
I can find the duplicate but max id I am having some issue.
Using MAX Function:
Lists both duplicated rows and non duplicated also
Select Max(ID), username from table group by username
If u need only duplicated only then
Select Max(ID), username from table group by username
Having min(ID)<>Max(id)

DB2 - select last inserted 5 rows from a table

I have a table that has no indexed rows, nor a specific column...
Let's say "City, PersonName, PersonAge". I need to obtain the last 5 people inserted in that table...
How can I do it in in DB2?
I tried
select * from PEOPLE fetch first 5 rows only
this work perfectly... but no idea how to do it with the LAST rows....
You can't select the last 5 rows inserted, the database doesn't keep track of this. You need some sort of autoincremented ID or timestamp and order by that column descending.