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.
Related
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
)
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
I have many users with unique userID in table-1 with their specifications, I have other table-2 that Supervisors choose hours that users work. (I connect userID of table-2 to table-1 and it's ok)
supervisors are many so maybe their is many row with different hour for same useriD. So I create view with
select w_userid,sum(w_hour)as total from Workk group by w_userid
for show me one userID with sum of his hours. But I cant add it in database layer.
You should join two tables using 'inner join' and you have to use a 'where' condition. In where condition use 'AND' to check its the same person using the employee id and then sum the total working hours.
I'm currently playing around with SQL and trying to find the best way to accomplish this:
I currently have a user table that has a user_id, organisation_id, registered_datetime. There are
a number of users in this table with different organisations. There may be 3 different users in
1 organisation, or 1 in 1 organisation, etc.
I have added a new column called admin_user and I am trying to string up an SQL statement together
to update the admin user column. There can only be one admin user per organisation, and I want
the user who registered the earliest for that organisation to be the admin.
I could do this manually but it would take time if I had a lot of users. What would be the best
way to accomplish this?
EDIT:
So I have a number of users like this with the columns. The ones highlighted are the users that has registered the earliest. I want to be able to set those users as an admin user. The only admin user within their organisation and set the rest to 0. 1 (Admin) 0 (Not Admin)
This SQL query will mark users which registered_datetime are lowest in its organisation_id as admin.
UPDATE users SET admin_user = 1
WHERE user_id IN (
SELECT u.user_id FROM users u
WHERE u.registered_datetime IS NOT NULL AND NOT EXISTS(
SELECT 1 FROM users iu
WHERE iu.organisation_id = u.organisation_id AND iu.registered_datetime < u.registered_datetime
)
)
You might want to update all users to admin_user = 0 before this code, so all your users will have their values set.
One caveat here, if two users in one organisation were registered in exact same time, then both of them will be marked as administrators.
Update
I have added u.registered_datetime IS NOT NULL into the WHERE clause to filter out users with NULL in registered_datetime.
MSSQL
In MsSql server I usually solve this problem a in another way, by using ROW_NUMBER():
WITH base AS (
SELECT user_id, ROW_NUMBER() OVER ( PARTITION BY organisation_id ORDER BY registered_datetime ASC ) AS rn
FROM user
WHERE registered_datetime IS NOT NULL
)
UPDATE user SET is_admin = 1
WHERE user_id IN (
SELECT base.user_id FROM base WHERE rn = 1
)
This is too long for a comment.
You are describing three different tables:
Users
Organizations
UserOrganizations
The last has one row per user and per organization. This provides the mapping between the two. This can be called a "mapping" table, "junction" table, or "association" table.
How you implement one admin per organization depends on the database you are using.
You do not need the admin_user column. You need a column isadmin.
When a user is registered, if he is the first in the organization, then the isadmin column has the value 1, otherwise 0
Also, you can use the AAA query to find the administrator
SELECT `table`.`user_id`, MIN(`table`.`registered_datetime`) WHERE `organisation_id`=...
I'm trying to identify a list of users that all have the same set of IDs from another table.
I have users 1, 2, 3, and 4, all that can have multiple IDs from the list A, B, C, and D. I need to see how many users from list one have ONLY 3 IDs, and those three IDs must match (so how many users from list one have ONLY A, B, and C, but not D).
I can identify which users have which IDs, but I can't quite get how to get how many users specifically have a specific set of them
Here is the SQL that I'm using where the counts just aren't looking correct. I've identified that there are about 7k users with exactly 16 IDs (of any type), but when I try to use this sql to get a count of a specific set of 16, the count I get is 15k.
select
count(user_id)
from
(
SELECT
user_id
FROM user_id_type
where user_id_type not in ('1','2','3','4','5')
GROUP BY user_id
HAVING COUNT(user_id_type)='16'
)
So you want users with 3 IDs as long as one of the IDs is not D. How about;
select user
from table
group by user
having count(*) = 3 and max(ID) <> 'D'
The HAVING clause is useful in situations like this. This approach will work as long as the excluded ID is the max (or an easy change for min).
Following your comment, if the min/max(ID) approach isn't viable then you could use NOT IN;
select user
from table
where user not in (select user from table where ID = 'D')
group by user
having count(*) = 3
Following the updated question, if I've understood the mapping between the initial example and reality correctly then the query should be something like this;
SELECT user_id
FROM user_id_type
WHERE user_id not in (select user_id from user_id_type where user_id_type in ('1','2','3','4','5'))
GROUP BY user_id
HAVING COUNT(user_id_type)='16'
What is odd is that you appear to have both a table and a column in the table with the same name 'user_id_type'. This isn't the clearest of designs.