My query to get data by month
$this->db->select('vehicle_make_and_model');
$this->db->join('tukai_drivers','tukai_drivers.driver_id=reqn_challans.dc_driver_id','left');
$this->db->where('dc_challan_date','MONTH(2)');
$this->db->order_by('dc_challan_id','desc');
$result=$this->db->get('reqn_challans')->result();
I am trying it via codeignitor like this
$this->db->where('dc_challan_date','MONTH(2)');
It did not work? What is wrong
Storing date like this
http://awesomescreenshot.com/0424q72ne5
You need to update your where condition from
$this->db->where('dc_challan_date','MONTH(2)');
to
$this->db->where('MONTH(dc_challan_date)','2');
The actual function is like MONTH(date) so here you have MONTH(dc_challan_date) which is your column name
In SQL databases you should NOT apply functions to data to suit the where clause so that you maximize the benefit of indexes (i.e. get better performance). For example, instead of using MONTH(dc_challan_date) you can achieve getting a month of data using the >= with < such as the following SQL:
select * from table
where dc_challan_date >= '2015-02-01'
and dc_challan_date < '2015-03-01'
The database optimizer can use an index on column dc_challan_date in that query. The example uses "sargable predicates".
Related
I have a condition that I need to query Couchbase database field with two different where clauses in the same query.
for example:
for 'sales' field in a database entity, I want to have one query to get the sales in a single hour and whole day
output example:
`{`
`hourly_sales = 500`
`total_day_sales = 5000
`}`
I know that I can use 2 different queries, but requirement is to use one query for both
You can achieve that using case when statement. For example -
SELECT SUM(CASE WHEN `TIME` BETWEEN "13:00:00" AND "14:00:00" THEN `SALES`) AS HOURLY_SALES,
SUM(CASE WHEN `TIME` BETWEEN "00:00:00" AND "23:59:59" THEN `SALES`) AS TOTAL_DAY_SALES
FROM `BUCKET_NAME`
Make use of the time format according to your need.
I have a the following query :
select * from table where createddate>='03-Feb-2020' and createddate<'04-Feb-2020'
The above query will give me incremental count for a single day.
How do i generalize the above query so that i can get the entire historical data/full dump without changing the where clause.
For example:
select * from table where createddate>='VARIABLE1' and createddate<'VARIABLE2'
Is there a way that without changing the schema of the sql query i can just pass in different values for the createddate to get the full dump?
Is this what you want?
where createddate >= '1000-01-01' and createddate < '9999-12-31'
Note that dates in MySQL must be formated as YYYY-MM-DD.
Do you want group by?
select date(createddate), count(*)
from table
group by date(createddate);
This returns the count for each day.
I'm trying to accelerate a query. This query looks if the year is '2017'. I'm comparing costing between using: LIKE = '2017%', YEAR (date) = '2017' and date BETWEEN '2017-1-1' AND '2017-12-31'.
I would like to make an index for the colum date but using the function year, something similar to:
CREATE INDEX indexDATE ON
table (YEAR(date));
Is this possible?
The right way to express the logic is:
date BETWEEN '2017-01-01' AND '2017-12-31'
or as I prefer:
date >= '2017-01-01' AND date < '2018-01-01'
This can use an index on (date).
The expression LIKE = '2017%' is simply bad coding. You are using string functions on a date/time column. That is a really bad idea and it precludes the use of indexes.
The expression YEAR (date) = 2017 is logically ok -- once you remove the single quotes around the number. However, the use of the function on the column precludes the use of an index.
Finally, in most data sets, years are not very selective. That is, you are still going to be selecting a significant portion of the rows. Indexes are less useful for such queries.
I was hoping if any of you could be of any help, if I have a huge database with million of entries and all I need is information relating to a specific year (say, 2015), if I use the following query, performance wise will this be best practice or is there another way to have a better query,
CREATE INDEX table1_idx ON table1 (date_column);
SELECT text, date_column
FROM table1
WHERE datepart(yyyy, date_column) = 2015;
Any help relating to this will be highly appreciated.
If this is about SQL Server, that is not the way to do it. Using any functions on the fields makes in non-SARGable.
Always use correct search criteria like this:
SELECT text, date_column
FROM table1
WHERE date_column >= '20150101' and date_column < '20160101'
You might also want to consider adding text as included column to prevent key lookups:
CREATE INDEX table1_idx ON table1 (date_column) include (text)
MySQL can't use an index range scan operation, due to the function wrapped around the column. The function has to be evaluated for every row in the table.
To get an index range scan operation, do comparisons on the bare column.
For example:
SELECT t.text
, t.date_column
FROM table1 t
WHERE t.date_column >= '2015-01-01'
AND t.date_column < '2015-01-01' + INTERVAL 1 YEAR
I have the following problem:
In mysql I have a table which contains two date columns start_date and end_date. The date format is yyyy-mm-dd. What I am trying to do is to get all data from all the rows where a specific date, lets say '2012-03-05' mateches one of these date columns or are something in between.
How can I create a good sql-query that gets the data needed? I've checked on the between statement but I don't really know if that's the best way to go. I guess this is generally a simple task but I just can't figure a good query out.
Thanks.
SELECT * FROM table WHERE start_date <= '2012-02-29' AND end_date >= '2012-02-29';
Should do it.
This is a very common way to structure your tables with ranges of dates, especially in temporal database designs. It lets you perform range-based queries very efficiently, assuming that indexes on both columns exist. You query the data like this:
select *
from mytable t
where t.start_date <= #desired_date and t.endDate > #desired_date
#desired_date is the date for which you would like to query, e.g. '2012-03-05'.
Note the <= on one side and > on the other side, without =. This is done to ensure that the from-to ranges define non-overlapping intervals.
Not sure, try something like this:
SELECT
*
FROM
mytable
WHERE
'2012-03-05' BETWEEN start_date AND end_date
SELECT * FROM mytable
WHERE '2012-03-05' BETWEEN start_date AND end_date;