Initially I have a data sheet like this
What I want is that after using the SQL the SQL will produce a table like this
I have just learned SQL, have tried count () and group by but not yet.
The TOTAL column will be counted based on the SIZE and NAME columns.
First you want to format your date accordingly using DATE_FORMAT(date, "%Y-%m") to get the format 2021-03. Then you can use GROUP BY along with COUNT(*).
SELECT
name,
size,
DATE_FORMAT(date, "%Y-%m") as month,
COUNT(*) as Total
FROM test
GROUP BY
name,
size,
DATE_FORMAT(date, "%Y-%m")
ORDER BY
COUNT(*) DESC,
name ASC;
name
size
month
Total
A
1
2021-03
2
B
2
2021-03
2
A
2
2021-03
1
C
3
2021-03
1
Working example at https://www.db-fiddle.com/f/7edM78Qhp8wLZ1JJZoE3bH/1
It's not clear what order you want the results returned in, so you can change the ORDER BY clause to customize it how you want.
select Name,Size,Cast(date as date) as Date,count(1) from tablename
group by Name,Size,Cast(date as date)
Related
I have a clients mysql table and I´d like to get a cumulatie count of clients grouped by month-year.
I have tried the next, but no look:
SELECT DATE_FORMAT(datacad,'%m-%y') AS month-year, count(id) OVER(ORDER BY id) AS cumulative_count
FROM clients;
clients
-------
id datacad
1 2001-10-10
2 2001-10-11
3 2002-11-12
4 2001-12-14
5 2003-12-15
6 2003-12-16
7 2003-12-17
//required result
month-year cumulative_count
----------------------------
10-2001 3
11-2002 4
12-2003 10
Additional Info:
When I use:
SELECT DATE_FORMAT(datacad,'%m-%y') AS label, count(*) as total FROM clients
GROUP BY label
order by datacad
I get:
label,total
03-2011,1
04-2011,1268
05-2011,1365
06-2011,1244
07-2011,1102
08-2011,315
02-2013,1
03-2013,1
03-2014,1
06-2014,1
07-2014,1
01-2017,1
02-2017,1
01-2018,4
05-2018,2
08-2018,1
09-2019,1
04-2020,3
06-2020,1
But when I use:
SELECT DATE_FORMAT(datacad,'%m-%y') AS month_year,
SUM(COUNT(id)) OVER (ORDER BY id) AS cumulative_count
FROM clients
GROUP BY month_year
ORDER BY MIN(datacad);
I get:
month_year,cumulative_count
03-11,1271
04-11,1268
05-11,2636
06-11,3880
07-11,4982
08-11,5297
02-13,5298
03-13,5299
03-14,5300
06-14,5301
07-14,5302
01-17,5303
02-17,5304
01-18,5308
05-18,1270
08-18,5309
09-19,5310
04-20,5313
06-20,5314
The results from both don´t match.
You need a GROUP BY and to SUM() the counts for a cumulative sum:
SELECT DATE_FORMAT(datacad,'%m-%y') AS month_year,
SUM(COUNT(id)) OVER (ORDER BY MIN(datacad)) AS cumulative_count
FROM clients
GROUP BY month_year
ORDER BY MIN(datacad);
I also assume you want the data in date order, so I added an ORDER BY.
I have 2 tables.ms_expese and track_expense.Using this table generate a fact table
I want the expense_name in ms_expense,expense_amount from track_expense.
I want to get the sum of expense_amount for a particular expense_name based on date.The date in the order of 1,2...12 as month id
SELECT DATE_Format(a.date,'%b') as month_id,b.expense_name AS expense_type, sum(a.expense_amount) AS expense_amount FROM ms_expense b JOIN track_expense a on a.`expense_id`=b.`expense_id` group by DATE_Format(a.date,'%b')
how to put the month id in the order of 1,2,..12 and my date format y-m-d
I get the month in apr,aug and so on but i need jan as 1,feb as 2
I have 25 expenses(expense name).In this query i got the total expense amount of first expense only.I want the total expense of all expenses in every month
CREATE TABLE fact AS
(<your select query>)
Your select query can be in the following form
SELECT MONTH(date)as month_id,expense_name,sum(expense_amount)
FROM ms_expense JOIN track_expense using (expense_id)
group by expense_name,MONTH(date)
I have a table with 2 dates in it and a product, and I need to get the average days difference between them considering just the last 3 rows for each product.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121
This gives me the average of all the date differences for product 121
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121 LIMIT 3
Still gives me the average off all the records, ignoring the LIMIT argument.
Also when I try a different approach, it also does ignore the last argument and shows the average off all the rows.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product =121 && date1 > 2015-01-01
Any idea on how to fix this or what I'm doing wrong?
When you have problems like this, I recommend breaking it up and putting it back.
Before doing any calculations, you know that you need the last three rows for each product. So, if you want for example the rows with the latest date2 you can select them by doing the following:
SELECT *
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3;
That will select the 3 latest rows you want. Then, just use that as a subquery to preform the aggregation. This way, the calculations are only made on the rows you are concerned with:
SELECT product, AVG(DATEDIFF(date2, date1))
FROM(
SELECT product, date1, date2
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3) tempTable;
Hi i am trying to get the count of records per day which i can do, but i also want the date to be show, for example,
Result
Date | Count
26/01/2015 20
25/01/2015 | 413
Here is an example of my data.
I would think this would work. Replace 'yourTable' with your table name
SELECT Date, COUNT(*) FROM yourTable GROUP BY Date;
Get the total count and group them by date.
SELECT `date`, COUNT(*) as Total
FROM `table`
GROUP BY `date`
ORDER BY `date`;
This is what I am trying to do:
Get the sum of all rows grouping by date
Get all the rows individually
I have successfully done this... The problem is I want the order to change overall
Here is my table columns
[id][date][amount]
Example being:
1, 2013-10-01, 102.10
2, 2013-10-01, 256.15
3, ...
The output I get in Laymen's terms,
the first x of rows are the totals grouped by date
the rest of the rows are the individual amounts
The output I want in laymen's terms,
The first row is the total of the following rows by date, then rinse and repeat.
i.e.
2013-10-01, 200.00 <-- Total
2013-10-01, 150.00
2013-10-01, 50.00
2013-10-02, 300.00 <-- Total
2013-10-02, 150.00
2013-10-02, 150.00
Here is my query:
SELECT
t.date,
round(sum(t.amount), 2) as total
FROM invoice t
GROUP BY t.date
UNION ALL
SELECT
t.date,
round(t.amount, 2)
FROM invoice t;
See my example at SQLFiddle!
Thanks in advance for any assistance on this.
Assuming that you mean you want to change the order of your results by date (and I typically don't make such an assumption), you would simply need to add an "ORDER BY" to your sql. The below should do the trick.
SELECT
t.date,
round(sum(t.amount), 2) as total
FROM invoice t
GROUP BY t.date
UNION ALL
SELECT
t.date,
round(t.amount, 2)
FROM invoice t
ORDER BY date
Given your edits, it appears you want to sort by date, and then total amount descending:
SELECT
date,
round(sum(amount), 2) as total
FROM invoice
GROUP BY date
UNION ALL
SELECT
date,
round(amount, 2)
FROM invoice
ORDER BY date, total DESC
Updated Fiddle Demo