开发者

Update records if new data since a specific stored procedure was last run?

I'm not sure if this is practically possible with SQL (I'm using MS SQL 2005) but what I'm trying to do is to create a summary table of customer transactions which would be updated weekly or so - a very simplified version of this would look like this:

+-----------------+----------------+----------------+----------------+----------------+
+   Customer_id   + 2007 purchases + 2008 purchases + 2009 purchases + 2010 purchases +
+-----------------+----------------+----------------+----------------+----------------+
+   id_001        +       0        +       7        +       10       +       50       +
+-----------------+----------------+----------------+----------------+----------------+
+   id_002        +       0        +       0        +       5        +       20       +
+-----------------+----------------+----------------+----------------+----------------+
+   id_003        +       100      +       0        +       0        +       0        +
+-----------------+----------------+----------------+----------------+----------------+

Within my table I have around 7 million customers so I don't want to have to recreate this each week if their data hasn't changed within the last week.

Idea开发者_如何学Pythonlly I would like to determine whether or not customer purchase data has changed since the last time the stored procedure was run (and i would only be interested in cases where there has been a new purchase since running that named procedure, therefore ignoring all other ones), and then only apply the update statement to those customers.

I was thinking along the lines of doing something like applying this as a filter:

WHERE EXISTS (SELECT customer_id 
FROM purchases WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, purchase_date)) > DATEADD(dd, -8, DATEDIFF(dd, 0, getdate()))

To pick up new purchases within the last week, but if the frequency of the stored procedures changes, this will need to be altered as well, so if I can filter for records only updated since last occurence of the stored procedure that would be best!

Thanks!

Actual Table Structure - simplified

CREATE TABLE
(
id UNIQUEIDENTIFIER
,customer_id NVARCHAR(255)
,date_received DATETIME
,value DECIMAL(18,3)
)

So I am using what amount to a pivot query to include information for each year for each customer in the sample table above (which works fine) and is part of a more complex query. The query is quite slow when applied to all customers, and so I'm fundamentally looking for a way to only run it within a stored procedure if the output is going to differ from what's already in the table!


why don't you just store the date when the report is run? that way your query doesn't have to change, you just take the difference between 'today' and the last time the stored proc was run.

edit I would add another table to the database where you simply store the date when you run the report. you can simply populate this table from the code that requests the report, or from the stored procedure that generates it. However, you may not need the entire history of report runs, so you might be able to get away with storing just the last date when the report was run (this would obviously also save you some space in the database).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜