post_id organisation_id
1 5
2 57
3 57
4 57
5 58
6 59
7 60
8 61
8 62
9 59
11 57
12 57
4 62
is there anyway to select post_id, organisation_id1, organisation_id2 in a single row?
Since you didn't specify what your table looks like and what would be the outcome that you would like, this is just a guess.
SELECT
a.post_id,
a.organisation_id,
b.organisation_id
FROM your_table a
LEFT JOIN your_table b
ON a.post_id = b.post_id AND a.organisation_id < b.organisation_id
Is this what you're looking for?
select post_id, group_concat(organisation_id)
from YourTable
group by post_id;
Related
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.
good evening,
i have a table:
A B C
45 1 1
22 2 1
40 3 1
43 1 2
21 2 2
61 3 2
49 4 2
60 5 2
76 1 3
41 2 3
57 3 3
i find max(A) from max(B) group by C. The result should be 60 - max number in A from last row in B from each group (C)
Thank you for your help
If i understand correctly your question you could use an inner join on select max(b):
select max(A)
from my_table m
inner join (
select C,
max(B) act_B
from my_table
group by C
) t on t.act_B = m.B and t.c = m.c
I have a table which have a lot of data, it's have a category ID and postId, I need to read 3 new post per category with same CatID.
it's not duplicate of the question suggest by other people. Please check that in my question the postid catid can be anything when in duplicate question it's calculate before running query.
What I have written is
SELECT
MAX(` postid `) AS p1,
` catid ` AS c1
FROM
` postcategory `
GROUP BY
` catid
I can put 2 other query in it union distinct but it will make a query a lot big. Is there any good way to do this in MySQL. What I am looking for reading 3 postId (maximum) belong to same category.
postId catId
------ --------
9 3
15 3
16 3
17 3
18 3
19 5
20 8
21 6
22 8
23 6
46 6
46 8
26 3
25 3
27 5
28 3
37 6
39 10
40 6
41 6
42 6
43 6
44 5
45 11
63 6
64 5
65 6
66 6
68 6
You can read 3 new post from each category Using the below query.
SELECT
p1.postId,
p1.catId
FROM
postcategory p1
JOIN postcategory p2 ON p1.catId = p2.catId
AND p2.postId >= p1.postId
GROUP BY
p1.postId,
p1.catId
HAVING
COUNT(*) <= 3
ORDER BY
catId,
postId
Here you can see the Live Demo
Output:
I have following MySQL table structure,
id product_id filter_tag_id
14 1 48
17 3 49
18 10 49
19 10 54
20 11 49
21 11 55
22 12 49
23 12 56
24 9 48
25 9 52
26 6 48
27 6 53
28 7 48
29 7 56
30 8 48
31 8 53
32 13 48
33 13 52
34 14 48
35 14 54
36 14 55
37 15 48
38 15 55
i need to fetch only those product_id's which have same filter_tag_id's,
For example only one product_id (9 and 13) having the same filter_tag_id (48 and 52), so I need to fetch only product_id 9 and 13, I'm trying following query, but no success yet.
select product_id from filter_data where filter_tag_id=52 AND filter_tag_id=48;
select product_id from filter_data where filter_tag_id in (52,48);
First query return no result and second one returning wrong results
Use self-join. It looks like more complicated than GROUP BY, but it is faster than group-by. Because with GROUP BY approach, those which only has 48 or 52 should be groupped that is not unneeded rows.
SELECT t1.product_id
FROM filter_data t1 INNER JOIN filter_data t2 ON t1.product_id = t2.product_id
WHERE t1.filter_tag_id = 48 AND t2.filter_tag_id = 52;
If what you want is to find only product_ids having filter_tag_id values equal to 48 and 52 and nothing else but these two values, then try:
SELECT product_id
FROM mytable
GROUP BY product_id
HAVING COUNT(CASE WHEN filter_tag_id = 48 THEN 1 END) > 0 AND
COUNT(CASE WHEN filter_tag_id = 52 THEN 1 END) > 0 AND
COUNT(CASE WHEN filter_tag_id NOT IN (48,52) THEN 1 END) = 0
Demo here
use GROUP BY
Select product_id,COUNT(DISTINCT filter_tag_id) filter_match
from filter_data where filter_tag_id in (52,48)
GROUP BY product_id
HAVING filter_match = 2
the value of filter_match is count you will pass in condition
I am (very) new to MySQL. Forgive my lack of knowledge....
I am working on a hockey stat database where I need to add all the assists and all the goals together to get "total points". I have two queries already figured out, but I am not able to figure out how to sum the two.
Here are the queries:
select player_id, count(*)
from(select * from 1st_assists
union
select * from 2nd_assists) as tem
join players on tem.fk_player_id=players.player_id
group by fk_player_id
order by count(*) desc
select player_id, count(*)
from goals_for
join shots_for on goals_for.fk_shot_for_id=shots_for.shot_for_id
join players on shots_for.fk_player_id=players.player_id
group by player_id
order by count(*) desc;
how do I combine these two queries into one and get the total of both counts?
Here are the results of each of the queries
Total Assists
player_id count(*)
79 24
55 22
45 17
90 16
40 15
65 15
37 13
1 13
20 11
84 11
64 10
27 9
93 7
8 5
24 3
57 1
Goals
player_id count(*)
90 38
37 28
40 19
55 13
45 11
1 8
24 8
20 8
84 8
27 6
8 5
79 4
65 4
93 1
64 1
It is untested, but can you, please, try this:
select p.player_first_name, p.player_last_name, (count1+count2) as total_count
from
(select player_id, count(*) count1
from(select * from 1st_assists
union
select * from 2nd_assists) as tem
join players on tem.fk_player_id=players.player_id
group by fk_player_id
order by count(*) desc) q1
left join
(select player_id, count(*) count2
from goals_for
join shots_for on goals_for.fk_shot_for_id=shots_for.shot_for_id
join players on shots_for.fk_player_id=players.player_id
group by player_id) q2
ON q1.player_id=q2.player_id
left join player p ON q1.player_id=p.player_id
order by (count1+count2) desc;