如何从多个视图创建摘要视图?

发布于 2024-08-18 05:22:37 字数 566 浏览 2 评论 0原文

鉴于我有以下视图,其中 N=1..100

detail_view_N

Pant  Quantity Fieldx ...
A     20          
A     13
B     4

目前我们有汇总视图,这些视图插入到类似的表中

summary_view_N

Report_Name     Plant  Count
summary_view_1  A      2
summary_view_1  B      1

然后使用表来创建总体摘要,例如

summary_view_all_plants

Report_Name     Plant   Count
summary_view_1  A       2
summary_view_1  B       1
...
summary_view_N  X       Y

有没有办法创建 summary_view_all_plants 而无需必须创建每个单独的summary_view_N?我希望能够迭代报告列表并动态生成插入视图。

Given I have the following view where N=1..100

detail_view_N

Pant  Quantity Fieldx ...
A     20          
A     13
B     4

Currently we have summary views which insert to a table like

summary_view_N

Report_Name     Plant  Count
summary_view_1  A      2
summary_view_1  B      1

The table is then used to create an overall summary like

summary_view_all_plants

Report_Name     Plant   Count
summary_view_1  A       2
summary_view_1  B       1
...
summary_view_N  X       Y

Is there a way to create summary_view_all_plants without having to create each individual summary_view_N? I would like to be able to iterate though a list of reports and dynamically generate the insert views.

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

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

发布评论

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

评论(3

快乐很简单 2024-08-25 05:22:37

有没有办法创建summary_view_all_plants而不必创建每个单独的summary_view_N?

不 - 您必须定义视图使用的所有表和/或视图。

对视图进行分层并不是理想的做法。创始观点可能会改变,从而破坏相关的孩子。查询本身存在未针对性能进行优化的风险。

将值插入临时表意味着您必须删除现有记录或使用逻辑来相应地添加或更新。这也意味着必须定期执行此操作以保持同步。索引视图(又名物化视图)可能是一个潜在的解决方案。

Is there a way to create summary_view_all_plants without having to create each individual summary_view_N?

No - you have to define all the tables and/or views utilized by a view.

Layering the views is not an ideal practice. A founding view can change, breaking related children. The queries themselves risk not being optimized for performance.

Inserting values into a temp table means you have to remove existing records or employ logic to add or update accordingly. It also means that this has to be performed periodically to keep in sync. An indexed view, AKA materialized view, might be a potential solution.

一场信仰旅途 2024-08-25 05:22:37

我不确定插入视图是什么意思。

也许您正在寻找这样的查询?

select Plant, count(*) as Count
from MyTable
group by Plant
order by Plant

我假设您的所有摘要视图都访问同一个表 MyTable。如果不是这种情况,您可能需要一个带有一些动态 SQL 的存储过程来生成您正在寻找的内容......

I am not sure what you mean by insert view.

Perhaps a query like this is what you are looking for?

select Plant, count(*) as Count
from MyTable
group by Plant
order by Plant

I am assuming all of your summary views are accessing the same table, MyTable. If this is not the case, you will likely need a stored procedure with some dynamic SQL to generate what you are looking for...

与之呼应 2024-08-25 05:22:37

找到了一个很好的解决办法。我有一个表,其中保存了我想要总结的视图的名称(detail_view_Names)。我思考每个报告名称并构建一个查询来执行每个报告的摘要。

DECLARE @REPORT_ID nvarchar(50),
    @sqlCommand varchar(1000)

DECLARE REPORT_cursor CURSOR
    FOR SELECT Report_Name
    FROM detail_view_Names

OPEN REPORT_cursor
FETCH NEXT FROM REPORT_cursor INTO @REPORT_ID

WHILE @@FETCH_STATUS = 0
BEGIN

    SET @sqlCommand = 'SELECT ''' + @Report_ID + ''' AS ReportName, Plant, COUNT(*) AS [Count] FROM dbo.' + @Report_ID + ' GROUP BY Plant'
    EXEC (@sqlCommand)

    FETCH NEXT FROM REPORT_cursor INTO @REPORT_ID

END
CLOSE REPORT_cursor
DEALLOCATE REPORT_cursor

要将新报告添加到摘要中,只需将新报告添加到detail_view_Names

Found a good solution to this. I have a table which holds the names of the views which I want to summarize (detail_view_Names). I go thought each Report Name and build a query that performs the summary of each report.

DECLARE @REPORT_ID nvarchar(50),
    @sqlCommand varchar(1000)

DECLARE REPORT_cursor CURSOR
    FOR SELECT Report_Name
    FROM detail_view_Names

OPEN REPORT_cursor
FETCH NEXT FROM REPORT_cursor INTO @REPORT_ID

WHILE @@FETCH_STATUS = 0
BEGIN

    SET @sqlCommand = 'SELECT ''' + @Report_ID + ''' AS ReportName, Plant, COUNT(*) AS [Count] FROM dbo.' + @Report_ID + ' GROUP BY Plant'
    EXEC (@sqlCommand)

    FETCH NEXT FROM REPORT_cursor INTO @REPORT_ID

END
CLOSE REPORT_cursor
DEALLOCATE REPORT_cursor

To add a new report to the summary, just add a new report to the detail_view_Names

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文