MySQL Workbench - Filter Date - mysql

I tried researching this problem but I couldn't find anything that fits.
I have a query that pulls data the way I want, but it shows me results for the last 30 days only, the archive is about 3 years and I would like to add a parameter that will tell me the exact date, for example August 2019.
My query:
SELECT FROM_UNIXTIME(datetimeOrigination),
callingPartyNumber,originalcalledPartyNumber,
from_unixtime(datetimeDisconnect)
FROM corn.originalcdr
WHERE callingPartyNumber like '9000'
ORDER BY datetimeorigination DESC;
Could you please advise me on the best approach?

If you want to restrict, e.g., the origination datetime field to August of 2019, then add an appropriate criteria to the WHERE clause:
SELECT *
FROM corn.originalcdr
WHERE
callingPartyNumber = '9000' AND
FROM_UNIXTIME(datetimeOrigination) >= '2019-08-01' AND
FROM_UNIXTIME(datetimeOrigination) < '2019-09-01'
ORDER BY
datetimeOrigination DESC;

Related

How would you query a cumulative sum over x amount of days?

I'm trying to find the cumulative sum of sessions of a link for its first 3 days. I tried this but it doesn't seem to take the date clause into account:
select
date,
link,
sum(sessions) as sessions
from ga
where date <= date+interval 3 day
group by link
But if I manually enter a date, it seems to work. Why is it not seeing date+interval 3 day as a proper date...?
Any help would be greatly appreciated! :)
Date is a column, not a value, you need to provide a specific date entry. Also "between" is a better keyword to use in this situation.
You need to also add date column in GROUP BY clause. Also, avoid using column name as date. It will create confusion.
Try below query :
select date_column,
link,
sum(sessions) as sessions
from ga
where date_column BETWEEN CURDATE()-3 AND CURDATE()
group by link, date_column

Simple reports in rails -- from_date to to_date

So basically, I want to create reports for my Attendance Records Table.
Table has :user_id, :clock_in, :clock_out, :seconds columns that are being updated.
Basically my report needs: to calculate (sum of :seconds) for a given period.
I.e. Jan 1, 2014 to Jan 31, 2014 ... I would like to list all records from this daterange, and in the end would calculate sum of :seconds.
Any ideas how would I approach this?
Your question is very general, please, before question try to do some work and come with technical questions.
Tips
Your question is related about SQL, basically you need to perform a select query
select * from your_table where date <= your_date and date >= your_date
Need help on mysql date range query
In rails, where clauses are build by ActiveRecord
YourModel.where('date <= ? and date >= ?', your_date. your_date)
Rails where date is greater than given date query
If you need something more advanced, use Ransack
http://railscasts.com/episodes/370-ransack
Hope I can help you ;)

MySQL SELECT Month from a particular year

Yes I've read the docs here
I have the following query:
SELECT * FROM db.table
WHERE event_type = 3
AND (YEAR(event_time) = 2014 AND MONTH(event_time) = 10)
This returns all records from October 2014. Great. Only wanted to ask, is there a shorter, better or neater way than having to write (YEAR(event_time) = 2014 AND MONTH(event_time) = 10)? Is this the "conventional" way of extracting a particular month in a given year?
Learning here.
If you have an index on the event_time column, using YEAR and MONTH (or pretty much any function) will prevent the index being used to optimize the query fully. Something like this will be fastest; I'll leave it to you to determine if it's neatest:
SELECT * FROM db.table
WHERE event_type = 3
AND event_time >= '2014-10-01' AND event_time < '2014-11-01'
That will give you everything in October 2014, and it's fully optimizable.
You can create a temporary view using the with clause of the records in the year 2014 and then extract only October from that view.
Because everytime the query needs to check if it is 2014 and month October.
So I think views will do it better.
Also you can enhance the performance by adding index

Mysql Select with Dates and maybe Case when

im having a problem where i cant think of a solution, maybe im having a bad table-structure or i just dont know enough about mysql select commands to think of a good solution. Maybe you can help me out:
So i got a table that has a Column with the Date-format (yyyy-mm-dd) i wanted to select all upcoming dates so i did:
SELECT * WHERE date >= now.
This worked kinda well but i also got "dates" where only the year is entered (2014-00-00) i also wanted to select these but "now" is already bigger so i made another column with the year only and if the month, date or both arent known i will use 0000-00-00 and the Column "year" now i could select like this:
SELECT * WHERE date >= now AND year >=now(year)
Now all entrys with 0000-00-00 wont be selected. If i use OR the entrys from last year will be shown.
So thats my problem, is there any way i can change my table so i can have entries with only the year or only year and month and of course all together? I already considered get rid of the date-format and use simple INT with seperated columns for year, month and date. But i think i will have the same problem.
Sometimes i just want to do a capsuled select like
SELECT *
WHERE (date >= now AND year >= now(year))
OR date == "0000-00-00" (i know that this doesnt work)
If I understood your problem correctly, you could use this request:
WHERE (date >= now OR year > now(year))
There is probably a simpler way though, that would preserve your design, like initializing at January 1st (01-01) instead of 00-00
I think you can use this code:
$_SESSION['month'] = //set here your selected month
$_SESSION['year'] = //set here your selected year
SELECT * FROM table WHERE DATEPART(m,date) >= '".$_SESSION['month']."' AND DATEPART(yyyy,year) >= '".$_SESSION['year']."' AND date <> '0000-00-00'
Change your table structure format. Actually just allow for that field to have null value when not entered. By default it will be null then. You shouldn't be storing 0000-00-00 as a value for Date type field. I would rather leave it as null , or as suggested in some of previous answers, initialize it with some other date. It would be much easier to manipulate with database then.
the problem is that half of you write is not MySQL and your database schema is terrible...
You have the following problems:
column data date does not have the date data type.
To fix it, you need to add a cast to the select statement eg. cast(datecolumn as date)
select * from table where cast(datecolumn as date) >= '2014-01-10';
the way to use now date is using the now function.
select now(), date(now());
result> 2014-01-10 11:11:36, 2014-01-10
select * from table where cast(datecolumn as date) >= date(now());
Because your datecolumn is not a date (2014-00-00 is not a valid date), you need to use string manipulation to extract the year.
select substring('2014-01-01', 1,4)
result> 2014
select * from table where substring(datecolumn, 1,4) = year(now());
The comparassion operator is = and not ==
the select statement syntax looks like this (pay attention because you are missing the table in your statement)
select * from [Table] where [column] = condition ...
You probably need or instead of ands, therefore your query should look like this:
select * from FooTable where
cast(datecolumn as date) >= date(now())
or substring(datecolumn, 1,4) >= year(now())
or datecolumn = '0000-00-00'
You should use something like phpmyAdmin or mySQL workbench to test your sql queries before try to use them on php, java or whatever is your programing language.

MySQL - Query last six weeks of data

In order to calculate projected sales for a given day, I need to query the last six weeks of data for a given day. For example, if I want projected sales for Friday, I need to query data from the last six Fridays only.
I'm assuming there is a way to do this within a query, just not sure exactly how. Any help or insight would be greatly appreciated, as always.
Thanks in advance.
The easiest way is to use a limit.
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=6
ORDER BY date DESC LIMIT 6;
EDIT: To get this relative to today, just add CURDATE()
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=DAYOFWEEK(CURDATE())
ORDER BY date DESC LIMIT 6;
You can use a combination of different MySQL date and time functions to achieve this. Your query could look something like this:
SELECT fields FROM table WHERE DAYOFWEEK(table.date) = DAYOFWEEK(CURDATE()) ORDER BY table.date DESC LIMIT 6
Of course you can replace CURDATE() with the date that you are trying to predict.
Select * From table Where date_field > DATE_ADD(now(),INTERVAL -42 DAY)
That's about what you will need to do.
I just seen you wanted to query only a particular day of each week. Give me a moment and I'll update this.
Nevermind, I'm not going to edit this. Bobby has your answer for you. You just need to place variables in there through your script as needed. +1 Bobby.