I'm trying to get my code to output in the following format:
january 2012 - 34
february 2012 - 23
where 34 and 23 would be a count of the total rows that fall within that month that have the id_dealership of 7. I need this to output all data for every month that an assignment was ever made.
The assignments table structure is as follows:
id_dealer (int)
date_assigned (int)
I've tried this but it does not work at all:
SELECT MONTH(date_assigned), YEAR(date_assigned), COUNT(*)
FROM assignments
GROUP BY MONTH(date_assigned), YEAR(date_assigned)
SELECT
MONTH(FROM_UNIXTIME(date_assigned)),
YEAR(FROM_UNIXTIME(date_assigned)),
COUNT(*)
FROM assignments
GROUP BY
MONTH(FROM_UNIXTIME(date_assigned)),
YEAR(FROM_UNIXTIME(date_assigned))
Your date_assigned column should be of type DATE. AFAIK MONTH works on date columns
and if you want the month name from a DATE column use : MONTHNAME(date_assigned)
try this query
SELECT
MONTH(FROM_UNIXTIME(date_assigned)),
YEAR(FROM_UNIXTIME(date_assigned)),
COUNT(*)
FROM assignments
GROUP BY 1,2
For people who would like to output a DATETIME rather than a month/year combo, here's another way to solve the problem. The benefit of using DATETIME is that it can easily be plugged into data visualization libraries and tools.
SELECT
LAST_DAY(FROM_UNIXTIME(date_assigned)),
COUNT(*)
FROM assignments
GROUP BY 1
ORDER BY 1 DESC
The LAST_DAY() function returns the last day of the month for a given DATE or DATETIME value. If you'd rather grab the first day, you could select this instead: ADDDATE(LAST_DAY(SUBDATE(FROM_UNIXTIME(date_assigned), INTERVAL 1 MONTH)), 1). It adds a day to the last date then subtracts a month.
The 1 values are column position integers -- shorthand so we don't have to type LAST_DAY(FROM_UNIXTIME(date_assigned)) any more than we need to (they start at 1, not 0).
Example output:
|-------------------------------------------|------------------|
| LAST_DAY(FROM_UNIXTIME(date_assigned)) | COUNT(*) |
|-------------------------------------------|------------------|
| September 30, 2020, 12:00 AM | 34 |
|-------------------------------------------|------------------|
| August 31, 2020, 12:00 AM | 23 |
|-------------------------------------------|------------------|
Related
I have an inventory database, on the database I'm storing multiple Items with the date that the item was created.
The date is in epoch time.
What I need to do is to create a report per fiscal year - in this case from sept 1, 2020 to Aug 30, 2021, it is possible to do this query if I have the time on epoch or it easier if I store the date on a different format.
id | name | status | time
1 | pens | active | 1636395754
It's possible using FROM_UNIXTIME function. Example query:
SELECT *,
FROM_UNIXTIME(time)
FROM mytable
WHERE FROM_UNIXTIME(time) >= '2020-09-01'
AND FROM_UNIXTIME(time) <= '2021-08-31';
If you want to add a new time column in UTC format based on the existing time column, you can do like this (provided that you have the access privilege):
ALTER TABLE mytable ADD utctime DATETIME;
Then UPDATE the new column:
UPDATE mytable SET utctime=FROM_UNIXTIME(time);
Demo fiddle
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
We can see that some functions do not like incomplete dates:
Some date functions can be used with “zero” dates or incomplete dates such as '2001-11-00', whereas others cannot.
I want to subtract two dates like this:
SELECT DATEDIFF(CURRENT_DATE(), birthdate)
birthdate is a DATE column according to the phpMyAdmin console.
But some of the birth dates don't have the month or day, example '1874-00-00'. You cannot get the days difference because the days are not known.
Is there a way to tell it to assume Jan 1 or something? Or would I need to resort to some building the date up myself so it recognizes it?
I really just need the approximate number of years difference. But I get NULL.
Thanks.
Use only YEAR from your field & do simple subtraction:
SELECT YEAR(NOW())-YEAR(birthdate) FROM YOUR_TABLE_NAME;
It will work both for valid & invalid date
If you have dates like '1874-00-00' then the data type of the column is VARCHAR unless you have disabled NO_ZERO_IN_DATE mode which is enabled by default.
I really just need the approximate number of years difference
If the year is always included in your dates and it is 4 digits at the start then all you need is to subtract your date form the current year:
SELECT birthdate,
YEAR(NOW()) - birthdate AS age
FROM tablename
MySql will do implicit conversion of the birthdate to an integer which will be just the year.
For this sample data:
CREATE TABLE tablename(birthdate VARCHAR(20));
INSERT INTO tablename(birthdate) VALUES
('1950-05-15'), ('1960-08-00'), ('1970-00-00');
the result will be:
| birthdate | age |
| ---------- | --- |
| 1950-05-15 | 70 |
| 1960-08-00 | 60 |
| 1970-00-00 | 50 |
Or if you want to be more precise when the birthdates are valid dates:
SELECT birthdate,
COALESCE(
TIMESTAMPDIFF(YEAR, birthdate, NOW()),
YEAR(NOW()) - birthdate
) age
FROM tablename
See the demo.
i have table for structure like this
empid fromdate todate office_ID
1 2012-03-01 2014-04-2 1
1 2014-04-03 ---- 2
2 2012-03-01 2014-04-2 2
3 2012-03-01 2014-04-2 1
from this table i want to fetch that at some month
for ex. for may 205 where was employee 1
for that i have designed query
select* from tbl_emp_office where (year(From_date)*100+ month(From_date)) < '201505'
but it is not giving correct answer for all users
if employee currently working at some office then todate will be not known hence it is null (this logic can be changed if you suggest some better )
If I understand you correctly, you want this:
select office_id
from employee_history
where year(fromdate) <= 2015
and month(fromdate) <= 5
and (
(month(todate) >= 5 and year(todate) >= 2015)
or todate is null
)
and empid = 1;
The query uses mysql functions to break the dates into year and month, and then makes sure that the month and year you are looking for is equal to or after their startdate, and that their todate is either null, or occurs after the month/year combo you are looking for.
This should also include offices they were in for a partial month.
I have a mysql database with a table in it. This table consists of the some of the following information. It has values in one column with months Jan-May. So five months. On the adjacent column, there are "Counts" with integer values to each month. Bear in mind that there can be duplicate values of the months. So, for example, a snippet of the table could read
January | 5
January | 10
February | 1
March | 20
April | 23
April | 34
April | 43
May | 9
There are a lot more records (160). Say the average of the month is running some sql command like
select month, avg(count) from tablename group by month. However, this divides the sum of counts for each month by the number of records. A true average would divide the sum of the counts by the number of days in each month. So I have the following statements,
select month, sum(count)/31 from trendsummary.traffictype where month like 'January';
select month, sum(count)/28 from trendsummary.traffictype where month like 'February';
select month, sum(count)/31 from trendsummary.traffictype where month like 'March';
select month, sum(count)/30 from trendsummary.traffictype where month like 'April';
select month, sum(count)/31 from trendsummary.traffictype where month like 'May';
This gives me the averages for the counts for each month. So the question is...what would be the syntax if I wanted an average of the averages of Jan-April? So... I want to have statements that would take the averages (based on the number of days of the month) for each of the months, and then take the average of the averages for January, February, March, And April and spit that value out? How would one go about this? Thanks!
you can try that :
select month, sum(count)/(31+28+31+30)
from trendsummary.traffictype
where month in ( 'January' , 'February','March','April' );
Union the selects and enclose them in parentheses and treat that as you data source as in this example:
select avg(*) from (
select month, sum(count)/31 as average from ...
union select ...
union select ...
)
remember that most sql engines will require to name the computed expression column like I did (as average) at least in the first select of all union selects.
We have a table that has a StartDate field which holds a type of datetime. There are thousands of records and I am looking for a way to find the number of days within a given result returned from this table. For instance, if my table had this data:
ID | StartDate
--------------
1 01/01/2013 09:34:54
2 01/01/2013 11:23:21
3 04/11/2013 14:43:23
4 04/11/2013 17:13:03
5 04/25/2013 18:02:59
6 07/21/2013 02:56:12
7 10/01/2013 19:43:10
Then the query should return 5 as the 2 dates on 01/01/2013 count as 1 and the same for 04/11/2013.
The only SQL I've been able to come up with is:
SELECT COUNT(DISTINCT(DATEPART(DAY, StartDate)))
FROM Stats
WHERE StartDate BETWEEN '01/01/2013' AND '12/31/2013' --This is just for filtering
But this returns 4 because it doesn't take the month into account.
Any help is appreciated.
You can CAST as date
SELECT COUNT(DISTINCT CAST(StartDate AS DATE))
FROM Stats
WHERE StartDate >= '20130101' AND StartDate < '20140101'
Also use an unambiguous date format such as yyyymmdd and >= < not BETWEEN.
Your current query would include the 31st December if there was a row with exactly the value 20131231 00:00:00 but not any with different times on that date. I doubt that is intentional.