getting unique user number for visited_pages in sql how? - mysql

My table is Person. I am trying to get unique user number for each visited page, but I cant find to use distinct, count and group by at the same query. Do you have any solution guys ??
Persons:
user
visit_page_no
11
66
11
66
11
66
44
66
55
66
55
77
99
77
33
88
Wanted result:
visited_page_no
count
66
3
77
2
88
1

You seem to want count(distinct):
select visit_page_no, count(distinct user)
from t
group by visit_page_no;

Related

Calculate the number of users based on the below conditions by writing a single query SQL

There are two campaigns running campaign A and Campaign B and list of user ids participated in those two campaign is given below. Calculate the number of users based on the below conditions by writing a single query.
Participated in campaign A
Participated in campaign B
Participated in campaign A only
Participated in campaign B only
Participated in both the campaigns
Participated in either campaign A or Campaign B
Campaign A Campaign B
user_id user_id
91 62
27 11
58 16
50 92
64 17
65 71
54 12
98 37
78 93
24 58
31 54
73 94
63 85
72 30
94 32
20 1
38 48
8 99
43 45
33 46
26 39
100 29
61 49
87 73
84 81
15 88
80 70
77 33
40 55
82
42
56
95
88
I am not able to figure out how to write in single SQL query.
Assuming you have two different tables, you can use union all and aggregation:
select in_a, in_b, count(*) as num_users
from ((select user_id, 1 as in_a, 0 as in_b
from a
) union all
(select user_id, 0 as in_a, 1 as in_b
from b
)
) u
group by in_a, in_b;
This gives you all the information you need. You can use group by in_a, in_b with rollup to get all combinations.
Or, you can summarize this into one row:
select sum(in_a) as in_a, sum(in_b) as in_b,
sum(in_a * (1 - in_b)) as in_a_only,
sum(in_b * (1 - in_a)) as in_b_only,
sum(in_a * in_b) as in_ab
from ((select user_id, 1 as in_a, 0 as in_b
from a
) union all
(select user_id, 0 as in_a, 1 as in_b
from b
)
) u;
Note: These both assume that users are unique in each campaign. If not, just use select distinct or union in the subquery.

Select option of same table - MySQL

I have a table like this:
id product option
1 55 78
2 55 55
3 55 42
4 68 78
5 68 62
6 68 36
7 94 25
8 94 47
9 94 81
How can I select all option where I have option 78 for example? I need to select 55, 42, 62, 36 because product have option 78.
select distinct `option`
from your_table
where product in (select distinct product from your_table where `option` = 78)

Select data basis of two values of one column matches of a table

suppose this is my table structure of table user
id field_id user_id value
1 1 37 Lalit
4 2 37 Test
5 13 37 123
6 18 37 324
7 28 37 english
8 33 37 203
9 21 37 201
10 1 39 Mukesh
11 2 39 Test
12 13 39 523
13 18 39 245
14 28 39 French
15 33 39 278
16 21 39 2897
So I wnat to get the result to match the two or three values from the column value and want the result
I made query like
SELECT DISTINCT user_id FROM user where value =123 AND value=523;
But it is not working please give solution how we get the result
A value in a row, as per your example, cannot be both 123 and 523. You have to use OR
SELECT DISTINCT(user_id) FROM user WHERE value=123 OR value=523;
Alternatively you can also use IN clause
SELECT DISTINCT user_id
FROM user
WHERE value IN (123, 523);

Ranking Based On Aggregate Sum Derived Table Without #variables

This is been a headache challenge for me in my project, being a php and or say sql novice.
I must first of all indicate that i'm running this code in php with binded params, since I am using pdo.
Now, as the title indicates, how can I rank students in a class, after having calculated their raw scores from Aggregate Sum Derived Table (From Clause), without #variables in sql. Something like, let assume my data pulled from the database is:
studref Name English Maths Gov
Bd1 Ida 66 78 49
Bd2 Iyan 58 80 69
Bd3 Ivan 44 56 80
Bd4 Iven 63 92 68
Bd5 Ike 69 77 59
So using Aggregate Sum in derived table, I then add another column, which is summation of the marks from the various subjects like:
SUM(THE SCORES) AS accum_raw_scores
studref Name English Maths Gov accum_raw_scores
Bd1 Ida 66 78 49 193
Bd2 Iyan 58 80 69 207
Bd3 Ivan 44 56 80 180
Bd4 Iven 63 92 68 223
Bd5 Ike 69 77 59 205
So, what I want to do next is to add another column, which will represent the rank of each student, based on his/her total score from the subjects. Hence, this is where I want the code below to handle that for me:
1+(SELECT COUNT(*)
FROM (SELECT s.studref, SUM(s.subjectscore) AS total_score
FROM studentsreports s
GROUP BY s.studref) AS sub
WHERE sub.total_score > main.accum_raw_scores) AS overall_position,
So that in the end, I will have something like:
studref Name English Maths Gov accum_raw_scores rank
Bd1 Ida 66 78 49 193 4
Bd2 Iyan 58 80 69 207 2
Bd3 Ivan 44 56 80 180 5
Bd4 Iven 63 92 68 223 1
Bd5 Ike 69 77 59 205 3
Unfortunately, I have tried several approaches but to no success. Please help!
Meanwhile, I want to try as much as possible to do without variables.

Comparison of multiple rows using a single identifier in SQL

I would like to compare three columns in two different tables by joing through unique identifier.But my for single identifier there are multiple rows will be returned.
Example:
Table A
Identifier Flag1 Flag2 Flag3
1 56 36 46
1 89 65 33
1 56 89 22
1 11 89 65
Table B
Identifier Flag 1 Flag2 Flag3
1 56 36 46
1 89 65 33
1 56 89 22
1 10 89 65
Now i would like to compare these two tables based on Identifier 1 , can you please help me out if all the column values are matching i need update the flag.Thanks in advance
I think below code will help,
below code will update flag of table A for records whose column values are same as that of table b
update a
set someflag = 1
where exists
(
select * from B
where b.flag1 = a.flag1
and b.flag2 = a.flag2
and b.flag3 = a.flag3
)