Add a constant date column in mysql - mysql

i have a table in mysql.
+------+---------+
|value | unit |
+------+---------+
| 2 | DAY |
| 3 | MONTH |
+------+---------+
this is just a part of my table. it consists of many rows
i want to add a date column to this table with a constant date. say '2009-01-01'
type of the column should be date.
+------------+------+---------+
| date |value | unit |
+------------+------+---------+
| 2009-01-01 | 2 | DAY |
| 2009-01-01 | 3 | MONTH |
+------------+------+---------+
i want to create table_2
create table table_2 as
select value,unit,`dates` DATE NOT NULL DEFAULT '2009-01-01' from table;
but getting syntax error.
any ideas how to do it?

Try like this:
CREATE TABLE table_2(
dates DATE
) SELECT '2009-01-01' AS dates, value,
unit FROM table_1
for more information Visit here: http://dev.mysql.com/doc/refman/5.5/en/create-table-select.html

Related

How to sum alias fields with date range

I need to show SUM value of alias table that has value of the result from multiplying 2 fields.
Here is asset table:
+----------+-----+------------+
| price | qty | tanggal |
+----------+-----+------------+
| 6775000 | 1 | 2019-03-30 |
| 4760000 | 2 | 2019-03-25 |
| 7800000 | 2 | 2019-04-01 |
| 13599000 | 1 | 2019-03-30 |
+----------+-----+------------+
I've already tried:
SELECT
SUM((price*qty))
AS worth
FROM asset
WHERE DATE(tanggal) >= 2019-03-25 AND DATE(tanggal) <= 2019-03-30
Also using BETWEEN clause like this:
SELECT
SUM((price*qty))
AS worth
FROM asset
WHERE DATE(tanggal) BETWEEN 2019-03-25 AND 2019-03-30
It keeps giving me NULL value, but if I remove the WHERE clause it
works fine and give me value of 45494000. Any ideas?
What about adding single quotes, right know you are only doing a subtraction
SELECT SUM((price*qty)) AS worth
FROM asset
WHERE tanggal >= '2019-03-25' AND tanggal <= '2019-03-30'

Properly SQL query

I need to skip results with high price per day. I've got a table like this:
+------+-------------+-------+
| days | return_date | value |
+------+-------------+-------+
| 2 | 2017-12-27 | 15180 |
| 3 | 2017-12-28 | 14449 |
| 4 | 2017-12-29 | 13081 |
| 5 | 2017-12-30 | 11203 |
| 6 | 2017-12-31 | 9497 |
| 6 | 2017-12-31 | 9442 |
+------+-------------+-------+
How can I print only the lowest price for 6 days (9442 in this example).
We can use a GROUP BY clause and an aggregate function. For example:
SELECT t.days
, t.return_date
, MIN(t.value) AS min_value
FROM mytable t
GROUP
BY t.days
, t.return_date
This doesn't really "skip" rows. It accesses all the rows that satisfy the conditions in the WHERE clause (in this example, every row in the table). Then MySQL collapses rows into groups (in this example, rows with identical values of days and return_date get put into a group. The MIN(t.value) aggregate function selects out the minimum (lowest) value out of the group.
The query above is just an example of one approach of satisfying a particular specification.

Where clause containing date in MySQL statement not working

Table Name: DemoTable.
Total Fields: 2
Fields:
id (int, auto increment, primary key)
month_and_year (varchar(10))
month_and_year contains date as '2015-03', '2015-01', '2014-12' and so on...
I am trying to get values from the table between '2014-10' and '2015-03'.
SELECT * FROM DemoTable where month_and_year>='2014-10' AND month_and_year<='2015-03' ORDER BY month_and_year DESC
Query does not give desired output as month_and_year field has varchar data type. Changing varchar to date data type isn't possible as date data type does not accept date in 'yyyy-mm' format.
How can the result be obtained?
PS:Is UNIX_TIMESTAMP() a safe bet in this case?
You should never store date value as varchar and choose mysql native date related data types like date,datetime or timestamp
However in your case you need to do some date related calculations before doing the select query. Consider the following table
mysql> select * from test ;
+------+----------------+
| id | month_and_year |
+------+----------------+
| 1 | 2014-10 |
| 2 | 2014-10 |
| 3 | 2014-09 |
| 4 | 2014-11 |
| 5 | 2015-01 |
| 6 | 2014-08 |
+------+----------------+
Now the approach would as
First convert the varchar to real date
Then for the lower limit always start the comparison from first day of the year month value
The upper limit will be till the end of the month.
So the query becomes
select * from test
where
date_format(
str_to_date(
month_and_year,'%Y-%m'
),'%Y-%m-01'
)
>=
date_format(
str_to_date('2014-10','%Y-%m'
),'%Y-%m-01'
)
and
last_day(
date_format(
str_to_date(month_and_year,'%Y-%m'
),'%Y-%m-01'
)
)
<=
last_day(
date_format(
str_to_date('2015-03','%Y-%m'
),'%Y-%m-01'
)
);
The output will be as
+------+----------------+
| id | month_and_year |
+------+----------------+
| 1 | 2014-10 |
| 2 | 2014-10 |
| 4 | 2014-11 |
| 5 | 2015-01 |
+------+----------------+
Use the function STR_TO_DATE(string,format);
http://www.mysqltutorial.org/mysql-str_to_date/
You should use either mysql date time functions or use int field in mysql and store UNIXTIMESTAMP and compare like you are already doing. I think it is overkill to store unixtimestamp because you only need month and year and you won't benefit a lot from unixtimestamp advantages.

want to get day name for the corresponding date

I have a table setup as shown below.
Table Name: activity.
| ACTIVITY_ID | DATE | ASSIGN_ENGR | TASK_TYPE | TASK_STATUS |
|-------------|------------|-------------|-----------|-------------|
| 1 | 2013-12-31 | Sachin | Monthly | Scheduled |
| 2 | 2013-12-23 | Mikel | Weekly | Done |
| 3 | 2013-10-18 | John | Monthly | Done |
I want to get day name against my date field using query.
MySql Query
SELECT DAYNAME('2007-02-03');
Output:
Saturday
Your Query would be like this
select Activity_ID, Date , DayName(Date) As Day, Assign_Engr, Task_Type,Task_Status From Your_Table_Name;
Dayname() function
Mysql provides with DAYNAME() function.
DATE_FORMAT(NOW(),'%a') # %a: Abbreviated weekday name
For more information: w3schools
A simple query could look like this:
SELECT DATE_FORMAT(date, ‘%a’) AS dateday FROM users WHERE id = 1;

MySQL - move from flat table to first normal form

I am building an application that will allow a user to record weekly activity over a 6 week period. Each week has 3 benchmarks to record against, here is an example:
Week 1
+------------+-----------+------------+-----------+
| Day | Minutes | Location | Miles |
+------------+-----------+------------+-----------+
| Monday | | | |
+------------+-----------+------------+-----------+
| Tuesday | | | |
+------------+-----------+------------+-----------+
| Wednesday | | | |
+------------+-----------+------------+-----------+
| Thursday | | | |
+------------+-----------+------------+-----------+
| Friday | | | |
+------------+-----------+------------+-----------+
| Saturday | | | |
+------------+-----------+------------+-----------+
| Sunday | | | |
+------------+-----------+------------+-----------+
This is repeated for each week up to 6.
In my flat table I have the following:
UserID | Username | Week 1 Day 1 Minutes | Week 1 Day 1 Location | Week 1 Day 1 Miles | Week 1 Day 2 Minutes | Week 1 Day 2 Location | Week 1 Day 2 Miles ETC...
X 7 for a week and then X 6 for the 6 weeks.
I am trying to figure out where my eliminations are, and what my separate tables would be. So far I have the following:
User Table
+------------+-----------+
| UserID | Username |
+------------+-----------+
| | |
+------------+-----------+
Activity Table
+------------+-----------+------------+-----------+------------+-----------+
| UserID | WeekID | Day | Minutes | Location | Miles |
+------------+-----------+------------+-----------+------------+-----------+
| | | | | | |
+------------+-----------+------------+-----------+------------+-----------+
Weeks Table
+------------+-----------+------------+
| UserID | WeekID | Week_No |
+------------+-----------+------------+
| | | |
+------------+-----------+------------+
I think I am getting along the right lines, but the Weeks Table doesn't seem right and I am not sure what the relationships are - I don't think I need UserID in each table, and I'm not sure what the PKs should be.
Any comments on this schema, or an efficient way to achieve the first normal form given the application requirements would be much appreciated, many thanks.
EDIT:
Thanks very much for all the answers, great stuff.
I think having a Location Table would be beneficial as I could standardize locations (could provide a list to choose from) and if I need to query based on location, I'll have consistent location names.
Revised the schema to this:
User Table - UserID PK
+------------+-----------+
| UserID | Username |
+------------+-----------+
| | |
+------------+-----------+
Activity Table - ActivityID PK
+------------+-----------+------------+-----------+------------+-------------+-----------+
| ActivityID | UserID | Week_No | Day | Minutes | LocationID | Miles |
+------------+-----------+------------+-----------+------------+-------------+-----------+
| | | | | | | |
+------------+-----------+------------+-----------+------------+-------------+-----------+
Location Table - LocationID PK
+------------+---------------+
| LocationID | Location_Name |
+------------+---------------+
| | |
+------------+---------------+
2nd EDIT:
I now have a question on 2NF and 3NF on this topic:
MySQL - moving from 1st Normal Form to 2nd and 3rd Normal Forms
Add a Location table and change Location to LocationID (PK). The Weeks table does not need UserID in it. You can find what weeks a user has by querying the Activity table.
I only see the need for a Week table if Week_No changes by user, which doesn't seem to make too much sense. Otherwise, you can just replace WeekID with WeekNo in the Activity table, and delete the Weeks table.
In the weeks table your primary key should be WeekID. I'm not sure you would need a week table though as you don't seem to be storing anything in it apart from the week that the activity took place, which could actually be in the activity table. So I would get rid of it, add Week_No to the Activity table, and have an ActivityID in the Activity table as primary, and UserID as Foriegn Key.
Don't want to tell you too much, just enough to get you on your way as you seem to want to normalise this fully own your own.
PKs:
User tbl: UserID
Weeks tbl: WeekId
Activity tbl: (UserID,WeekID)
And you don't need a UserId in Weeks Tbl
i don't think you need a week table.
activity table with userid, weekno, day(enum), minutes, location, miles, is enough for what you mentioned...