Calculating the sum of the quantities of one table based on dates in another table in sql

Steven01123581321

Suppose I have a first table like this:

tbl1:

 item     date1         date2
   1    2020-06-21    2020-06-28
   2    2020-05-13    2020-05-24
   3    2020-06-20    2020-06-28

I also have a second table with the items, a date and a quantity

tbl2:

 item     quantity       date
   1         5        2020-06-24
   1         8        2020-06-20
   1         12       2020-06-27
   1         9        2020-06-29
   2         10       2020-05-24
   2         11       2020-05-15
   2         18       2020-05-18
   2         9        2020-05-14
   3         7        2020-06-18
   3         12       2020-06-21
   3         13       2020-06-24
   3         8        2020-06-28

Now I want to sum the quantities from tbl2 where the date is between the columns date1 and date2. So my result would look like:

tbl1:

 item     date1         date2        sum
   1    2020-06-21    2020-06-28     17
   2    2020-05-13    2020-05-24     48
   3    2020-06-20    2020-06-28     33

I've been starring at it for a while now and I really want to avoid a loop.

Is there an efficient way of obtaining the desired result??

Charlieface

You can just join them, group by table1 and sum:

select t1.*, sum(t2.quantity)
from table1 t1;
left join table2 t2 on t2.item = t1.item and
    t2.date >= t1.date1 and
    t2.date <= t1.date2
group by t1.item, t1.date1, t1.date2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculating the sum of the quantities of one dataframe based on dates in another dataframe (Python)

From Dev

Update mysql one table from another table based on dates

From Dev

How to select a quantity of rows from one table based on quantities from another

From Dev

Set two values in one table based on sum of a column in another table

From Dev

SQL calculating sum based on another column

From Dev

SQL - Selection in one table based on matching values given in another table

From Dev

link a value from one table to another and slice one table based on columns from another table in sql

From Dev

How to SUM from another table in SQL in one view?

From Dev

How to sum a specific row from one table to another in SQL Server

From Dev

How to Get Sum of One Column Based On Other Table in Sql Server

From Dev

Query for how to join two tables with sum oparation in one table condition based on another table?

From Dev

Sum data from a table between dates in another table

From Dev

Getting data from one table based on results of another SQL PHP

From Dev

Calculating sum(column) based on other column values in the same table

From Dev

SQL Server : filter on dates from another table

From Dev

How to compare dates from one table with another table

From Dev

UPDATE one SQL table based on equality of two columns with two columns from another table

From Dev

SQL: How to query whether a record exists in one table based on values in another table

From Dev

Select data from one table & then rename the columns based on another table in SQL server

From Dev

Joining row from one table with the sum value from another table

From Dev

Group by one table and sum from another table in Linq

From Dev

Use VBA to make pivot table sum values based on dates only?

From Dev

SQL computed column for sum of data in another table

From Dev

SQL: Sum and groupby, criteria for groupby in another table

From Dev

SQL Server Sum table fields based on params

From Dev

Updating values in one table based on another

From Dev

Excel - sum values based on a column that match another column in another table

From Dev

How do I insert a value obtained from a sql sum from one table into another in vb?

From Dev

How to update one table based on aggregate query form another table

Related Related

  1. 1

    Calculating the sum of the quantities of one dataframe based on dates in another dataframe (Python)

  2. 2

    Update mysql one table from another table based on dates

  3. 3

    How to select a quantity of rows from one table based on quantities from another

  4. 4

    Set two values in one table based on sum of a column in another table

  5. 5

    SQL calculating sum based on another column

  6. 6

    SQL - Selection in one table based on matching values given in another table

  7. 7

    link a value from one table to another and slice one table based on columns from another table in sql

  8. 8

    How to SUM from another table in SQL in one view?

  9. 9

    How to sum a specific row from one table to another in SQL Server

  10. 10

    How to Get Sum of One Column Based On Other Table in Sql Server

  11. 11

    Query for how to join two tables with sum oparation in one table condition based on another table?

  12. 12

    Sum data from a table between dates in another table

  13. 13

    Getting data from one table based on results of another SQL PHP

  14. 14

    Calculating sum(column) based on other column values in the same table

  15. 15

    SQL Server : filter on dates from another table

  16. 16

    How to compare dates from one table with another table

  17. 17

    UPDATE one SQL table based on equality of two columns with two columns from another table

  18. 18

    SQL: How to query whether a record exists in one table based on values in another table

  19. 19

    Select data from one table & then rename the columns based on another table in SQL server

  20. 20

    Joining row from one table with the sum value from another table

  21. 21

    Group by one table and sum from another table in Linq

  22. 22

    Use VBA to make pivot table sum values based on dates only?

  23. 23

    SQL computed column for sum of data in another table

  24. 24

    SQL: Sum and groupby, criteria for groupby in another table

  25. 25

    SQL Server Sum table fields based on params

  26. 26

    Updating values in one table based on another

  27. 27

    Excel - sum values based on a column that match another column in another table

  28. 28

    How do I insert a value obtained from a sql sum from one table into another in vb?

  29. 29

    How to update one table based on aggregate query form another table

HotTag

Archive