Count days in date range?

后端 未结 5 1692
无人及你
无人及你 2021-01-20 10:57

I have a query like this:

SELECT COUNT(*) AS amount
FROM daily_individual_tracking
WHERE sales = \'YES\'
AND daily_individual_tracking_date BETWEEN \'2010-01         


        
相关标签:
5条回答
  • 2021-01-20 11:03

    This depends on what SQL server you're using.

    If you're using MS-SQL Server, you can use the function DateDiff

    0 讨论(0)
  • 2021-01-20 11:13

    Not really clear if you are looking for

    DATEDIFF('2010-03-31', '2010-01-01')
    

    or

    COUNT(DISTINCT daily_individual_racking_date)
    
    0 讨论(0)
  • 2021-01-20 11:27

    What exactly are you trying to count? The total number of distinct values of daily_individual_tracking_date? Do you need it in the same query as the count(*) query?

    0 讨论(0)
  • 2021-01-20 11:27

    I'm not sure which SQL you are using. TSQL has a DATEDIFF that will count the number of days between two dates. See this

    0 讨论(0)
  • 2021-01-20 11:28

    You can use the MySQL datediff function:

    SELECT DATEDIFF('2010-01-01','2010-01-31') AS DiffDays
    

    It should return a floating point, where 1.0 represents a single day.

    And for MS SQL use ,

    SELECT DATEDIFF( day ,'2010-01-01','2010-01-31') AS DiffDays
    
    0 讨论(0)
提交回复
热议问题