views:

260

answers:

3

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 itterate though a list of reports and dynamically generate the insert views.

A: 

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...

RedFilter
Sorry if that wasn't clear. What I mean is that in order to create the final view summary_view_all_plants, the intermidiate views like summary_view_N insert into a temporary table. Then summary_view_all_plants just outputs the contect of that table.
noel_g
@noel_g: What do you mean the views insert into a temp table.. Are you confusing terminology here?
Chris Lively
Please post your underlying table schema.
RedFilter
CREATE TABLE [ttPlantSum] ( [ReportName] [char] (80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Site] [char] (6) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Count] [numeric](18, 0) NULL ) ON [PRIMARY] GO
noel_g
I mean for the source data - this looks liek some summary table...I am getting a bit lost in what your process is and why you are doing it...
RedFilter
for better readability put answer here http://www.pastie.org/788289
noel_g
+1  A: 

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.

OMG Ponies
We do delete all rows from the temp table each time the summary_view_all_plants is run via a set of stored procedures (there is actually an ASP front end which I have left out of the discussion). Needless to say what you say about brittleness is correct and performance is less than idea. I inherited this and am trying to make the best out of it.I did not know about indexed views, but my googling tells me it may not work since the underlying data changes and some views have non-determanistic columns. Performance is not a big deal as these run once daily on a dedicated server.
noel_g
@noel_g: Yeah, sadly materialized views are notoriously unaccommodating
OMG Ponies
+1 for OMG Ponies. You have to define the tables/views, and once you've done it, you can't change any of the underlying tables or views without recompiling the summary view.
Brent Ozar
A: 

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

noel_g