sql - Creating a query that checks for new data every day -
i wondering if on issue i'm having automating query in sql server 2008. goal pull relevant customer data across multiple tables in db using "customer_id" link tables @ clause. in addition, i'd automate query runs every day, pulling information entered previous day , exporting excel file, apprehended collected information.
now here have far:
select distinct [table1].customer_id "customer id", [table2].customer_room "room", [table3].last_name "last", [table3].first_name "first", [table3].sex "sex", [table3].age "age", [table4].check_in_time "checkin", [table4].check_out_time "checkout", [table4].hotel_location "hotel", [table4].hotel_id "hotel id", [table5].plane_arrival "plane arrival", [table5].plane_depart "plane departure", [table6].log_create_time [table1] inner join [table2] on [table2].customer_id =[table1].customer_id inner join [table3] on [table3].customer_id =[table1].customer_id inner join [table4] on [table4].customer_id =[table1].customer_id inner join [table5] on [table5].customer_id =[table1].customer_id inner join [table6] on [table6].customer_id =[table1].customer_id [hotel_id] '02%' , datepart(year, [table6].log_create_time)=2015 , datepart(month[table6].log_create_time) between 04 , 05 , [table4].hotel_location= '003' the above statement pulls information need returns duplicate information in cases , nudge in right direction how should edit datepart(log_create_time) portion @ end it's looking new entries came in previous day. of now, i'm pulling entire months test query.
as automation, plan on using sql server 2008's export wizard point excel file , telling sql server agent run ssis package every night issue i'm seeing right approach overwrites previous information contained within excel file. there no way apprehend new information file?
to answer datepart question, try this:
and [table6].log_create_time between convert(date, getdate() -1) , convert(date, getdate()) just remove month , year datepart statements. return every record log_create_time starting midnight yesterday midnight today. should solve duplicate data issue assuming few things:
- you run script once day, no more.
- you not run script @ midnight while records inserting log @ exact time.
- new records entered table6 use getdate feature log_create_time , value not changed after insertion.
- you change cross joins else. cross joins trouble , don't think they're giving think are. use inner joins instead.
you may consider tracking records have been exported using bit field, , changing query pull records have not yet been exported.
Comments
Post a Comment