Getting all entries from N unique users - sql

I am working with Google BigQuery and I have two tables as described by the following link:
[Tables].
user_metric contains entries with the lifetime information of all users.
user_daily_metric contains entries for each user and each of the days they have been active
My challenge is that I wish to take the first 500 unique users (represented by the candidate key user_metric.userid) and I want to create a table with entries for each of these 500 unique users and all of their days active. Resulting in a table similar to this: [Resulting table]
(Consider the user with userid = 0690894780 as not being a part of the first 500 unique users)
My current query works for creating the table I desire, in terms of columns, but I have not been able to figure out how to limit it to only entries from the 500 unique users.
Current query:
SELECT
user_metrics.userid, user_metrics.userProgression, user_daily_metrics.missionSecondsPlayed_sum, user_daily_metrics.missionMovesUsed_sum
FROM
user_metric
JOIN user_daily_metric
ON user_metric.userid = user_daily_metric.userid
ORDER BY
user_metrics.userid
In advance, thank you very much for taking the time to read my question (and if I'm lucky, even reply to it) :)

Use a subquery:
SELECT um.userid, um.userProgression, user_daily_metrics.missionSecondsPlayed_sum,
udm.missionMovesUsed_sum
FROM (SELECT um.*
FROM user_metric um
ORDER BY um.userid
LIMIT 500
) um JOIN
user_daily_metric udm
ON um.userid = udm.userid
ORDER BY um.userid

Related

Get all data of users after grouping by

I have two tables "Users" "Bookings" and I merged two tables and groping by the booking and users count and get a new table which has the count of users who made a specific count of bookings ex:
so in the first column (62 users made 3 booking and the second columns 52 users made 4 bookings)
I want to get the data of users when I click on any line on the graph, means when I click on the first line on the graph I want to show the 62 users in a table, can I do this or not?
If you want a SQL solution, it would use group by and having:
select userid
from bookings
group by userid
having count(*) = 3;
This gives the list of user ids. You can use in, exists, or join to get additional information about the users if that is what you really want.

Removing users accounts with a zero order value inside wordpress multisite database

I am looking for a query to best remove user accounts from a multisite database, the database has over 20 thousand users account and a majority of them have 0 orders against them is it possible to delete customers that have no order against their account so in theory an sql query that selects the user where order amount is zero
Based on the example query you included in the question, it seems like Order is a column in your table that holds the total number of orders. If so, a delete command like below would work:
delete from user
where order = 0
If each order is shown by a different entry, you will need the list of UserIDs and use those in your delete command like below:
delete from Users
where UserID in (
Select F_UserID
From Orders
Group by F_UserID
Having count(*) < 1
)

SQL Server 2012 Database Slowness

So we have a system that uses two columns as the unique ID the userid as well as the a date. We have to keep every record that has ever been associated with a particular subject so there are no deleted records. So one subject can have 50 records. The database designer created views to get the latest row for a subject. Database is really not that huge in terms of record count we are roughly at 750000 records.
The view is written for every table very similar to:
Select Username,
UserID
From users
where USerID = 000
and UserUpdatedDate = (
Select MAX(UserUpdatedDate)
FROM
users a
WHERE a.USerID = UserID
)
We are seeing a major slowness, any suggestions would be welcomed?
We are rewriting some queries using temp tables, it seems to be quicker. Is this a good thing or bad in long haul
Replace this subquery
(Select MAX(UserUpdatedDate) FROM users a WHERE a.USerID = UserID ) with a join - subqueries are slow

Select entries from a list that do not occur in the query result

I use Postgres 9.1. I am running a query which is like
select count(distinct(user_id)) from users where user_id in (11,32,763,324,45,76,37,98,587);
Here the list of user_ids contains 600 entries. The result I am getting is 597. Thus there are 3 user_ids from the list, which are not present in the users. How do I get to know these 3 user_ids?
Please note that user_id is not the Primary Key of users
DISTINCT in your count-query only makes sense if user_id is not defined UNIQUE.
We don't need it either way for the query you ask for:
SELECT t.user_id
FROM unnest('{11,32,763,324,45,76,37,98,587}'::int[]) t(user_id)
LEFT JOIN users u USING (user_id)
WHERE u.user_id IS NULL;
Beware of NOT IN if NULL values can be involved on either side of the expression! IN / NOT IN with a long value list also scales poorly.
Details:
Optimizing a Postgres query with a large IN
Select rows which are not present in other table

Extract Both Counts AND Earliest Instance from my Dataset

Using Microsoft Sql 2000
I have a requirement to be able to email a monthly report that details a number of events.
(I have got the email bit sussed).
Amongst the data I need to email is a report on the number of certain courses people have attended. (so far so easy, couple of inner joins and a Count() and Im there.)
To add to that, some of the internal courses that we run have an expiry date which prompts a referesher course. I have been able to crudely get the data I need by using the sql code for part one and sticking the result set into a temp table, then by iterating over each row in that table, getting the user Id, querying the users course attendences, sorting it on date so that the earliest is at the top, and just taking the TOP 1 record.
This seems so inefficient, so is there any way I can ammend my current query so that I can also get the date of just the earliest course that the user attended?
i.e.
SELECT uName, COUNT(uId), [ not sure what would go in here] FROM UserDetails
INNER JOIN PassDates
ON PassDates.fkUser = uId)
GROUP BY uName, uId
where, for examples sake
UserDetails
uId
uName
and
PassDates
fkUser
CourseId
PassDate
Hope Ive explained this well enough for someone to help me.
To put an answer to the question..
SELECT uName, COUNT(uId), MIN(PassDate)
FROM UserDetails
INNER JOIN PassDates ON PassDates.fkUser = uId
GROUP BY uName, uId
You can turn it into a left join if you have users without any courses (yet)