Is there any way by which i can SUM(value) of ColA (ColA is repeating in each ROW) based on DISTINCT(value) of ColB?
My Data Table is:
+-------+-------+------+--------+
| State | KEY_ID| Score| STATUS |
+-------+-------+------+--------+
| ABCD | 11 | 20 | 1 |
| ABCD | 11 | 30 | 1 |
| ABCD | 13 | 40 | 1 |
| DEFG | 21 | 10 | 1 |
| DEFG | 21 | 25 | 1 |
| DEFG | 23 | 15 | 1 |
+-------+-------+------+--------+
& wanted output like :
+-------+-------+------+--------+
| State | KEY_ID| Score| STATUS |
+-------+-------+------+--------+
| ABCD | 11 | 50 | 1 |
| ABCD | 13 | 40 | 1 |
| DEFG | 21 | 35 | 1 |
| DEFG | 23 | 15 | 1 |
+-------+-------+------+--------+
but not want to use GROUP BY function & OVER() in not working in my MySQL version 8.0.28
You want a simple GROUP BY query to aggregate your scores. fiddle.
SELECT State, KEY_ID,
SUM(Score) Score,
STATUS
FROM mytable
GROUP BY State, KEY_ID, STATUS
You can use GROUP_BY (https://www.w3schools.com/sql/sql_groupby.asp) on KEY_ID
like this query
SELECT State, KEY_ID, SUM(Score), STATUS FROM data_table GROUP BY KEY_ID;
Related
I have the following data table from which I would like to sum the values of the field 'pts' for each 'pid' as follows:
The sum of the top 3 values per 'cont' plus the values of any other 'cont' per 'pid'. The results should be presented in DESC order by 'total'
+--------+-----+------+
| pid | pts | cont |
+--------+-----+------+
| 121693 | 40 | 1 |
| 121693 | 80 | 2 |
| 121693 | 120 | 1 |
| 121693 | 100 | 1 |
| 121693 | 500 | 1 |
| 121694 | 20 | 1 |
| 121694 | 0 | 2 |
| 121694 | 30 | 3 |
| 121695 | 0 | 1 |
| 121695 | 30 | 2 |
| 121695 | 0 | 1 |
+--------+-----+------+
In this example the query should return something like this
+--------+-------+
| pid | total |
+--------+-------+
| 121693 | 800 |
| 121694 | 50 |
| 121695 | 30 |
+--------+-------+
Is this possible?
Thanks in advance.
SELECT DISTINCT pid, SUM(Pts) AS Total
FROM your tablename
GROUP BY Pid
ORDER BY TOTAL
(Requires testing and minor fixes on small syntax)
I have a table named travel which is as follows:
+-------+-----+----------+
| Name | Day | Distance |
+-------+-----+----------+
| Ravi | 1 | 10 |
| Ravi | 2 | 21 |
| Ravi | 3 | 23 |
| Hari | 1 | 12 |
| Hari | 2 | 32 |
| Kiran | 1 | 12 |
| Kiran | 2 | 32 |
| Kiran | 3 | 21 |
| Kiran | 4 | 43 |
+-------+-----+----------+
using group in sql for this table as
select name, day, distance, sum(distance) as total_dist
from travel
group by name;
I get the result as follows
+-------+-----+----------+------------+
| Name | Day | Distance | total_dist |
+-------+-----+----------+------------+
| Ravi | 1 | 10 | 54 |
| Hari | 1 | 12 | 44 |
| Kiran | 1 | 12 | 108 |
+-------+-----+----------+------------+
That is mysql gives the top row by default for columns which are not aggregated. But I would prefer to get all the rows with aggregated values or rows with bottom row in the group like as follows:
+-------+-----+----------+------------+
| Name | Day | Distance | total_dist |
+-------+-----+----------+------------+
| Ravi | 3 | 23 | 54 |
| Hari | 2 | 32 | 44 |
| Kiran | 4 | 43 | 108 |
+-------+-----+----------+------------+
or either in this way:
+-------+-----+----------+------------+
| Name | Day | Distance | total_dist |
+-------+-----+----------+------------+
| Ravi | 1 | 10 | 54 |
| Ravi | 2 | 21 | 54 |
| Ravi | 3 | 23 | 54 |
| Hari | 1 | 12 | 44 |
| Hari | 2 | 32 | 44 |
| Kiran | 1 | 12 | 108 |
| Kiran | 2 | 32 | 108 |
| Kiran | 3 | 21 | 108 |
| Kiran | 4 | 43 | 108 |
+-------+-----+----------+------------+
I am new to using mysql and confused with using group by, please guide me if any of the above two results are possible to get.
To get the entire list with the sum for each group, you can use a subquery and join the total back to the original table:
select t.name, t.day, t.distance, t2.total_dist
from travel t
join (select name, sum(distance) total_dist
from travel
group by name) t2 on t.name = t2.name
SQL Fiddle Demo
Or to get the max day per group, just include max(day) in the subquery:
select t.name, t.day, t.distance, t2.total_dist
from travel t
join (select name, sum(distance) total_dist, max(day) max_day
from travel
group by name) t2 on t.name = t2.name and t.day = t2.max_day
More Fiddle
it will work for you
select name,max(day),max(distance)
from tablename
group by name
and other case
select name,max(day),max(distance),(select sum(distance) from table) as total_dist
from tablename
group by name
For each week I want to select the rows which have the highest weight for each distinct value of reps
-------------------------------------
| id | reps | weight | date |
| 1 | 1 | 15 | 2015-06-10 |
| 2 | 2 | 29 | 2015-06-12 |
| 3 | 1 | 30 | 2015-06-13 |
| 4 | 4 | 11 | 2015-06-14 |
| 5 | 1 | 15 | 2015-06-29 |
| 6 | 1 | 9 | 2015-06-30 |
and I would like it to return
-------------------------------------
| id | reps | weight | date |
| 2 | 2 | 29 | 2015-06-12 |
| 3 | 1 | 30 | 2015-06-13 |
| 4 | 4 | 11 | 2015-06-14 |
| 5 | 1 | 15 | 2015-06-29 |
I've tried
SELECT MAX(weight) as weight, reps, date, id FROM log_items
GROUP BY reps, WEEK(date)
But it seems to return completely random results, I have also tried using sub queries but they didn't work either (I'm guessing I was doing it wrong)
Try:
SELECT *
FROM log_items
WHERE (reps, WEEK(date), weight ) in (
SELECT reps, WEEK(date) , MAX(weight)
FROM log_items
GROUP BY reps, WEEK(date)
)
demo: http://sqlfiddle.com/#!9/4bf84/9
I have a table that has some values in it, along with the time that value was taken against an associated ID from another table.
I am looking to retrieve the latest value for every item in that table, and then order by those latest values.
Here is an SQL fiddle, http://www.sqlfiddle.com/#!2/0be99
And here is text output.
'hist' table
| HIST_ID | HIST_ITEM_ID | HIST_VALUE | HIST_TIME |
|---------|--------------|------------|------------|
| 1 | 1 | 1 | 1420291000 |
| 2 | 1 | 2 | 1420292000 |
| 3 | 1 | 3 | 1420293000 |
| 4 | 1 | 5 | 1420294000 |
| 5 | 1 | 10 | 1420295000 |
| 6 | 1 | 50 | 1420296000 |
| 7 | 1 | 60 | 1420297000 |
| 8 | 1 | 77 | 1420298000 |
| 9 | 1 | 90 | 1420299000 |
| 10 | 1 | 101 | 1420300000 |
| 11 | 2 | 1 | 1420291000 |
| 12 | 2 | 3 | 1420292000 |
| 13 | 2 | 7 | 1420293000 |
| 14 | 2 | 9 | 1420294000 |
| 15 | 2 | 15 | 1420295000 |
| 16 | 2 | 21 | 1420296000 |
| 17 | 2 | 33 | 1420297000 |
| 18 | 2 | 35 | 1420298000 |
| 19 | 2 | 55 | 1420299000 |
| 20 | 2 | 91 | 1420300000 |
'items' table
| ITEM_ID | ITEM_TITLE |
|---------|------------|
| 1 | ABCD |
| 2 | XYZ123 |
So, I can do something like...
select * from hist
inner join items on hist_item_id = item_id
group by hist_item_id
order by hist_value desc
However this returns me a grouping that I cannot order. How can I order this grouping? I had a look at other similar questions on here but was unable to apply their solutions successfully to my query to produce the desire result.
The desired result here would be to return.
HIST_ITEM_ID | ITEM_TITLE | HIST_VALUE |
|------------|------------|------------|
| 1 | ABCD | 101 |
| 2 | XYZ123 | 91 |
You can use a join to get the most recent history item. Then you can join back to the history table and the item table to get additional information:
select h.*, i.item_title
from (select hist_item_id, max(hist_id) as max_hist_id
from hist
group by hist_item_id
) hh join
hist h
on h.hist_id = hh.max_hist_id join
items i
on i.item_id = hh.hist_item_id;
Here is a SQL Fiddle.
You should use MAX function and group by the item id. That would look like this:
SELECT i.item_id, i.item_title, MAX(h.hist_value)
FROM items AS i
INNER JOIN hist AS h
ON i.item_id = h.hist_item_id
GROUP BY i.item_id
i have a table called rc_language_type_table with:
id language
1 english
2 Xhosa
3 afrikaans
etc
then i have a table rc_language_type_assoc_table with:
profile_id | language_type_id |
+------------+------------------+
| 3 | 1 |
| 13 | 1 |
| 15 | 1 |
| 16 | 1 |
where i have profiles and each profile is connected to a language id in a 1 to many
so then i did:
select *,count(*) from rc_language_type_assoc_table group by language_type_id;
+------------+------------------+----------+
| profile_id | language_type_id | count(*) |
+------------+------------------+----------+
| 3 | 1 | 96 |
| 3 | 2 | 19 |
| 3 | 3 | 18 |
| 64 | 4 | 51 |
| 94 | 5 | 10 |
| 37 | 6 | 26 |
| 3 | 7 | 21 |
| 3 | 8 | 4 |
| 3 | 9 | 6 |
| 88 | 10 | 4 |
| 3 | 11 | 3 |
+------------+------------------+----------+
what i want now is: instead having the language_type_id i want to display the actual language...how would i do this please???
i tried:
select *, count(*)
from rc_language_type_assoc_table, rc_language_type_table
group by language_type_id
where rc_language_type_assoc_table.language_type_id = rc_language_type_table.id;
but i get a syntax error...
please help??
thank you
GROUP BY should be "after" the WHERE statement and not before
select *, count(*)
from rc_language_type_assoc_table, rc_language_type_table
where rc_language_type_assoc_table.language_type_id = rc_language_type_table.id
group by language_type_id ;