I would like to count(*) how much customers have created a post or made a comment. If the same customer has made several posts and comments, it should count only once.
Customer Table:
ID Name ...
1 Jonh
2 Mark
3 King
4 Doe
Post Table:
ID USER_ID...
1 1
2 1
3 3
4 1
Comment Table:
ID USER_ID...
1 1
2 3
3 3
4 4
It should return count(*) = 3
(user_id: 1, 3 and 4).
Try this one. It worked for me and returns what you're looking for:
SELECT COUNT( USER_ID ) AS TOTAL
FROM (
SELECT USER_ID
FROM POSTS
UNION
SELECT USER_ID
FROM COMMENTS
)X
I used POSTS and COMMENTS as table names bc I was unsure what your exact table names are, so make sure to change these in your query.
This should work:
SELECT COUNT(DISTINCT USER_ID) FROM (
SELECT USER_ID FROM POST_TABLE
UNION
SELECT USER_ID FROM COMMENT_TABLE
)
Related
i have 2 tables as following.
User
id name
---------------
1 john
2 raju
3 manu
4 raghu
friendtable
id userid recvId
------------------------
1 1 2
2 1 3
3 2 3
4 3 4
Is it possible to filter users by their friends count from these tables.Please help me.
For eg :- range >=3 will result : john,manu
range >3 and range <2 will result : raju
range <2 result : raghu
Do a UNION ALL to get all id's from friendstable in one column. Join users table with that result.
Do a GROUP BY, adjust HAVING to decide what to return, e.g. at least 3 times etc.
select u.name
from users
join (select userid as id from friendtable
union all
select recvId as id from friendtable) f
on u.id = f.id
group by u.name
having count(*) >= 3
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recvid>=3
SELECT name FROM user a,friendtable b WHERE a.id=b.id and b.recvid>3 AND b.recid<2
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recid<2
Sorry I don't really know how to make a title for this because I can't explain it really. for example i have here a table
c_id emp_id clinic_id
1 1 1
2 1 2
3 2 1
4 3 3
5 1 3
now i will do a query like this
select distinct * from table where clinic_id <> 1
And the result would be
c_id emp_id clinic_id
2 1 2
4 3 3
5 1 3
at this point I need help, if from the where clinic_id <> 1 a certain emp_id is within its row of condition(Sorry for my bad english). for example emp_id 1. All emp_id 1 must not be display also.
So the result would be just
c_id emp_id clinic_id
4 3 3 // *The result I want*
You can use NOT EXISTS for this:
select distinct *
from mytable as t1
where clinic_id <> 1 and
not exists (select 1
from mytable as t2
where t1.emp_id = t2.emp_id and t2.clinic_id = 1)
For the result you're looking for, wouldn't something like this be simpler?
SELECT DISTINCT * FROM table WHERE clinic_id !=1 AND emp_id !=1
Here we're saying we want any clinic_id that is not 1 and any emp_id that is also not 1.
Since you're dealing with PHP, then you would simply substitute the numbers for the variables you're trying to not match.
SELECT DISTINCT * FROM table WHERE clinic_id !=$session_variable AND emp_id !=$some_value
SELECT DISTINCT *
FROM table
WHERE clinic_id <> 1
AND emp_id NOT IN
(SELECT DISTINCT emp_id
FROM table
WHERE clinic_id = 1)
Try this one.
It uses the subquery to return the emp_ids which are in the same row as the 1 in the column clinic_id, and removes them from the resultset because you also don't want those emo_ids.
Also you could use a GROUP BY clause instead of DISTINCT. Usually GROUP BY would be turned into a distinct by the database if you are not using any aggregate functions, but sometimes they behave differently. If you are interested in this topic you can also see this question: Is there any difference between GROUP BY and DISTINCT
SELECT c_id, emp_id, clinic_id
FROM clinics
WHERE clinic_id <> 1
AND emp_id NOT IN
(SELECT DISTINCT emp_id
FROM clinics
WHERE clinic_id = 1)
GROUP BY c_id, emp_id, clinic_id;
user_id | group_id
------------------
5 3
6 1
6 3
7 1
7 2
8 2
My join table looks like this. The query I'm trying to accomplish is finding if two user_ids have a matching group_id.
I don't need any specific information back from the query, just a simple true or false.
Try this:
select
(select group_id from tbl where user_id = 'user_id1')
<=>
(select group_id from tbl where user_id = 'user_id2');
Database structure
Table 'applicants'
id org_id team_id
1 1 1
Table 'teams'
id name
1 Test
Table 'teams_members'
id team_id user_id
1 1 1
2 1 2
Table 'users_playeraccounts'
id user_id summoner_id rank_solo
1 1 1 5
2 1 2 8
3 2 3 7
select sum(rank_solo) as rank_sum,
max(rank_solo) as highest_rank,
count(tt.id) as members,
t.name,
o.team_id
from applicants o
join teams t on o.team_id = t.id
join teams_members tt on t.id = tt.team_id
join users_playeraccounts p on tt.user_id = p.user_id
where org_id = :org
group by team_id
This offcourse gives me a result like
rank_sum highest_rank members name team_id
20 8 3 Test 1
Is there a way for me to get both the count of members with their playeraccounts aka
If 1 user has 2 it'll be 2
And also a way for me to keep it as 1 so it literally just counts the rows found in teams_members neglecting the entries in users_playeraccounts?
I want to receive both 2 and 3 as a result of my query.
You want to count the distinct number of entries in tt.id, so you can do that like this:
SELECT ... COUNT(DISTINCT tt.id) AS distinct_members ...
Rather than giving you a count of every row that has a non-null tt.id, you'll get a count of the number of unique values.
I am aggregating data and I cannot sum certain columns so I would like to take the most frequent observation from that column, or the median value. Example follows, thanks in advance.
ID site
1 3
1 3
1 2
1 3
2 4
2 5
2 5
2 5
I want it to look like
ID Site
1 3
2 5
WITH temp AS(
SELECT ID, Site, COUNT(*) As counts
FROM id_table
GROUP BY ID, Site
)
SELECT temp.ID, temp.Site
FROM temp
JOIN (SELECT ID, MAX(counts) max_counts
FROM temp
GROUP BY ID
)b
ON temp.ID = b.ID
AND temp.counts = b.max_counts
ORDER BY ID ASC
SQL Fiddle