在 Oracle 中存储每周和每月聚合

发布于 2024-12-28 11:55:02 字数 363 浏览 0 评论 0原文

我需要动态更新每个产品和客户的每周和每月销售数据。这些需要在产品销售过程中进行更新和检查,并且由于各种原因,我无法为此使用存储过程或物化视图(我将把所有内容读入应用程序,修改内存中的所有内容,然后更新和提交结果)。

保存一段时间内销售的最佳表格结构是什么?

  • 存储期间类型(月、周)以及开始日期和结束日期,还是仅存储类型和开始日期?
  • 使用日期字段和字符,或将其编码为字符串(“M201201”/“W201248”)
  • 将销售额和期间标准化为两个表,还是将销售额和期间保留在一个表中?

我将执行两种查询 - 选择当前每周(x或每月)周期/客户/文章的销售额但不更新它们,并选择更新客户/文章的每周和每月周期。

I need to dynamically update weekly and monthly sales data per product and customer. These need to be updated and checked during the sale of a product, and for various reasons I'm not able to use stored procedures or materialized views for this (I'll read everything into the application, modify everything in memory and then update and commit the results).

What is the best table structure for holding the sales during a period?

  • Store the period type (M, W) with start and end dates, or just the type and start date?
  • Use date fields and a char, or code it into a string ('M201201' / 'W201248')
  • Normalize sales and periods into two tables, or keep both sales and the period in a single table?

I will be doing two kinds of queries - select the sales of the current weekly (xor monthly) period/customer/article but not update them, and select for update weekly and monthly periods for a customer/article.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2025-01-04 11:55:02

如果您将适用期间的开始日期和结束日期都存储在行中,那么您的检索查询将会容易得多,至少是基于单个日期(例如今天)的检索查询。这是一种非常典型的访问模式,因为您可能会从给定日期发生的业务交易(例如特定销售)的角度来看待事物。

where @date_of_interest >= start_date and @date_of_interest <= end_date非常直接和简单。任何其他组合都要求您在进行查询之前或在查询本身中在代码中进行日期算术。

保留类型代码(M、W)以及开始日期和结束日期需要引入一些冗余。但是,您可能会选择引入这种冗余以简化数据检索。这个:where @date_of_interest >= start_date and @date_of_interest <= end_date and range_type='M'也非常直接和简单。

与所有非规范化一样,您需要确保拥有能够管理这种冗余的控制措施。

If you store both the start and end dates of the applicable period in the row, then your retrieval queries will be much easier, at least the ones that are based on a single date (like today). This is a very typical mode of access since you are probably going to be looking at things from the perspective of a business transaction (like a specific sale) which happens on a given date.

It is very direct and simple to say where @date_of_interest >= start_date and @date_of_interest <= end_date. Any other combination requires you to do date arithmetic either in code before you go to your query or within your query itself.

Keeping a type code (M, W) as well as both start and end dates entails introducing some redundancy. However, you might choose to introduce this redundancy for the sake of easing data retrieval. This: where @date_of_interest >= start_date and @date_of_interest <= end_date and range_type='M' is also very direct and simple.

As with all denormalization, you need to ensure that you have controls that will manage this redundancy.

为你鎻心 2025-01-04 11:55:02

我建议您为此目的使用规范化模式,将每周和每月聚合存储在两个具有相同结构的不同表中。我不知道您要执行哪种查询,但我认为这将使进行任何类型的搜索变得更容易(当以正确的方式完成时!!!)。

可能像这个样本

product_prices (
    prod_code,
    price,
    date_price_begin
);

sales (
    prod_code,
    customer_code,
    sale_date
);


<aggregate-week>
select trunc(sale_date,'w') as week,
    prod_code,
    customer_code,
    sum(price) keep (dense_rank first order by date_price_start) as price
from sales 
    natural join product_prices
where sale_date > date_from
group by trunc(sale_date,'iw'),
    prod_code,
    customer_code
/

<aggregate-month>
select trunc(sale_date,'m') as month,
    prod_code,
    customer_code,
    sum(price) keep (dense_rank first order by date_price_start) as price
from sales 
    natural join product_prices
where sale_date > date_from
group by trunc(sale_date,'m'),
    prod_code,
    customer_code
/

I would recommend you to use a normalized schema for that purpose where you store weekly and monthly aggregation in two different tables with the same structure. I don't know the kind of queries you're going to do, but I suppose that this would make it easier to do any sort of search (when it's done in the right way!!!).

Probably something like this sample

product_prices (
    prod_code,
    price,
    date_price_begin
);

sales (
    prod_code,
    customer_code,
    sale_date
);


<aggregate-week>
select trunc(sale_date,'w') as week,
    prod_code,
    customer_code,
    sum(price) keep (dense_rank first order by date_price_start) as price
from sales 
    natural join product_prices
where sale_date > date_from
group by trunc(sale_date,'iw'),
    prod_code,
    customer_code
/

<aggregate-month>
select trunc(sale_date,'m') as month,
    prod_code,
    customer_code,
    sum(price) keep (dense_rank first order by date_price_start) as price
from sales 
    natural join product_prices
where sale_date > date_from
group by trunc(sale_date,'m'),
    prod_code,
    customer_code
/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文