I have this kind of table which have varchar and int type only. I need to pass the date (parameter) from other table into this table. E.g : When i pass '2018-01-30' it will display from Jan column.
Is there a way to archieve this?
If you want to display a different column based on the month of the date, that's possible eith a single query by combining the elt() and month() functions to achieve this:
select elt(month('2018-01-30'),jan, feb, mar, ..., dec) as mon from yourtable
elt() returns the Nth parameter based on the index provided in its first parameter. The month() function returns the number of the month in the date (e.g. january => 1).
If you want to display different number of columns based on the date, then that is not possible with a single query because the number of columns must be fixed in a query. In that case you need to write either a stored procedure that uses dynamic sql to generate the query or a piece of code in an external programming language that dynamically assembles and executes such a query.
Further notes:
The data stored in your columns seem to be numbers, yet you store them as strings. The decimal data type seems a lot more appropriate.
Consider changing your data structure and have a single month or year_month column plus a price column instead of the several month columns. Total and average can be calculated on the fly.
Related
I have the following data.
I want to get a pivot result based on merchants as rows and Month, Year, Type as columns and Amount as the agg value.
I know the use of case clause in Mysql but I wish to automate it for the upcoming data and don't want to hard code month, Year and type since I have thousands of "Types" in the real data.
Thanks in advance.
I am stuck in a situation where I am reading data from CSV file through 'LOAD DATA LOCAL INFILE' and storing it in Mysql table.
The date column in my table is of type string.
The below query is not working if my Date format is 'yy/dd/MM', it only returns 2 records
select column1, column2 from myTable where date between '16/08/15' and '16/08/20';
and if I ran this:
select column1, column2 from myTable where date > '16/08/15';
It return all records.
Is there a way to ran the first query so I can specify start and end date ?
Turn your dates into 'yyyy-mm-dd' format before using them inside queries, and it will be easier.
Instead of passing the year as yy try to pass it as yyyy in the date string since my sql stores dates in that format.Since every year is a four digit one, giving a two digit year in the where clause in order to pull out all records greater than that year will actually pull out all the records.
Since you say your date column is of type "String" and not DATE so in your query mysql is not operating the between function on dates, instead it's operating it on strings. You probably need to cast you string dates to actual dates before applying the between function. see e.g. str_to_date function.
I am having my date field in Mysql which is stored as char is as follows 050712.. Now I would like to display the results which are available in the database which are less than this date. I write as follows
Condition should fail
select * from tblFedACHRDFI where date_format(changedate,'%m/%d/%Y')> 05/08/12;
This is displaying all records which are available but I don't need that I would like to display only when date is 05/06/12 which means
True Condition
select * from tblFedACHRDFI where date_format(changedate,'%m/%d/%Y')> 05/06/12;
The same worked for me in Sqlserver when I write as follows
Records not getting displayed which is true as per my requirement
select * from tblFedACHRDFI where
CONVERT(datetime,(SUBSTRING(ChangeDate,1,2)+'/'
+SUBSTRING(ChangeDate,3,2)+'/'+dbo.Years
(SUBSTRING(ChangeDate,5,2))+SUBSTRING(ChangeDate,5,2)))>
'05/08/2012'
So can any one help me where I went wrong in MySql statement..
A MySQL date should be YYYY-MM-DD, column type should be DATE.
If you wish to store a date any other way (for example, a CHAR(6) as you do here), you'll have to use conversions each time you use the date. This is slower, uses more CPU, and can fail because you can store invalid values in your CHAR field.
It does work, however. Use the STR_TO_DATE function to convert your CHAR column to a proper date. Now you can compare it against a proper date, use INTERVAL functions, the whole shebang:
select *
from tblFedACHRDFI
where str_to_date(changedate,'%m%d%Y') > "2012-08-05";
Is it better to store dates in mysql in three columns or use just one column. Which one is faster. Also, if I just want to do inserts with todays date in format dd/mm/yy , how do I do that. and also how do I do selects with that. Also, lets say if I wanted to get results for all the wednesdays, how do I do that or lets say one date 25th of all the months and years, how do i do that.
Thanks People.
I am using PHP with Apache and Mysql.
What are the drawbacks of using the structure that I am proposing. I can easily get all the 25th by using the date table and I can get all the days using another column for days. How much difference would be there in the terms of speed between my proposed solution and using a DATE table?
You will want to use a proper column type, such as DATE, DATETIME, or TIMESTAMP, depending on your needs. They are built specifically to handle dates, and can more easily perform other functions (adding, comparing, etc.) that would be difficult to perform on 3 separate columns.
Read this for more info.
DAYOFWEEK(date) will give you a numeric representation for the day. In your case, 4 = Wednesday. DAYOFMONTH(date) will work for finding all 25th days of the month.
DAYNAME(date) will return the name of the weekday for date
Also, if I just want to do inserts with todays date in format dd/mm/yy ,how do I do that.
Well it depends on the format your date is passed in through your
form but you are going to want to store your date in YYYY-mm-dd format.
INSERT INTO my_table (timefieldname) VALUES ( '$date' );
and also how do I do selects with that.
SELECT timefieldname FROM my_table;
//or you can format the date - this will give you month/day/year 01/01/2012
SELECT DATE_FORMAT(timefieldname, '%m/%d/%Y') FROM my_table;
Also, lets say if I wanted to get results for all the wednesdays,
SELECT timefieldname FROM my_table WHERE DAYNAME(timefieldname) = 'Wednesday';
How do I do that or lets say one date 25th of all the months and years, how do i do that.
SELECT timefieldname FROM my_table WHERE DAY(timefieldname) = '25';
You can free up having to pass dates from your codebase and let mysql insert them for you, provided they are time stamps:
ALTER TABLE tablename ADD `timefieldname` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;
It's not much of a speed boost, but it does reduce your need to code and validate variables passed to the database.
I have a MySQL database with two fields: Date and Value.
My dates are quarters (Q1-2007, Q2-2008...). I want to be able to use the following SQL query:
SELECT * FROM table WHERE 1 ORDER BY Date
How can I do this?
I'm guessing you want them ordered so that all the 2007 ones appear in order followed by the 2008 ones, etc. This should work:
SELECT * FROM table WHERE 1 ORDER BY SUBSTRING(Date, 4, 4), SUBSTRING(Date, 1, 2);
Assuming your date formats are all consistent and that your 'Date' column is actually a VARCHAR or similar...
Use a date-column and decide on a date for each quarter. Then you can use the builtin functions for dates.
Doesn't SELECT * FROM table WHERE 1 ORDER BY Date already sort the field Date as Q1-2007, Q2-2007, Q3-2007, Q4-2007? If you sort such strings, that should be how they are sorted.
If then I would create a database to contain those values, I would rather use 3 fields, and use two different fields for quarter, and year.