SQL many-to-many count sum - mysql

I have this table:
id_user id_user2
1 54
1 53
1 53
1 54
1 54
1 55
2 23
2 23
2 20
2 21
2 25
2 25
And i would like to count, how many each of id_user have relationship with id_user2. Output should be:
id_user id_user2 result
1 54 3
1 53 2
1 55 1
2 23 2
2 20 1
2 21 1
2 25 2

You have to use group by clause
select id_user,id_user2, count(1) as result
from userstab
group by id_user,id_user2

try this query
select id_user,id_user2,count(id_user2) as result
from TABLE_NAME group by id_user2

Related

MySQL: select sum of column having specific values

What I'm trying to do is to select a specific amount of tickets (max 2) and every person that has the sum of the number of tickets less of 3 and the valid field has to be != 'e'
I have this table:
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
4
330
1
e
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
9
443
1
s
10
444
1
s
11
445
2
s
Here is what I did:
SELECT m.id, m.id_person, m.nr_tickets, m.valid
FROM table m
JOIN table m1 ON m1.id <= m.id
WHERE m.nr_tickets > 0
GROUP BY m.id
HAVING SUM(case when m.valid != 'e' then m1.nr_tickets end) <= 10
This query gives me
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
As you can see the query it's almost right, the thing is that the person 220 in the results has the sum of the tickets is greater than 2.
What I'm trying to achieve is to bypass the ID 6, and to have instead the ID 9
select `id`,`id_person`, sum(`nr_tickets`) as `nr_tickets`, `valid`
from `test`
group by `id_person`
having sum(`nr_tickets`) < 3 and `valid`!="e"
Output:
id id_person nr_tickets valid
5 331 1 s
7 441 1 s
8 442 2 s
9 443 1 s
10 444 1 s
11 445 2 s

FInding the columns count with mutliple columns

I have a table as follows:
ID PID TID CID DID IsTrue
1 43 1 2 621 0
2 43 1 2 621 1
3 45 2 3 621 1
4 46 2 3 621 0
and I need to find the count of PID,CID and with the where condition DID=621 along with the other fields.
how can I do that?
Thanks.
Try this
Select count(pid) from table where did = 121 group by pid.

mysql pivot query and rank Query

I Have a marksheet table like:
ID STUDENT_ID Branch_id Class_id Exam_id Subject_id Numbers Date
1 653 5 1 1 8 60 2012-01-01
2 653 5 1 1 9 40 2012-01-01
3 653 5 1 1 10 80 2012-01-01
4 653 5 1 1 11 50 2012-01-01
5 653 5 1 1 12 65 2012-01-01
6 653 5 1 1 13 33 2012-01-01
7 653 5 1 1 15 86 2012-01-01
8 222 5 1 1 8 100 2012-01-01
9 222 5 1 1 9 80 2012-01-01
10 222 5 1 1 10 92 2012-01-01
11 222 5 1 1 11 50 2012-01-01
12 222 5 1 1 12 65 2012-01-01
13 222 5 1 1 13 33 2012-01-01
7 222 5 1 1 15 86 2012-01-01
I want to get rank I got answer by this question
Also when I fetched all class result I use pivot query:
SELECT stu_id, sum(numbers) AS total, branch_id, depart_id, class_id,
SUM( IF( subject_id =1, numbers, 0 ) ) AS MAth,
SUM( IF( subject_id =2, numbers, 0 ) ) AS Eng,
SUM( IF( subject_id =3, numbers, 0 ) ) AS Science
FROM marksheet where branch_id = 1 AND depart_id = 1
AND class_id = 1 GROUP BY stu_id ORDER BY total DESC
I want to get rank in my class query (pivot query)? And I want to count how many students on first position and how many on second and third?
Required Data sample:
ID Name Math English Science Total Percent Position Rank
Any one help?
I think what you need to do is create a second table with grade boundaries that are being referenced so for instance :
ID grade start_boundry end_boundry
1 A 60 100
ect..
then create a join between the tables and then do a WHERE statement between the Numbers and the start/ end boundries
so ->
SELECT grade FROM boundries_table RIGHT JOIN sudent_table
WHERE boundries_table.start_boundry < student_table.numbers
AND boundries_table.end_boundry > student_table.numbers
i think that should work if my MySQL memory serves, just modify the table to how you need it to run and it should work for how you need it.

Dynamic Get Rank By Mysql Query

I have a result table like:
ID STUDENT_ID Branch_id Class_id Exam_id Subject_id Numbers Date
1 653 5 1 1 8 60 2012-01-01
2 653 5 1 1 9 40 2012-01-01
3 653 5 1 1 10 80 2012-01-01
4 653 5 1 1 11 50 2012-01-01
5 653 5 1 1 12 65 2012-01-01
6 653 5 1 1 13 33 2012-01-01
7 653 5 1 1 15 86 2012-01-01
8 222 5 1 1 8 100 2012-01-01
9 222 5 1 1 9 80 2012-01-01
10 222 5 1 1 10 92 2012-01-01
11 222 5 1 1 11 50 2012-01-01
12 222 5 1 1 12 65 2012-01-01
13 222 5 1 1 13 33 2012-01-01
7 222 5 1 1 15 86 2012-01-01
My Desire Result like:
Student_ID Math English Science Total Rank
1 90 89 88 267 1
2 90 89 88 267 1
3 58 45 98 201 2
I want to get student rank by this method:
Reference Link
SET #rank = 0, #prev_val = NULL;
SELECT rank, correct FROM
(
SELECT
#rank := IF(#prev_val=correct,#rank,#rank+1) AS rank,
#prev_val := correct AS correct, uid
FROM quiz_user
ORDER BY correct DESC
)as result WHERE uid=xxxxxxxxxxxx
This query what I need only difference between table structure the author of post assign a rank on correct column and I need to assign rank on numbers SUM(numbers) column after sum all numbers.
Please Help.
Try this:
SELECT STUDENT_ID, Numbers, IF(#marks=(#marks:=Numbers), #auto, #auto:=#auto+1) rank
FROM (SELECT STUDENT_ID, SUM(Numbers) Numbers
FROM quiz_user
GROUP BY STUDENT_ID
ORDER BY Numbers DESC, STUDENT_ID
) AS A, (SELECT #auto:=0, #marks:=0) AS B;

count items in col as rows MYSQL

Dear developers and programmers,
I have a tabel with SHOP_ID, PROD_ID, ipadres and some more tables, but these are where its all about...
Looks like this:
id ip number timestamp shop_id prod_id
--------------------------------------------------
42 81.69.205.25 1319488326 2 3
43 81.205.141.48 1319492649 2 3
44 193.58.10.10 1319520579 14 17
45 84.28.22.226 1319529529 11 19
46 88.15.81.188 1319543745 2 1
47 178.17.241.191 1319563031 14 7
48 87.28.107.171 1319563038 2 6
49 80.156.47.144 1319572818 14 7
50 82.76.241.175 1319577506 11 1
51 82.76.241.175 1319577584 13 1
52 82.76.241.175 1319577785 14 1
53 82.76.241.175 1319577860 4 1
54 62.94.133.153 1319579221 14 1
55 62.94.133.153 1319579281 2 3
56 77.70.175.221 1319617238 11 1
57 77.70.175.221 1319621845 13 1
58 77.70.175.221 1319621848 2 1
59 77.70.175.221 1319621850 11 1
.... more
--------------------------------------------------------
Is there a way to see for each prod_id how many ip numbers there excist for each shop id?
output example
1 2 3 4 5
---------------------------------------------
1 18 5 51 8 4
2 58 5 45 3 4
3 7 6 31 9 2
where horizontal is the prod_id and vertical is the shop_id
I've don this already:
select
shop_id,
count(distinct(ipadres)) amount
from table
GROUP BY shop_id
order by amount desc
but this wil give me only the result of all prod_id's combined.
I would like to have the prod_id's seperate in columns.
I hope there is a solution!
Kind Regards
Try this
select `prod_id`,shop_id,
count(distinct(ipadres)) amount
from table
GROUP BY shop_id,prod_id
order by amount desc