Group By Multiple Columns Separately - mysql

I know that you can use the GROUP BY keyword to group by multiple columns, but here's what I'm trying to do:
Table: Codes
user_id day_of_week id
------------------------------
1 T 1
1 W 2
1 W 3
2 F 4
2 F 5
2 M 6
And I am trying to find a command to output to get this:
Table: Codes
user_id day_of_week count(*)
-------------------------------------
1 T 1
1 W 2
2 F 2
2 M 1
So as you can see, it's grouping by the day of the week and the user_id Can someone help me achieve this with MySQL?

You can use multiple columns in GROUP BY clause:
SELECT user_id, day_of_week, COUNT(*) AS cnt
FROM Codes
GROUP BY user_id, day_of_week

Related

Trying to get latest status for related shipment but the results I receive are incorrect

I am currently working on a project while trying to learn MySQL and I would like to join three tables and get the latest status for each related shipment. Here are the tables I'm working with (with example data):
shipments
id
consignee
tracking_number
shipper
weight
import_no
1
JOHN BROWN
TBA99900000121
AMAZON
1
101
2
HELEN SMITH
TBA99900000190
AMAZON
1
102
3
JACK BLACK
TBA99900000123
AMAZON
1
103
4
JOE BROWM
TBA99900000812
AMAZON
1
104
5
JULIA KERR
TBA99900000904
AMAZON
1
105
statuses
id
name
slug
1
At Warehouse
at_warehouse
2
Ready For Pickup
ready_for_pickup
3
Delivered
delivered
shipment_status (pivot table)
id
shipment_id
status_id
1
1
1
2
2
1
3
3
1
4
4
1
5
5
1
6
1
2
7
2
2
8
3
2
9
4
2
10
5
2
all tables do have created_at and updated_at timestamp columns
Example of the results I'm trying to achieve
slug
shipment_id
status_id
ready_for_pickup
1
2
ready_for_pickup
2
2
ready_for_pickup
3
2
ready_for_pickup
4
2
ready_for_pickup
5
2
Here's the query I wrote to try to achieve what I'm looking for based on examples and research I did during the past couple of days. I find that sometimes there is sometimes a mismatch with the latest status that relates to the shipment
SELECT
statuses.slug AS slug,
MAX(shipments.id) AS shipment_id,
statuses.id AS status_id,
FROM
`shipments`
INNER JOIN `shipment_status` ON `shipment_status`.`shipment_id` = `shipments`.`id`
INNER JOIN `statuses` ON `shipment_status`.`status_id` = `statuses`.`id`
GROUP BY
`shipment_id`
Because we need to reference other fields from the same record that evaluates from the MAX aggregation, you need to do it in two steps, there are other ways, but I find this syntax simpler:
SELECT
shipments.id AS id,
statuses.slug AS slug,
statuses.id AS status_id,
shipment_status.shipment_id as shipment_id
FROM
`shipments`
INNER JOIN `shipment_status` ON `shipment_status`.`shipment_id` = `shipments`.`id`
INNER JOIN `statuses` ON `shipment_status`.`status_id` = `statuses`.`id`
WHERE
shipment_status.id = (
SELECT MAX(shipment_status.id)
FROM `shipment_status`
WHERE shipment_status.shipment_id = shipments.id
)
try it out!
This query makes the assumption that the id field is an identity column, so the MAX(shipment_status.id) represents only the most recent status for the given shipment_id
You can use window functions:
SELECT s.id, st.slug, st.id
FROM shipments s JOIN
(SELECT ss.*,
ROW_NUMBER() OVER (PARTITION BY shipment_id ORDER BY ss.id DESC) as seqnum
FROM shipment_status ss
) ss
ON ss.shipment_id = s.id JOIN
statuses st
ON ss.status_id` = st.id
WHERE ss.seqnum = 1;
Also note the use of table aliases so the query is easier to write and to read.

Sum of values from 2 columns from 2 tables AND sorting by a column

I have 2 tables:
Table "credits":
id | amount | type
1 8 1
2 7 2
3 2 1
4 1 1
5 5 3
6 4 2
and
Table "debits":
id | amount
1 3
1 2
3 2
4 1
5 3
5 1
I need to get the sum of all "id's" balances (credit-debit) and grouping it by "type". So far I have this:
SELECT id, SUM(amount) as balance,
FROM
(
SELECT id, amount FROM credits
UNION ALL
SELECT id, -amount FROM debits
)
unified_table
GROUP BY id
But it just gives me the "id's" balances:
id | balance
1 3
2 7
3 0
4 0
5 1
6 4
Ideally, I need something like this:
type | balance
1 3
2 11
3 1
I tried to add the "type" column in the first "select" of the union, and then group by "type". But not working I think because table "debits" dont have column "type". How can I accomplish this? Thank you for your help
I think this would do it:
SELECT c.type, sum(c.amount - IFNULL(d.amount,0))
FROM credits c LEFT OUTER JOIN (SELECT id, sum(amount) FROM debits GROUP BY id) d
ON c.id=d.id
GROUP BY c.type
The idea is to group the debits table first, and then join it with the credits table, which will result in a table that you can group by type
Try this:
SELECT Type, Sum(Amount)
FROM (
SELECT C.Amount - ISNULL(D.Amount, 0) AS Amount, C.Type
FROM Credits C
LEFT JOIN (SELECT Id, Sum(Amount)
FROM Debits
GROUP BY ID) D ON C.Id = D.Id
) A
GROUP BY A.Type
Here is my solution:
SELECT
credits.`type`,
credits.`amount` - IFNULL(t_debit.`d_amount`, 0) AS balance
FROM
credits,
(SELECT id, SUM(amount) AS d_amount FROM debits GROUP BY id)t_debit
WHERE
credits.`id` = t_debit.`id`
GROUP BY
credits.`type`;
First I select sum of amounts from debits table group by id and after I did another select query on the credits table where credit id match to debit id. I don't use UNION operator because the id's column in debits table is an foreign key.

mysql count and then group by that count

I have a table structured like this:
user_id saved_id
1 2
1 34
1 36
2 489
2 14
3 731
4 48
5 901
6 234
6 9
6 64
What I would like to do is first count how many saved ids each user has, and then group these results so that I know how often each total_saves occurs.
This is what I currently have:
SELECT user_id, count(*) as total_saves FROM table GROUP BY user_id ORDER BY total_saves DESC
Which gives me
user_id total_saves
1 3
6 3
2 2
3 1
4 1
5 1
What I would like to have is this:
total_saves count
3 2
2 1
1 3
Can't get my head around how to group the total_saves that I already have. I tried GROUP BY total_saves but that doesn't work.
Use two aggregations:
select total_saves, count(*) as cnt
from (select user_id, count(*) as total_saves
from t
group by user_id
) t
group by total_saves;
Use Subquery
select total_saves, count(total_saves) as count
from (select user_id, count(*) as total_saves
from table
group by user_id
) a
group by total_saves order by total_saves;

Sum values in mysql table where userid is identical

I have read the different answers here on SO, but I am stuck on this question. Please help.
I have this mysql view named "activeuser":
userid COUNT(*) ACRONYM
1 23 admin
2 2 doe
3 4 tompa
12 4 Marre
13 1 Mia
1 2 admin
3 1 tompa
12 1 Marre
13 1 Mia
2 1 doe
3 1 tompa
12 1 Marre
How can I sum the COUNT column so that I get the following wanted result?
userid COUNT(*) ACRONYM
1 25 admin
2 3 doe
3 6 tompa
12 6 Marre
13 1 Mia
EDITED:
I used this query to create the view:
CREATE VIEW activeuser AS
(SELECT boats_comments.userid, COUNT(boats_comments.userid), boats_user.acronym, boats_user.email
FROM boats_comments
INNER JOIN boats_user
ON boats_comments.userid = boats_user.id
GROUP BY boats_comments.userid
ORDER BY COUNT(boats_comments.userid) DESC)
UNION ALL
(SELECT boats_answers.userid, COUNT(boats_answers.userid), boats_user.acronym, boats_user.email
FROM boats_answers
INNER JOIN boats_user
ON boats_answers.userid = boats_user.id
GROUP BY boats_answers.userid
ORDER BY COUNT(boats_answers.userid) DESC)
UNION ALL
(SELECT boats_questions.userid, COUNT(boats_questions.userid), boats_user.acronym, boats_user.email
FROM boats_questions
INNER JOIN boats_user
ON boats_questions.userid = boats_user.id
GROUP BY boats_questions.userid
ORDER BY COUNT(boats_questions.userid) DESC)
My goal is to see which users are the most active by checking the number of comments, questions and answers... but I got stuck...
As the results in your view has duplicates I guess the underlying code for the view is grouping on something it maybe shouldn't be grouping on.
You can get the results you want by applying SUM to it:
select userid, sum("whatever column2 is named") as "Count", Acronym
from activeuser group by userid, Acronym;
select userid, count(*) from activeuser group by userid;

Access Totals Query Not Necessarily Returning First Record

I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.