SQL: How do I add specific rows together? - mysql

How I would add 2 specific rows in an SQL table together?
For example:
ID Quantity
1 1
1 3
1 2
2 3
2 4
I want to be able to add together ID 1 and 2 so the table becomes
ID Quantity
1 6
2 7

You need to use the SUM aggregate function and GROUP BY the ID.
SELECT Id, SUM(Quantity)
FROM yourtable
GROUP BY ID
Output:
ID Quantity
1 6
2 7
SQL Fiddle:
Further Reading.

Related

SQL return latest data from each date

Sample table
id
id_sequence
date
1
1
2022-06-27
2
1
2022-06-27
3
1
2022-06-27
4
2
2022-06-27
5
2
2022-06-27
6
1
2022-06-28
7
1
2022-06-28
8
2
2022-06-28
9
2
2022-06-28
Expected Output
id
id_sequence
date
3
1
2022-06-27
5
2
2022-06-27
7
1
2022-06-28
9
2
2022-06-28
how can I make a query to get latest data on every date in MySql. tried to use MAX(id) for the id_sequence but it does not return a correct value since the expected output will take only highest id of every sequence and the output will only display distinct data of id_sequence 1,2 at date 2022-06-28.
If you want to make sure dates are taken distinctively, you need to add it inside the GROUP BY clause.
SELECT MAX(id) AS id,
id_sequence,
date_
FROM tab
GROUP BY id_sequence,
date_
ORDER BY id
Check the demo here.

Order by subgroup

I need help building a MySQL query that will output all rows in a certain order. The table definition contains id, sub_id, and name. The sub_id column is used to indicate that a given row should be grouped within id, and if sub_id is blank/0/null, then the row is a top-level row.
The desired sort order is by name, except I want all sub_id rows to be grouped within their corresponding id row. Within each subgroup, each row should be sorted by name.
For example, given this unordered table:
id sub_id name
=====================
1 0 bananas
2 0 apples
3 0 apricots
4 2 strawberries
5 2 cherries
6 1 oranges
The desired order is:
id sub_id name
=====================
2 0 apples
5 2 cherries
4 2 strawberries
3 0 apricots
1 0 bananas
6 1 oranges
(spacing between subgroups for illustrative purposes only)
Help? Thanks in advance!
This should do it:
SELECT t.id, t.sub_id, t.name
FROM table t LEFT JOIN table p on t.sub_id = p.id
ORDER BY COALESCE(p.id, t.id), t.name

MySQL Count and Group By two distinct columns

I have a table recommendation with fields recommender, attractionid
I want to count the how many attractionid existed in the table group by the attractionid but if there are same pairs of recommender and attractionid they are counted as 1.
For example,
attractionid recommender
1 1
1 2
1 1
2 3
2 1
2 2
2 2
2 2
expected result :
attractionid count
1 2
2 3
the rows below should be counted as 1
attractionid recommender
1 1
1 1
2 2
2 2
2 2
Use distinct attractionid,recommender inside count function.
Query
select attractionid,
count(distinct attractionid,recommender) as `count`
from recommendation
group by attractionid;
Try:
select attractionid, count(recommender) cnt
from (
select distinct attractionid, recommender
from recommendation
) x
group by attractionid

how to write the sql

i have a table like
ID USER_ID type
1 1 2
2 1 1
3 3 3
4 3 1
5 6 2
6 6 3
and i want to get the sum of all user_id = 1 and user_id =3
and every type sum in user_id = 1 and user_id =3 ,it can be two sql
the result is
sum
4
type sum
1 2
2 1
3 1
The first function you needed to achieve the result is Count() not SUM() function
For first result
select count(*) as sum from user1 where user_id in(1,3)
For second result you need to select type also and it should be grouped
select type, count(*) as sum from user1 where user_id in(1,3)
group by type
Fiddle for second you can check the first one also there by taking the query
Next time post your effort. Guess you are naive to sql.

mysql get the columns sum and also get the distinct values at a time

I have my data base like this
id project_id client_id price
1 1 1 200
2 2 1 123
3 2 1 100
4 1 1 87
5 1 1 143
6 1 1 100
7 3 3 123
8 3 3 99
9 4 3 86
10 4 3 43
11 4 3 145
12 4 3 155
Now here I want that it will sum the price columns with the same client_id.
For that I just made my query like this
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`
This one is doing sum the price but I am getting only two project_id in the result. I want the result should be all the distinct project for the client id and the price will be summed for the group clients.
So can someone tell me how to do this? Any help and suggestions will be really appreciable. Thanks
You should not have "bare" column in a group by query that are not in the group by statement.
If you want the list of projects, you can get them in a list like this:
SELECT client_id, GROUP_CONCAT(project_id), SUM(price)
FROM table-name
GROUP BY client_id;
you only have two client that why you are getting only two record , you can group by two column,
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`, `project_id`