SQL Input Date Range to work out Rate per day

DanF

Hi can anyone help me with the T-SQL query required to produce a rate per day based on two tables as below:

I have one table containing ranges:

FromDate   | ToDate
2017-06-20   2017-07-01

and one table containing rates (This table is not limited to 2 records):

FromDate     | Rate
2017-06-11     400  
2017-06-24     1200

Based on the range from the first table I need to produce a result set as follows which returns the rate for each date in the input range:

Date        |  Rate
2017-06-20     400
2017-06-21     400
2017-06-22     400
2017-06-23     400
2017-06-24     1200
2017-06-25     1200
2017-06-26     1200
2017-06-27     1200
2017-06-28     1200
2017-06-29     1200
2017-06-30     1200
2017-07-01     1200

I will then need to sum the rate in a later step to produce a total.

I would really appreciate any help you can provide.

Gordon Linoff

A pretty simple method is a recursive CTE and apply:

with cte as (
      select fromdate as dte, todate
      from table1
      union all
      select dateadd(day, 1, fromdate), todate
      from cte
     )
select dte, t2.rate
from cte cross apply
     (select top 1 t2.rate
      from table2 t2
      where t2.fromdate <= cte.dte
      order by t2.dte desc
     ) t2;

There are definitely other approaches. This will work on ranges up to 100 days long by default (you can use the maximum recursion option to get more).

If you have more data, a numbers table of some sort is helpful.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

visitors per day graph with multiple array input

分類Dev

MySQL select all days in date range even if no data is available for day

分類Dev

Get the maximum repeated row per day in oracle sql

分類Dev

SQL Get date of first day of previous week

分類Dev

SQL - How to declare date range in a temp table?

分類Dev

SQL find count within date range

分類Dev

SQL: Get users not loggedIn in given date range

分類Dev

Detect and merge date range successive overlaps in SQL

分類Dev

SQL: Calculate day-1 retention rate from user registration table and event log

分類Dev

Convert a Varchar/String with day name to Date in Sql Server

分類Dev

SQL Server determining the day from an existing date column

分類Dev

How to get date from week number and day in sql?

分類Dev

need help in sql database to retrieve day from date

分類Dev

Visualise summary statistics per day, per group

分類Dev

Azure Bot Service using over 1GB of data transfer out per day. Why? How can I stop that?

分類Dev

subtract last data from first data in ms sql but doing it per day

分類Dev

SQL Server - find people between date range excluding the year

分類Dev

SQL Create dates between date range for multiple dimensions

分類Dev

SQL: find records having data for each month in a given date range

分類Dev

PL/SQL function to calculate average monthwise for a given date range

分類Dev

PostgreSQL: How change date_trunc week start day from monday to another day in sql query?

分類Dev

SQL Server - Deleting rows between a date range using SQL. Date conversion fails

分類Dev

Mongoose unique value per day

分類Dev

SQL query doesnt bring out results when queried using date

分類Dev

Every JQuery / Javascript Number formating doesn't work, Parsed out of Range

分類Dev

Copying data from a file and changing the target every day using a date input

分類Dev

Mysql exception"out of range"?

分類Dev

IndexOutOfRangeException, but there is no out of range

分類Dev

SQL Find Where Date Falls in Table Based on Date Range in Another Table

Related 関連記事

  1. 1

    visitors per day graph with multiple array input

  2. 2

    MySQL select all days in date range even if no data is available for day

  3. 3

    Get the maximum repeated row per day in oracle sql

  4. 4

    SQL Get date of first day of previous week

  5. 5

    SQL - How to declare date range in a temp table?

  6. 6

    SQL find count within date range

  7. 7

    SQL: Get users not loggedIn in given date range

  8. 8

    Detect and merge date range successive overlaps in SQL

  9. 9

    SQL: Calculate day-1 retention rate from user registration table and event log

  10. 10

    Convert a Varchar/String with day name to Date in Sql Server

  11. 11

    SQL Server determining the day from an existing date column

  12. 12

    How to get date from week number and day in sql?

  13. 13

    need help in sql database to retrieve day from date

  14. 14

    Visualise summary statistics per day, per group

  15. 15

    Azure Bot Service using over 1GB of data transfer out per day. Why? How can I stop that?

  16. 16

    subtract last data from first data in ms sql but doing it per day

  17. 17

    SQL Server - find people between date range excluding the year

  18. 18

    SQL Create dates between date range for multiple dimensions

  19. 19

    SQL: find records having data for each month in a given date range

  20. 20

    PL/SQL function to calculate average monthwise for a given date range

  21. 21

    PostgreSQL: How change date_trunc week start day from monday to another day in sql query?

  22. 22

    SQL Server - Deleting rows between a date range using SQL. Date conversion fails

  23. 23

    Mongoose unique value per day

  24. 24

    SQL query doesnt bring out results when queried using date

  25. 25

    Every JQuery / Javascript Number formating doesn't work, Parsed out of Range

  26. 26

    Copying data from a file and changing the target every day using a date input

  27. 27

    Mysql exception"out of range"?

  28. 28

    IndexOutOfRangeException, but there is no out of range

  29. 29

    SQL Find Where Date Falls in Table Based on Date Range in Another Table

ホットタグ

アーカイブ