This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 9 years ago.
I want to get the result set only by one query if possible
i have table like that
------------------------------
user_id | activity_id | score
------------------------------
1 | 1 | 100
------------------------------
1 | 1 | 50
------------------------------
1 | 2 | 20
------------------------------
1 | 3 | 10
------------------------------
1 | 3 | 50
------------------------------
2 | 1 | 300
------------------------------
3 | 3 | 10
------------------------------
3 | 2 | 40
------------------------------
All what i need to select each user with sum of all of his high score in each activity.
for example the result set should be like that:
------------------------------
user_id | score
------------------------------
1 | 170
------------------------------
2 | 300
------------------------------
3 | 50
------------------------------
To get the highest score for each user/activity pair, you would use the following query:
select user_id, activity_id, max(score) as highscore
from t
group by user_id, activity_id;
To add these up for a given user, you would make this a subquery and do another aggregation:
select user_id, sum(highscore)
from (select user_id, activity_id, max(score) as highscore
from t
group by user_id, activity_id
) t
group by user_id;
Related
So, as the title says, I think I want to get the SUM of a row in column A (Meta Value) based on matching DATEs in column A (Meta Value) joined from IDs in column B (Item ID).
Essentially, I want to look for a specific date 2017-05-05 in Meta Value. Then, when a date is a match, find the Item ID. In the example below, this would be 2 and 3. Then, get the SUM of the Field ID (11) for both Item ID 2 and Item ID 3 and return the SUM to a variable in PHP.
Here is what my data looks like:
+-----------+------------+------------+
| Meta Value| Field ID | Item ID |
+-----------+------------+------------+
| John | 8 | 1 |
|john#e.com | 10 | 1 |
| 2 | 11 | 1 |
|2016-11-20 | 12 | 1 |
| Mary | 8 | 2 |
|mary#e.com | 10 | 2 |
| **5** | 11 | 2 |
|2017-05-05 | 12 | 2 |
| Mike | 8 | 3 |
|mike#e.com | 10 | 3 |
| **2** | 11 | 3 |
|2017-05-05 | 12 | 3 |
+-----------+------------+------------+
I am after the SUM of 7 from Mike and Mary.
My current wordpress php call looks like this (but only gets me row count):
$bookings = $wpdb->get_var("SELECT COUNT(*) FROM wp_frm_item_metas WHERE field_id=12 AND meta_value='$select_date'");
Any help is appreciated!
seems you need sum on a self join
select sum(a.field_id)
from wp_frm_item_metas a
inner join wp_frm_item_metas b on b.item_id = a.item_id
and b.meta_value = '$select_date'
but if you need the sum for meta_value (11) you should
select sum(a.meta_vale)
from wp_frm_item_metas a
inner join wp_frm_item_metas b on b.item_id = a.item_id
and b.meta_value = '$select_date'
and a.filed_id =11
I am sorry for the vague title but I really cannot make this any more specific with my English.
I have this table called posts__tags that I use to associate post ids and tag ids with the following columns
record_id, post_id and tag_id. Here is an example sample
record_id | post_id | tag_id
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 2
5 | 3 | 1
6 | 3 | 3
7 | 4 | 2
8 | 4 | 3
9 | 4 | 4
I have a php function that has an array of tag_ids as a parameter. Lets say for example that my parameter is the array(2,3). I need to select all the post_id's that are associated with tag_id = 2 AND tag_id = 3.
In the above example I should get the 1 and 4 post_id's since only those are associated with both 2 and 3 tag_id 's.
What is the best way to achieve that with a MySQL query?
Thanks in advance
select post_id from posts__tags where tag_id in (1,2) group by post_id having count(*)=2
Try this
select distinct post_id from table_name where tag_id in (2,3)
I have a MySQL table that is formatted as follows:
group_clue:
---------------------------------------------------
| id | group_id | clue_id | completed | run_order |
---------------------------------------------------
| 1 | 1 | 2 | 1 | 1 |
| 2 | 1 | 6 | 1 | 2 |
| 3 | 1 | 4 | 1 | 3 |
| 4 | 1 | 3 | 0 | 4 |
| 5 | 1 | 1 | 0 | 5 |
| 6 | 1 | 5 | 0 | 6 |
| 7 | 2 | 9 | 1 | 1 |
| 8 | 2 | 2 | 0 | 2 |
...
---------------------------------------------------
The data above in the group_clue is constructed such that each group_id has every clue_id at some run_order (ranging from 1 to the number of clue_ids and not repeating for a particular group).
First Question
I want to create a table showing the first clue_id for each group_id where completed = 0 when ordered by run_order (aliased as current_clue). Using the above example, this would give:
---------------------------
| group_id | current_clue |
---------------------------
| 1 | 3 |
| 2 | 2 |
---------------------------
My preliminary attempt is:
SELECT group_id, MIN(clue_id) as current_clue
FROM group_clue
WHERE completed = 0
GROUP BY group_id
However, this returns the same clue_id for each group_id.
Second Question
From the data in the first question, I would like to compose a final table where I GROUP_CONCAT() these results so that it contains every current_clue and each group_id that contains that current_clue. I would also like it ordered from those clues with the most group_ids to those with the fewest. An example resulting table is:
--------------------
| clue | group_ids |
--------------------
| 3 | 1,5,4,3 |
| 2 | 2,6 |
--------------------
I cannot figure out the ordering. My preliminary attempt is:
SELECT clue_id, GROUP_CONCAT(group_id)
FROM [resulting_table]
GROUP BY clue_id
ORDER BY [something]
Any help is appreciated: what queries would fit this scenario?
The first part of your question can be solved this way (it expects that run_order is unique per group):
SELECT t1.group_id,
t1.clue_id AS current_clue
FROM group_clue t1
INNER JOIN (SELECT group_id,
MIN(run_order) as run_order
FROM group_clue
WHERE completed = 0
GROUP BY group_id) t2 USING (group_id, run_order)
The logic of this query is pretty simple:
The inner query selects the pairs of group_id and the corresponding minimal value of run_order which has the completed = 0.
After that we join the original table to this set of pairs so that we could select the corresponding clue_id additionally.
You can sort by number of elements per group using
ORDER BY COUNT(*) DESC
I have this 2 table in database in sql
TABLE1:
USER id | state| TYPE | time |
1 | 1 | 1 | time |
2 | 1 | 2 | time |
3 | 1 | 2 | time |
4 | 1 | 2 | time |
5 | 0 | 1 | time |
6 | 0 | 1 | time |
TABLE2:
id |USER id| run
1 | 3 | 7
2 | 1 | 5
3 | 1 | 5
4 | 4 | 8
5 | 2 | 6
6 | 2 | 6
7 | 3 | 7
8 | 3 | 7
9 | 3 | 7
10 | 3 | 7
11 | 2 | 6
12 | 4 | 8
13 | 4 | 8
14 | 1 | 5
15 | 2 | 6
16 | 2 | 6
17 | 5 | 9
18 | 4 | 8
I am printing this
SELECT * FROM TABLE1 WHERE state != 0
it will print row this way
USER ID 1
USER ID 2
USER ID 3
USER ID 4
But I want to ascending this by count
WHERE, Count = num of row of TABLE2
Where user id=1 or 2 or..N
Here:
count of USER id 1 = 3
count of USER id 2 = 5
count of USER id 3 = 5
count of USER id 4 = 4
Now i want to ascending
Table 1 where high count to low count from table 2 and high run to low run
USER ID 3
USER ID 2
USER ID 4
USER ID 1
Using PHP AND MYSQLI
Please help me
select t1.id, count(t2.id) as t2_count
from table1 t1
left join table2 t2 on t1.`user id` = t2.`user id`
group by t1.id
order by t2_count desc
I have a table of bids (like an eBay bids, for example):
bid_id | bid_from_user | bid_for_auction | bid_price
1 | 150 | 1 | 20
2 | 453 | 1 | 30
3 | 42 | 1 | 50
4 | 12 | 2 | 12
5 | 60 | 2 | 20
I need to select each bid_for_auction only once with its highest current bid, so my result should look like this:
bid_for_auction | bid_price
1 | 50
2 | 20
How would I go about doing this?
Use GROUP BY
SELECT bid_for_auction, MAX(bid_price) AS bid_price
FROM bids GROUP BY bid_for_auction;
SELECT bid_for_auction, MAX(bid_price)
FROM bids
GROUP BY bid_for_auction
This should do the trick:
SELECT
`bid_for_auction`,
MAX(`bid_price`)
FROM `bids`
GROUP BY `bid_for_auction`
SELECT bid_for_auction,MAX(bid_price) FROM bids GROUP BY bid_for_auction