I am trying to get users with their count on notification_template_id.
Means if a user is targeted more than once with the notification_template_id 56.
E.G
user_id notification_template_id count
229 56 3
117 13 2
the query i am trying is:
SELECT
user_id,
notification_template_id,
count(notification_template_id) AS cnt
FROM
user_notifications
GROUP BY
user_id;
but it shows only total count of notifications per user.
the sql is:
SELECT
user_id,
notification_template_id,
count(notification_template_id) AS count
FROM
user_notifications
GROUP BY
user_id,
notification_template_id;
Related
I try few mysql statement but didn't come to my expectations.
How to get the total of to_user chat and order by the lowest total?
Let say in this case,
id 7 chat with 2 user
id 6 chat with only 1 user.
so the minimum will be id 6.
Can someone help me with sql statement?
This is what my expected result
count
to_user
1
7
2
6
I think your problem will be solved with the following code:
SELECT COUNT(DISTINCT(from_user)) AS total,to_user
FROM chats
GROUP BY to_user
ORDER BY total ASC
Make sure you index the junction in both directions, otherwise it will BYITA later!
ALTER TABLE messages
ADD INDEX idx_from_to (from_user, to_user),
ADD INDEX idx_to_from (to_user, from_user);
You may want to take into account the fact that the full list of chats for a given user is (from any user, to given user) UNION (to any user, from given user).
Consider user 1 in your sample data, who has sent messages to 5, 6, & 7 but not received any. And user 5 has sent to user 7 and received from user 1.
SELECT COUNT(DISTINCT(from_user)) AS count, to_user
FROM messages
GROUP BY to_user
ORDER BY count ASC
returns the following (which matches the expected result detailed in your question, errors aside)
count
to_user
1
5
1
6
2
7
whereas
SELECT COUNT(*) AS count, from_user AS user
FROM (
SELECT from_user, to_user FROM messages
UNION
SELECT to_user, from_user FROM messages
) t
GROUP BY from_user
ORDER BY count ASC;
returns
count
user
1
6
2
5
2
7
3
1
I am developing a WordPress website for e learning. So one student attends the course, many times and scored the mark many times. Now I need to get one id with score and last record. I have tried many examples, but am able to get the result. I have given below my code.
SELECT m.id
, m.email
, t.id_tracking
, t.user_id
, FROM_UNIXTIME(t.date)
, t.score
, t.groupe_id
FROM tracking t
join membres m
WHERE t.id_tracking IN (
SELECT MAX(date)
FROM tracking
GROUP BY user_id
)
I have used about the query I don't know what I did wrong
user_id email score date
1 test#testmail.com 78 15-06-2019
1 test#testmail.com 89 12-08-2019
2 sam#testmail.com 66 24-03-2018
2 sam#testmail.com 44 19-07-2019
3 siv#testmail.com 98 09-02-2019
3 siv#testmail.com 78 13-08-2020
I need to get result below like
user_id email score date
1 test#testmail.com 89 12-08-2019
2 sam#testmail.com 44 19-07-2019
3 siva#testmail.com 98 09-08-2020
You can GROUP BY email/user_id and select maximum of date from each group, by converting the date to a UNIX TIMESTAMP, like this
SELECT user_id, email, score, FROM_UNIXTIME(MAX(UNIX_TIMESTAMP(date)))
FROM tableName
GROUP BY user_id
I am not sure about your DB but,
Have you tried like this...
SELECT
*
FROM
(SELECT * FROM process_table ORDER BY date desc) tbl1
GROUP BY
tbl1.id
I want to concatenate fields intersected part of the same table
id user_id user_ip
4 971 108.54.218.114
5 972 108.54.218.114
6 973 108.54.218.114
7 974 108.54.218.114
8 975 107.222.159.246
9 975 98.54.818.133
In the example above, we can see that the user with the IP address (108.54.218.114) address to create multiple accounts with the following account IDs (971, 972, 973, 974), but also that the user with the account ID (975) is connect from the following IP addresses (107.222.159.246, 98.54.818.133)
I want to format the results like this
user_id user_ip
971,972,973,974 108.54.218.114
975 107.222.159.246, 98.54.818.133
MySQL
SELECT
GROUP_CONCAT(DISTINCT users_log.user_id) AS ID_LOG,
GROUP_CONCAT(DISTINCT users_log.user_ip) AS IP_LOG
FROM users_log
GROUP BY users_log.user_id
ORDER BY users_log.user_id DESC
If anyone can help me?
Thank you in advance for your help
There is another recent question (which I can't find) on SO very close to this. I asked about the intersecting row because of the (for lack of better term) recursiveness of this type of solution.
Here is SQL to give you your exact answer for that exact sample set.
SELECT user_id, GROUP_CONCAT(DISTINCT user_ip) AS user_ip
FROM users_log
GROUP BY user_id
HAVING COUNT(*) >1
UNION
SELECT GROUP_CONCAT(DISTINCT user_id) AS user_id, user_ip
FROM users_log
GROUP BY user_ip
HAVING COUNT(*) >1;
I need to find the count of users who have filled the questionnaire on that day.
These are the total counts , i need counts per day. Here is the picture of database: http://www.upload.ee/image/3800828/pildike.png
SELECT DISTINCT USER_ID, COUNT(ANSWER_TIME) AS ARV FROM RESULT WHERE ANSWER_TIME IS NOT NULL GROUP BY USER_ID ORDER BY ARV DESC;
For example this gives me:
32 2142
143 1098
26 979
76 878
But i need like distinct rows, answer_day_of_week is from 1 to 7, depending on day and answer_time is TIMESTAMP. The statistics is been for 97 days and for example person 32 has filled it 2000+ times in 97 days, but i only need to count them once...
I thought to use if-elseif or while or case or some sort of subquery ? I have tried some queries but i always fail...
For example for one day i can have 15 rows from one person on 1. oct but have 0 rows on 2. oct , then it gives answer that he has filled the survey only once.
SELECT USER_ID, COUNT(ARV) AS ARV
FROM
(
SELECT DISTINCT USER_ID, DATE(ANSWER_TIME) AS ARV FROM RESULT WHERE ANSWER_TIME IS NOT NULL
) A
GROUP BY USER_ID ORDER BY ARV DESC;
Please try the above query
Just change your sql query like this:
SELECT DISTINCT USER_ID, COUNT(ANSWER_TIME) AS ARV FROM RESULT WHERE DATE(ANSWER_TIME) >= DATE_SUB(CURDATE(), INTERVAL '97 DAYS') GROUP BY USER_ID ORDER BY ARV DESC;
I have a column that takes user names. How can I count the number of instances of a users name. For example I have 10 rows and in column username i want to count all names that show up multiple times. I would like to build a list of the top contributors to my database. So if username alex shows up 5 times and jeff shows up 3 and april shows up 2 times i will count this and from that I can build my list.
Try GROUP BY:
SELECT username, COUNT(*) AS user_count
FROM yourtable
GROUP BY username
ORDER BY user_count DESC
Try something like
SELECT USER_NAME, COUNT(USER_NAME) FROM YOUR_TABLE GROUP BY USER_NAME;
If you want to get a count of all the usernames then you just do the following SQL:
Select Count(*) from tablename
If you want to get just the count of unique usernames
Select Count(*) from tablename Group by username