How can I calculate dates from another column and another table?

Zoltan Molnar

I have 2 tables:

table 1:

|| *handtool_id* || *maintenance_interval_value* || *unit_unit_id* || *handtool_last_date_of_maintenance* || *handtool_next_date_of_maintenance* ||
||             1 ||                            1 ||              5 ||                          2014-11-07 ||                                     ||
||             2 ||                            1 ||              6 ||                          2014-11-07 ||                                     ||
||             3 ||                            4 ||              4 ||                          2014-11-07 ||                                     ||

table 2:

|| *unit_id* || *unit_name* || *unit_value* || *unit_parent_id* ||
||         1 ||      Minute ||            1 ||                1 ||
||         2 ||        Hour ||           60 ||                1 ||
||         3 ||         Day ||         1440 ||                1 ||
||         4 ||        Week ||        10080 ||                1 ||
||         5 ||       Month ||        32767 ||                1 ||
||         6 ||        Year ||       525949 ||                1 ||

What is the right syntax for calculating the *handtool_next_date_of_maintenance* from *maintenance_interval_value* and from *unit_unit_id* + *handtool_last_date_of_maintenance*? Thank you

Haleemur Ali

As Mats Kindani suggested, you need to join the two tables to get the next maintenance date.

SELECT
table1.handtool_id,
table1.handtool_last_date_of_maintenance,
DATE_ADD(table1.handtool_last_date_of_maintenance, 
    INTERVAL table1.maintenance_interval_value*table2.unit_value SECOND) next_date_of_maintenance
FROM table1
JOIN table2 ON table1.unit_unit_id = table2.unit_id

The function I'm using to calculate the next maintenance date is called DATE_ADD. You can read up on its usage in previous link.

Finally, if you want to populate table1 with the "next date of maintenance", you'll have to transform my select query into an update query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I calculate dates from another column and another table?

From Java

How can I add column from one table to another in Clickhouse?

From Dev

How can I calculate a ratio that express the proportionality grouped by another column?

From Dev

Calculate column value from another column in another table

From Dev

Calculate column value from another column in another table

From Dev

How can I add a column that increments on another column in same table?

From Dev

How can I add a column that increments on another column in same table?

From Dev

How can I copy one column to from one table to another in SQL Server

From Dev

How can I parse data from one column in a table and put the result into another with SQL Server?

From Dev

How can i create Computed Column which gets data from another table

From Dev

How can I update a column with same value in another table ssms?

From Dev

How can I populate a parent and a child table from another table?

From Dev

How to get all records from one table between dates of column in another table

From Dev

How to Update Column from a Column in another table

From Dev

How can I add a column from one dataframe to another dataframe?

From Dev

How to create a computed column that references another column from another table?

From Dev

How can i sum rows when a certain column value are equal from another table and insert them to a third table?

From Dev

How can i sum rows when a certain column value are equal from another table and insert them to a third table?

From Dev

How can I replace NaN values in DataFrame from another table?

From Dev

How can I add a new attribute from another table to a serializer?

From Dev

How can i insert rows from another table in firebird?

From Dev

How can I add a new attribute from another table to a serializer?

From Dev

How can I delete multply rows from a table with another condition?

From Dev

Can I group-aggregate-calculate from another table directly in razor view?

From Dev

How to compare dates from one table with another table

From Dev

how to calculate average of values in a column by considering the information from another column?

From Dev

How to copy values from table for another column

From Dev

How to copy the column id from another table?

From Dev

Can I use a computed column in a table to fetch data from another table columns

Related Related

  1. 1

    How can I calculate dates from another column and another table?

  2. 2

    How can I add column from one table to another in Clickhouse?

  3. 3

    How can I calculate a ratio that express the proportionality grouped by another column?

  4. 4

    Calculate column value from another column in another table

  5. 5

    Calculate column value from another column in another table

  6. 6

    How can I add a column that increments on another column in same table?

  7. 7

    How can I add a column that increments on another column in same table?

  8. 8

    How can I copy one column to from one table to another in SQL Server

  9. 9

    How can I parse data from one column in a table and put the result into another with SQL Server?

  10. 10

    How can i create Computed Column which gets data from another table

  11. 11

    How can I update a column with same value in another table ssms?

  12. 12

    How can I populate a parent and a child table from another table?

  13. 13

    How to get all records from one table between dates of column in another table

  14. 14

    How to Update Column from a Column in another table

  15. 15

    How can I add a column from one dataframe to another dataframe?

  16. 16

    How to create a computed column that references another column from another table?

  17. 17

    How can i sum rows when a certain column value are equal from another table and insert them to a third table?

  18. 18

    How can i sum rows when a certain column value are equal from another table and insert them to a third table?

  19. 19

    How can I replace NaN values in DataFrame from another table?

  20. 20

    How can I add a new attribute from another table to a serializer?

  21. 21

    How can i insert rows from another table in firebird?

  22. 22

    How can I add a new attribute from another table to a serializer?

  23. 23

    How can I delete multply rows from a table with another condition?

  24. 24

    Can I group-aggregate-calculate from another table directly in razor view?

  25. 25

    How to compare dates from one table with another table

  26. 26

    how to calculate average of values in a column by considering the information from another column?

  27. 27

    How to copy values from table for another column

  28. 28

    How to copy the column id from another table?

  29. 29

    Can I use a computed column in a table to fetch data from another table columns

HotTag

Archive