I have a table - accounts, and it has 3 columns: id , timestamp and value.
I need to create a query that returns a table which in each row it will be the month (2, 3, etc.) and the sum of the values from this month.
for example: if my table will have 3 rows from January and one row from February, the query will return a two-rows table within the first row it'll be 1 and the sum of January's values, and the second will be 2 and the sum of February's values.
I have no idea how to begin. can anyone help me?
all you need to do is sum the value and also use mysql's MONTH() function to pull out the month from your timestamp
SELECT SUM(value) as total_amount, MONTH(timestamp) as month_num
FROM table
GROUP BY month_num
GROUP BY is used when you have an aggregate function (in our case its SUM) to know how to group your common fields. without a group by you will have all rows summed together
SQL Server can use the DATEPART function.
SELECT DATEPART(MONTH, your_date_column) AS month_
, SUM(your_value) AS value_
FROM your_table
GROUP BY DATEPART(MONTH, your_date_column)
Related
I have a date/time field in my table called startTime.
I would like the output as follows:
select
YEAR(startTime),
MONTH(startTime),
DAY(startTime),
dayofmonth(startTime),
startTime,
...
This is fine, and I only have to group by startTime.
However, for my output, I am only really interested in the date part of the startTime.
So I changed my output to be
select
YEAR(startTime),
MONTH(startTime),
DAY(startTime),
dayofmonth(startTime),
DATE(startTime),
...
When I try to run this, SQL makes me group by Year, Month, day, dayofmonth and date(startTime).
This seems to be a quirk of the date() function?
I thought maybe it's due to the time part of the startTime field, but Year, Month, Day and dayofmonth are no more granular than a date so I am confused as why I have to group by those.
Any insights greatly appreciated!
My code currently:
YEAR(startTime),
MONTH(startTime),
DAY(startTime),
dayofmonth(startTime),
date(startTime),
count(id)
from
bookings
group by 1, 2, 3, 4, 5
in your select statement we must have distinct values of selected column (as a group) for every distinct value given value of what we group them by.
For example if you have startime in your select clause, and also in group by clause, we get distinct values of selected attributes - date, month, year, & startTime for every unique value of starttime. But if you remove starttime from select clause, the attributes selected are no longer guaranteed to be unique.
Consider 2 startTime values 2020-03-12T01:01:01.000UTC and 2020-03-12T02:02:02.000UTC its expected to produce two rows if we group by startTime (as two distinct startTimes), but the selected values for year, month and date would be same in both of these rows (as they differ only in time part) which is breaking the group by contract.
Hence we can only have group of attributes in select clause which MUST provide a different value for every combination of attributes in the group by clause.
I have a table student with a column dte_date (date) having values (2019-01-01,2019-02-01,2019-03-01..etc)
Conditions:
No repeated values in the column dte_date.
But there is a chance of missing values in dte_date(example miss 2019-02-01).
The day of the date field should be 01.
I want a query to check whether any month date is missing from this table.
You can use aggregating and having:
select student_id
from t
group by student_id
having max(dte_date) <> min(dte_date) + interval count(*) - 1 month;
Note that this assumes that you don't have duplicates in the table -- although that could be handled with count(distinct).
I am trying to fetch the count of records entered in each month of the financial year
For example, I have declared a column called issue in varchar because the data what I am taking is issues of the particular machine. And for example, let's say one issue is raised in July month I enter the data as 'Jul 19-1' and the again issue is raised in the month of September again I go back to the issue happened in July and enter the data as 'sep19-2'.
So in the backend, it takes as jul19-1 sep19-2
What can be the query that I can write for counting the number of issues raised in each month
I tried the below query but
SELECT COUNT(month_nc)
FROM `ncr`
WHERE month_nc='Jul18-1'
In some months there will be only one issue so I can the count of the month given in the above query
What will be the query if I want to fetch the count of each month
id issue issue_month
1 bearing jul18-1
sep18-2
2 motor jul18-2
3 battery apr18-3
ps: issue_month is declared in varchar(10)
Here are two methods. One using strings:
select left(issue_month, 5), count(*)
from t
group by left(issue_month, 5), count(*)
This will not order the values correctly.
You can convert to a date to order properly:
order by str_to_date(concat('01', left(issue_month, 5)), '%d%b%y')
Or, represent the dates correctly:
select str_to_date(concat('01', left(issue_month, 5)), '%d%b%y') as yyyymm, count(*)
from t
group by yyyymm
order by yyyymm;
Here is what you can do to split your issue_month into "month_year" and "issue_count"
yourTable
select id,
issue,
issue_month,
REGEXP_SUBSTR(issue_month, '[^-]+', 1) as month_year,
REGEXP_SUBSTR(issue_month, '[^-]+', 1,2 ) as issue_count
from yourTable;
Now you can aggregate the issue_count across issues or year_months or any other field in your table.
For example, to get the sum of all the issues for any given month_year
select
month_year,
sum(issue_count) issue_count
from
(select
id, issue, issue_month,
REGEXP_SUBSTR(issue_month, '[^-]+', 1) as month_year,
REGEXP_SUBSTR(issue_month, '[^-]+', 1,2 ) as issue_count
from yourTable) foo
group by month_year;
I have the next table named expenses_partial_payment.
My query is the next, i need the sum of amount_paid as total_paid:
SELECT id_partial_payment, epp.id_billing, epp.id_user, epp.month_of_payment,
COALESCE(SUM(epp.amount_paid),0) AS total_paid,
(be.total - amount_paid) AS pending_total , be.total as total
FROM expenses_partial_payment epp
INNER JOIN BillingExpenses be ON epp.id_billing = be.id_billing
WHERE epp.id_user = 23
but when i add in the clause where
WHERE epp.id_user = 23 AND (Month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01'))
I have an error with the total_piad, the query returns only the last row (300) and don't sum the amount_paid , the correct sum must be 501.00
What can i do to get the correct sum adding the clause where to filter the month_of_payment
(month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01'))
month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01')
last day() returns the last day of the Month not year. So it will not pull up data in the month greater than January.
Try to replace your date with this
STR_TO_DATE('01/01/2015', '%m/%d/%Y')
I have a table "Report" with relevant columns "Date", "Doctor". Each doctor appears several times throughout the table. The following code is what I have at current:
SET #variable = (SELECT Date FROM Report WHERE Doctor='DocName' ORDER BY Date DESC LIMIT 1)
SELECT DATEDIFF(CURDATE(),#variable) AS DiffDate
This gives me the DATEDIFF for one doctor, without name. Is there any way to loop through the table, find the last row/date for each doctor, then perform a DATEDIFF on each individual doctor outputting a list of doctors with their DATEDIFFs (against current date) next to them?
Thanks in advance!
you can use group by to get only 1 row per doctor and max to select latest date:
select `Doctor`, DATEDIFF(CURDATE(),max(`Date`))
from `Report`
group by `Doctor`