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 overa开发者_Go百科ll 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.
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.
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...
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
精彩评论