How to select the total unique number in SQL - sql

Say if I want to select total number of unique group which type is 'A' in User table, how can I write the query?
*multiple users may belongs to a same group, there is a field called 'group', another field called 'user type'
Searched on google but didn't find the right one, any answers are welcomed!

Your question isn't terribly clear, but it sounds like you might be after:
SELECT COUNT(DISTINCT group)
FROM table
WHERE type = 'A'

Without the full design, I have to imply a lot of things.
In this exemple, I suppose that :
Users have an Id, a Name and that GroupId is the Foreign Key.
Group has an Id as Primary Key.
This would do it. In that case.
SELECT COUNT(DISTINCT id), username FROM Users
JOIN Groups ON Users.GroupId = Groups.Id
WHERE Users.UserType = 'A'
GROUP BY username

Related

group_concat multiple rows where given condition

I have 3 tables (User, User_usergroups , usergroup). I would like to get a list of user who has usergroup equal to "member" and other groups (group_concat) belonging to this user. How do I do that?
I got the first part of the query, but I could not get other groups, aggregated groups of this user (I like to use group_concat to concatenate other groups in one field.
SELECT user.userId, (group_concat(group_.name)???)
FROM User_ user join Users_UserGroups userGroups
on user.userId= userGroups.userId
join UserGroup group_ on userGroups.userGroupId = group_.userGroupId
WHERE group_.name="member";
Assume that is the 3 tables
The outcome will be
Many thanks for your great help.
It looks like my query is so complex and it has down vote from the administrator or someone. I wish whoever did this could add their comment to my query so that, in the future, I will know what I need to do to clarify my question to the community or make my question better. Here is the answer to my question.
First retrieve the list of userId.
Then join this "member" table with other tables to find the result from the list of retrieved ID (step 1).
As we aggregate the name of the groups, we need to use groupby.
Keywords to resolve this problem is to use an alias for subquery or nested SQL
select member.userid, member.screenName, group_concat(member.name)
from (SELECT User_.userid userid, User_.screenName screenName , UserGroup.name name
FROM UserGroup
JOIN Users_UserGroups
ON UserGroup.userGroupId = Users_UserGroups.usergroupid
JOIN User_
on Users_UserGroups.userId = User_.userId
WHERE UserGroup.name = "member")member
JOIN Users_UserGroups
ON member.userid = Users_UserGroups.userId
JOIN UserGroup
on UserGroup.userGroupId=Users_UserGroups.userGroupId
GROUP BY member.userid;

Oracle Apex count and display number of entries in a column

I have a table in Oracle Apex populated with contacts and their information. On the dashboard of my app, I'd like it to say how many contacts are in the database using the big list of values plug-in or something like that. I'm using the following code:
select contact_ID, count(*)
from
contacts
group by contact_id
However all I'm getting is the number of times each specific contact appears in the table (which is 1), instead of the total number of contacts. I understand what I did wrong in the code, but I'm not sure how to fix it.
For further information, the table columns are very basic, just 'Name', 'contact_id' (the primary key), 'Phone_Number', and so on. The table is called CONTACTS.
I'm also looking to eventually have the total number of organizations and comments on the dashboard as well.
Thanks!
Try
SELECT
COUNT(mycolumn) AS d,
COUNT(mycolumn) AS r
FROM
mytable
I think you just want count(*):
select count(*)
from contacts;
Presumably, contactID is unique in a table called contacts.

How do I store multiple pieces of individual data within a single column?

I have a table which stores information about a group. Information such as the group name, group id, the number of members... However, I would also like to store the codes of each individual member within a group.
The way I initially tried to store the member codes was by creating individual columns. So I would have columns named MemberCode1, MemberCode2, MemberCode3. But the problem is a group can have 100 members which would mean 100 columns would be required to store the member codes individually.
My question: is there a way I can store an x amount of member codes within a single column within my table, or do simply make y columns and limit the number of users in a group to y?
This is a great example of when to use a separate table. You can create a second table, called "group", and store your group data there (group ID, group name).
I'm assuming that a member can belong to multiple groups, and a group can have many members.
If so, you can then have a table in the middle of those two tables to capture the members in each group. You can call this table "memberships" or "group_member" or something.
It would contain the Primary Keys of each of the entries in the member and group tables.
So your table structure could be:
MEMBER (member ID... other fields)
GROUP (group ID, group_name)
MEMBERSHIP (member_id, group_id)
Finally, to find the number of members in each group:
SELECT group_id, COUNT(*)
FROM membership
GROUP BY group_id;
Or to get group data with that:
SELECT m.group_id, g.group_name, COUNT(m.*)
FROM membership m
INNER JOIN group g ON m.group_id = g.group_id
GROUP BY m.group_id;
Generally speaking it is not a good idea to store multiple pieces of information in the same field. It violates normal form and, more practically, it is hard to maintain.
The best solution to your issue would be to create a separate table for the members (Member) and include a foreign key, perhaps groupID or group_id, referring to the group table (Group).
In the event that a "member" can belong to more than one "group", you would create a third table, MemberGroup, with a composite primary key (member_id, group_id) made up of foreign keys referring to Member and Group respectively.

SQL - I need to see how many users are associated with a specific set of ids

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.

SQL - Get answer from query as a single number

The following code returns a couple of numbers, identifying people who take part in more than three activities.
SELECT pnr
FROM Participates
GROUP BY pnr
HAVING count(activities)>3;
I want the answer to be the number of people who participate in more than three activities though, i.e. "4", instead of four unique numbers. What to do?
Access supports derived tables.
SELECT COUNT(*) AS NumberOfParticipants FROM
(
SELECT pnr
FROM Participates
GROUP BY pnr
HAVING count(activities)>3
) T
You will need a WHERE clause on the pnr field to uniquely identify one of your groupings:
SELECT COUNT(pnr)
FROM Participates
GROUP BY pnr
WHERE pnr = 'whatever'
HAVING COUNT(activities)>3
The order of my clauses might be wrong
Select Count(Distinct pnr)
From Participates
Having Count(activities) > 3