MySQL query group month into two periods - mysql

I want to do group select in MySQL, which is group my timestamp column into 2 periods of month and count each rows. just say, if there are rows in a month in date 1, 2 ,3, and also 17,19 in Sept it would be :
Period count
Sept 1 3
Sept 2 2
Anyone can help me please?
Thanks!

I assume you have 1 column period
SELECT period, count(period) AS count FROM table_name GROUP BY period

Related

Select * Where Date 1 and Date 2 elapsed time is greater than

I have 2 Columns in the same table: CREATED DATE and APPROVED DATE
In my Select statement, I would like to return only records Where the elapsed time between CREATED DATE and APPROVED DATE is greater than 2 days, or where the APPROVED DATE is greater than CREATED DATE by 2 days.
I really appreciate the help!
You can use DATEDIFF
WHERE DATEDIFF(created_date, approved_date) > 2 OR DATEDIFF(approved_date, created_date) > 2
Depending on how precisely you need to know:
If the "date" columns are dates:
WHERE DATEDIFF(`APPROVED DATE`, `CREATED DATE`) > 2
If the "date" columns are timestamps and you want to know the second 2 days have passed:
WHERE TIMESTAMPDIFF(DAY, `CREATED DATE`, `APPROVED DATE`) > 2

SQL: MAX of summing the values of 2 columns

so I have a table "records" like this:
name month year
Rafael 9 2018
Rafael 5 2018
Rafael 10 2017
And I want to get my last records (Rafael, 9, 2018). I thought about summing the month and the year and getting the max of that sum like this:
select * from records, max(month + year) as max_date
But doesn't seem to be right. Thanks for help
Using ORDER BY clause, you can get the highest year and month combo. Try the following:
SELECT *
FROM records
ORDER BY year DESC, month DESC
LIMIT 1
Do you mean the output of the follwing?
select *
from records
order by year desc, month desc
limit 1
In general, it would be more useful to use one DATE or DATETIME column type for this purpose where you can extract year and month if you want.
Use concat if you want to concat max month and max year
Select name ,concat (concat( max(month), '-'),max(year)) from records
Group by name
but if you want just year wise max year date information then use below
Select * from records
order by year desc
limit 1
https://www.db-fiddle.com/f/sqQz1WEEAukoWEWkbxBYxe/0
name month year
Rafael 9 2018

MYSQL - Can't group a summed column

I have a table with faktura_kroner (decimal 9,2), and faktura_dato (BIGINT 11, which is a unix timestamp).
I am trying to get the sum of faktura_kroner per month so I can use it in a chart, but am struggling a bit to find the correct sql query. Also it is unique by year. So October 2016 should not be grouped with October 2017 as an example.
Any help appreciated..
SELECT sum(faktura_kroner) as sum_faktura,
MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month,
YEAR(FROM_UNIXTIME(faktura_dato)) as year
FROM
faktura
WHERE
user_id = 1
AND
virksomhet_id = 1
GROUP BY YEAR(FROM_UNIXTIME(faktura_dato)) DESC,
MONTH(FROM_UNIXTIME(faktura_dato)) ASC, faktura_dato DESC;
You can try below query -
SELECT sum(faktura_kroner) as sum_faktura
,MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month
,YEAR(FROM_UNIXTIME(faktura_dato)) as year
FROM faktura
WHERE user_id = 1
AND
virksomhet_id = 1
GROUP BY MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month
,YEAR(FROM_UNIXTIME(faktura_dato)) as year

MySQL : Count row where sum of left day is less than 30

I want to count the users in the table who's subscriptions are going to expire within a month (30 days). Here is my code:
user_db
id name exp_date
1 John 2013-03-01
2 Alice 2013-02-25
3 Ken 2013-01-10
4 Elise 2013-04-11
5 Bruce 2013-03-14
According to the DB above. There should be 3 persons whom their subscription is about to be expired - John, Alice and Bruce. I don't want Ken to be counted because he doesn't want to subscribe for more.
Here's my MySQL code:
SELECT count(id) AS exp_pax,
datediff(exp_date,now()) AS day_left
FROM labour_db
WHERE day_left<=30
Well, the code does selects only a row in which the sum of day less than 30 but it doesn't count. So please you guy suggest me.
Regards,
If you want to count all records where (1) the expiration date is within 30 days of now and (2) the expiration date is not before now, then use
SELECT count(*) AS exp_pax
FROM user_db
WHERE exp_date<=timestampadd(day, 30, now())
AND exp_date >= now();
If that's the case then you need to add condition wherein it checks if the exp_date is less than today.
SELECT COUNT(*) totalCount
FROM user_db
where exp_date <= timestampadd(day, 30, now()) AND
exp_date > NOW()
SQLFiddle Demo
Remove the group by id to get your count.
Count will roll up rows, as you expect, but when combined with a group by clause, will count each "group". You could use this usefully, for instance, to group by expiration date.

How to count and group items by day of the week?

I have the following (MySQL) table called "tweets":
tweet_id created_at
---------------------
1 1298027046
2 1298027100
5 1298477008
I want MySQL returning the number of tweets per day of the week; taking the above data it should return:
Wednesday 1
Friday 2
I now have the following query (which should return the day of the week index, not the full name):
SELECT
COUNT(`tweet_id`),
WEEKDAY(FROM_UNIXTIME(`created_at`))
FROM tweets2
ORDER BY WEEKDAY(FROM_UNIXTIME(`created_at`))
This however returns:
COUNT(`tweet_id`) WEEKDAY(FROM_UNIXTIME(`created_at`))
7377 4
(There are a total of 7377 tweets in the database). What am I doing wrong?
SELECT
COUNT(`tweet_id`),
DAYNAME(FROM_UNIXTIME(created_at)) AS Day_Name1
FROM tweets2
GROUP BY Day_Name1
ORDER BY Day_Name1;
You have a count, but you don't have a group by. You should include a
GROUP BY WEEKDAY(FROM_UNIXTIME(`created_at`))
You are not grouping by week day so you only get one grand total. Try this:
SELECT
COUNT(`tweet_id`),
WEEKDAY(FROM_UNIXTIME(`created_at`))
FROM tweets2
GROUP BY WEEKDAY(FROM_UNIXTIME(`created_at`))
ORDER BY WEEKDAY(FROM_UNIXTIME(`created_at`));