How to get last 7 days data from current datetime to last 7 days in sql server

前端 未结 9 621
我在风中等你
我在风中等你 2020-11-30 04:57

Hi I am loading table A data from sql server to mysql using pentaho when loading data i need to get only last 7 days data from sql server A table to mysql In sql server crea

相关标签:
9条回答
  • 2020-11-30 05:01

    This worked for me!!

    SELECT * FROM `users` where `created_at` BETWEEN CURDATE()-7 AND CURDATE()
    
    0 讨论(0)
  • 2020-11-30 05:01

    Hope this will help,

    select id,    
    NewsHeadline as news_headline,    
    NewsText as news_text,    
    state,    
    CreatedDate as created_on      
    from News    
    WHERE CreatedDate >= cast(dateadd(day, -7, GETDATE()) as date)
    and CreatedDate < cast(GETDATE()+1 as date) order by CreatedDate desc
    
    0 讨论(0)
  • 2020-11-30 05:03

    DATEADD and GETDATE functions might not work in MySQL database. so if you are working with MySQL database, then the following command may help you.

    select id, NewsHeadline as news_headline,    
    NewsText as news_text,    
    state, CreatedDate as created_on    
    from News    
    WHERE CreatedDate>= DATE_ADD(CURDATE(), INTERVAL -3 DAY);
    

    I hope it will help you

    0 讨论(0)
  • 2020-11-30 05:08

    I don't think you have data for every single day for the past seven days. Days for which no data exist, will obviously not show up.

    Try this and validate that you have data for EACH day for the past 7 days

    SELECT DISTINCT CreatedDate
    FROM News 
    WHERE CreatedDate >= DATEADD(day,-7, GETDATE())
    ORDER BY CreatedDate
    

    EDIT - Copied from your comment

    i have dec 19th -1 row data,18th -2 rows,17th -3 rows,16th -3 rows,15th -3 rows,12th -2 rows, 11th -4 rows,9th -1 row,8th -1 row

    You don't have data for all days. That is your problem and not the query. If you execute the query today - 22nd - you will only get data for 19th, 18th,17th,16th and 15th. You have no data for 20th, 21st and 22nd.

    EDIT - To get data for the last 7 days, where data is available you can try

    select id,    
    NewsHeadline as news_headline,    
    NewsText as news_text,    
    state,    
    CreatedDate as created_on      
    from News    
    WHERE CreatedDate IN (SELECT DISTINCT TOP 7 CreatedDate from News
    order by createddate DESC)
    
    0 讨论(0)
  • 2020-11-30 05:12

    To pull data for the last 3 days, not the current date :

    date(timestamp) >= curdate() - 3
    AND date(timestamp) < curdate()
    

    Example:

    SELECT *
    
    FROM user_login
    WHERE age > 18
    AND date(timestamp) >= curdate() - 3
    AND date(timestamp) < curdate()
    
    LIMIT 10
    
    0 讨论(0)
  • 2020-11-30 05:16

    If you want to do it using Pentaho DI, you can use "Modified JavaScript" Step and write the below function:

    dateAdd(d1, "d", -7);  // d1 is the current date and "d" is the date identifier
    

    Check the image below: [Assuming current date is : 22 December 2014]

    enter image description here

    Hope it helps :)

    0 讨论(0)
提交回复
热议问题