mysql print date as date(week no) - mysql

I have a mysql table with date column, I want to print my table in a pattern that in a same column it should print date as date(week no.) or date-week no.
e.g. 2014-06-05-Week 22 or 2014-06-05(Week 22).
Please suggest, my table looks like as below:
------------+-----------+-----------+------------+------------+------------+------------+------------+------------+
date | 2006_2007 | 2007_2008 | 2008_2009 | 2009_2010 | 2010_2011 | 2011_2012 | 2012_2013 | 2013_2014 |
------------+-----------+-----------+------------+------------+------------+------------+------------+------------+
2013-06-05 | 412684953 | 618821373 | 912382161 | 1152333713 | 1696469379 | 1992249499 | 2311645340 | 2525687604 |

use MySQL WEEK() returns the week number for a given date.
The argument allows the user to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. If no argument is included with the function, it returns the default week format.
Syntax
WEEK(date[mode]);
Example
SELECT WEEK('2009-05-18',1);
EDIT
SELECT CONCAT('2009-05-18', '-week ', WEEK('2009-05-18',1));

Related

Sum a column or another based on a helper column - SQL

I'm using Google Data Studio, and have 12 columns, 1 per month, with numbers, and another column with dates. I'd like to SUM all the numbers that will fall inside a date range based on the date column.
So I've something like this:
+---------+---------+---------+---------+
| DATE | January | February | March |
+---------+---------+---------+---------+
|20180101 | 500 | | |
|20180203 | 150 | | |
|20180201 | | 100 | |
|20180301 | | | 200 |
+---------+---------+---------+---------+
I'd like to have 650 as the result from January extraction, but can't find a solution yet.
Are you looking for aggregation?
select sum(january)
from t
where date >= :datestart and date < :dateend

MySQL query with list of values

I have a table with over then 50kk rows.
trackpoint:
+----+------------+-------------------+
| id | created_at | tag |
+----+------------+-------------------+
| 1 | 1484407910 | visitorDevice643 |
| 2 | 1484407913 | visitorDevice643 |
| 3 | 1484407916 | visitorDevice643 |
| 4 | 1484393575 | anonymousDevice16 |
| 5 | 1484393578 | anonymousDevice16 |
+----+------------+-------------------+
where 'created_at' is a timestamp of row added.
and i have a list of timestamps, for example like this one:
timestamps = [1502744400, 1502830800, 1502917200]
I need to select all timestamp in every interval between i and i+1 of timestamp.
Using Django ORM it's look like:
step = 86400
for ts in timestamps[:-1]:
trackpoint_set.filter(created_at__gte=ts,created_at__lt=ts + step).values('tag').distinct().count()
Because of actually timestamps list is very very longer and table has many of rows, finally i getting 500 time-out
So, my question is, how to for it in ONE raw SQL query join rows and list of values, so it looks like [(1502744400, 650), (1502830800, 1550)...]
Where second first value is timestamp, and the second is count of unique tags in each interval.
First index created_at. Next build query like created_at in (timestamp, timestamp+1). For each timestamp, run the query one by one rather than all at once.

Different value counts on same column using LIKE

I have a database like below
+------------+---------------------------------------+--------+
| sender | subject | day |
+------------+---------------------------------------+--------+
| Darshana | Re: [Dev] [Platform] Build error | Monday |
| Dushan A | (MOLDOVADEVDEV-49) GREG Startup Error | Monday |
+------------+---------------------------------------+--------+
I want to get the result using the above table. It should check if the subject contains the given word then add one to the that word column for a given day.
|Day | "Dev" | "startup"|
+---------+------------+----------+
| Monday | 1 | 2 |
| Friday | 0 | 3 |
I was thought of using DECODE function but I couldn't get the expected result.
You can do this with conditional aggregation:
select day, sum(subject like '%Dev%') as Dev,
sum(subject like '%startup%') as startup
from table t
group by day;

MySQL: Proper date range between start and end date as two fields

I have the following table called seasons
+----+--------+------------+------------+
| id | cost | start | end |
+----+--------+------------+------------+
| 33 | 255 | 2014-01-05 | 2014-04-16 |
| 17 | 357 | 2014-04-17 | 2014-04-19 |
| 65 | 191.25 | 2014-04-20 | 2014-07-10 |
| 49 | 255 | 2014-07-11 | 2014-08-23 |
| 81 | 191.25 | 2014-08-24 | 2014-12-18 |
+----+--------+------------+------------+
I am trying to get a date range between start and end using the following query.
SELECT
*
FROM
seasons
WHERE
(start BETWEEN '2014-01-05' AND '2014-01-05' OR end BETWEEN '2014-01-05' AND '2014-01-05');
I was able to get a result set if the start date started exactly on the value on the field.
+----+------+------------+------------+
| id | cost | start | end |
+----+------+------------+------------+
| 33 | 255 | 2014-01-05 | 2014-04-16 |
+----+------+------------+------------+
Now the problem is when I advance the date to
2014-01-06
SELECT
*
FROM
seasons
WHERE
(start BETWEEN '2014-01-06' AND '2014-01-06' OR end BETWEEN '2014-01-06' AND '2014-01-06');
Empty set (0.00 sec)
There are NO RESULT. How can I get the date range in between to different fields on SQL?
Any help is appreciated.
You have your logic backward. If you want seasons where a given date is in that season then your where clause should look like:
WHERE '2014-01-06' BETWEEN start AND end;
Since you are not really looking for a range and only needing a specific date, you could also just use (SQL Fiddle):
SELECT *
FROM seasons
WHERE start = '2014-01-05' OR end = '2014-01-05'
When querying if a certain date is in a given range, the WHERE clause typically is:
WHERE 'specified_date' BETWEEN 'start_date' AND 'end_date'
Otherwise it is an illogical date span to MySQL and will return an empty result set.
The BETWEEN Comparison Operator is equivalent to:
min <= expr AND expr <= max
Therefore, it's meant to be used like this:
expr BETWEEN min AND max
Here is documentation from MySQL on the BETWEEN Comparison Operator.

MySQL select Current_timestamp between

I have a list of users in MySQL and on subscription the timestamp is set in the data base using the CURRENT_TIMESTAMP.
Now I want to do a select from this table where the subscribe date is between day X and day Y
I tried several queries but somehow they all turn up empty.
Here is my last version
SELECT *
FROM users
WHERE subscribe_date BETWEEN '2013-10-07'AND '2013-13-10'
As I know for sure this date: 2013-10-08 14:38:49
is in the subscribe_data field It should turn up somehow
What is the best way to do this?
Maybe good to know my 'subscribe_date' column has type 'timestamp' and is auto filled with 'CURRENT_TIMESTAMP'
Here is the data in this table:
+----+-----------+---------------------+
| id | firstname | subscribe_date |
+----+-----------+---------------------+
| 20 | Peter | 2013-10-01 14:37:17 |
| 21 | Jack | 2013-10-08 14:38:49 |
| 22 | Andrew | 2013-10-10 14:41:03 |
| 23 | Margret | 2013-10-14 14:42:46 |
+----+-----------+---------------------+
Since TIMESTAMP is up to seconds precision usually, you have to add time part:
SELECT *
FROM users
WHERE (subscribe_date BETWEEN '2013-10-07 00:00:00' AND '2013-12-10 23:59:59')
I've fixed your '2013-13-10' to '2013-12-10 23:59:59' since there's no 13-th month (and in DATETIME format it's YYYY-MM-DD, so month goes second)