How to calculate count of records between several dates

user3652316

I have a table that looks like:

date         name     lookback_date
1995-01-31   T1       1994-10-24
1995-01-30   T1       1994-10-23
1995-01-29   T1       1994-10-22
1995-01-31   T2       1994-10-24
1995-01-30   T2       1994-10-23
1995-01-29   T2       1994-10-22
1995-01-31   T3       1994-10-24
1995-01-30   T3       1994-10-23
1995-01-29   T3       1994-10-22

and so on

I need to calculate count of names between each record's date and lookback_date

edit: i need a result looks like that:

 date         name     lookback_date  cnt
1995-01-31   T1       1994-10-24     70
1995-01-30   T1       1994-10-23     69
1995-01-29   T1       1994-10-22     67
1995-01-31   T2       1994-10-24     73
1995-01-30   T2       1994-10-23     65
1995-01-29   T2       1994-10-22     63
1995-01-31   T3       1994-10-24     68
1995-01-30   T3       1994-10-23     66
1995-01-29   T3       1994-10-22     65

input date is aquired with this statement

select date, ticker_name
        ,dateadd(days, -100, date) as lookback_date
from table1
Angelo Fuchs

Try this (I haven't tried it)

SELECT t1.date, t1.ticker_name, dateadd(days, -100, date) AS lookback_date,
    (SELECT count(ticker_name) AS cnt
     FROM table1
     WHERE date > t1.date
           AND date < dateadd(days, -100, date))
FROM table1 t1

It uses a subselect in the SELECT clause that should do it, but needs optimization.

Basically you want to use two selects that reference each other, one that does the date range selection and one that does the counting. Then join them either in the FROM clause or in the SELECT clause.

The FROM clause would be better as it is better readable and thus better maintainable.

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 to calculate count of records by 2 columns

From Dev

How to get records between dates that are on different rows?

From Dev

How to get records between dates that are on different rows?

From Dev

How to calculate days between two dates,

From Dev

How to count the elapsed nights between two dates?

From Dev

How to count the number of sundays between two dates

From Dev

How to count fractional days between 2 dates

From Dev

How to count the number of sundays between two dates

From Dev

How to count and sum a field between 2 dates?

From Dev

SQL Count between dates

From Dev

How to calculate rolling correlations between several columns and one column in rollapply?

From Dev

How to Fetch Records from table in Sql between two dates

From Dev

How To get Records between two dates Asp.net

From Dev

Access SQL: How to find duplicate records between dates

From Dev

How to create dynamically records for each month between range of dates

From Dev

How to calculate the number of months between two dates in C#

From Dev

How can I calculate the number of working days between two dates

From Java

How to calculate number of days between two given dates?

From Java

How to calculate the difference between two dates using PHP?

From Dev

rails How to calculate time difference with integer between 3 dates

From Dev

How to calculate number of leaves taken in a month between two dates?

From Dev

how to calculate age or difference between two dates in java

From Dev

How to calculate the difference between dates in R for each unique id

From Dev

Python How to calculate number of days between 2 dates?

From Dev

how to calculate total number of saturday and sunday between two dates in mysql

From Dev

How to calculate the difference between 2 dates properly php

From Dev

How to calculate the number of days between two given dates

From Dev

How to calculate the number of days between two given dates in processing 3?

From Dev

How to calculate no of weeks , days past between two dates

Related Related

  1. 1

    How to calculate count of records by 2 columns

  2. 2

    How to get records between dates that are on different rows?

  3. 3

    How to get records between dates that are on different rows?

  4. 4

    How to calculate days between two dates,

  5. 5

    How to count the elapsed nights between two dates?

  6. 6

    How to count the number of sundays between two dates

  7. 7

    How to count fractional days between 2 dates

  8. 8

    How to count the number of sundays between two dates

  9. 9

    How to count and sum a field between 2 dates?

  10. 10

    SQL Count between dates

  11. 11

    How to calculate rolling correlations between several columns and one column in rollapply?

  12. 12

    How to Fetch Records from table in Sql between two dates

  13. 13

    How To get Records between two dates Asp.net

  14. 14

    Access SQL: How to find duplicate records between dates

  15. 15

    How to create dynamically records for each month between range of dates

  16. 16

    How to calculate the number of months between two dates in C#

  17. 17

    How can I calculate the number of working days between two dates

  18. 18

    How to calculate number of days between two given dates?

  19. 19

    How to calculate the difference between two dates using PHP?

  20. 20

    rails How to calculate time difference with integer between 3 dates

  21. 21

    How to calculate number of leaves taken in a month between two dates?

  22. 22

    how to calculate age or difference between two dates in java

  23. 23

    How to calculate the difference between dates in R for each unique id

  24. 24

    Python How to calculate number of days between 2 dates?

  25. 25

    how to calculate total number of saturday and sunday between two dates in mysql

  26. 26

    How to calculate the difference between 2 dates properly php

  27. 27

    How to calculate the number of days between two given dates

  28. 28

    How to calculate the number of days between two given dates in processing 3?

  29. 29

    How to calculate no of weeks , days past between two dates

HotTag

Archive