Creating an indexed view using an aggregate (MAX)

Posted on 2003-03-14
Medium Priority
Last Modified: 2008-02-01
I need to create an indexed view like the following

Create view v1
With SchemaBinding
Select EmployeeID, max(EmployeeLoginDate)
From dbo.Employee
Where Employee.Status = 1
Group by EmployeeID

Create Unique Clustered Index ix_v1 on dbo.v1 (EmployeeID)

I can create the view, but when I create the index, I get an error that says "Cannot create view.  It contains one or more disallowed constructs."

Any ideas on possible workarounds?  I am using this info as part of a query that takes forever to run.
Question by:Dre_The_DBA
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 69

Expert Comment

by:Scott Pletcher
ID: 8137634
I'm not sure you'll be able to create an index on a view with MAX().  

But you could create an index on EmployeeId and Login Date; hopefully, that should solve the original query quickly enough, although if there are LOTS of rows per employee per day maybe not.
LVL 50

Expert Comment

ID: 8137800
I'm not aware that you can create indexes on views...
(SQLServer v7)
you can only create indexes on the base tables

create index ix_v1 on dbo.employee (employeeid,status,employeelogindate)

but what indexes exist on the table anyway?

i'd have thought that an existing employeeid index would
give decent results anyway...



Author Comment

ID: 8137827
With SQL2K, you can create indexes on views.  

I am not actually using an employee table, but I just created a simple table for a simple example to show what I'm trying to do.  The actual view is a lot more complex, joining different tables, etc....
LVL 69

Accepted Solution

Scott Pletcher earned 200 total points
ID: 8138403
You can create indexes on some views, but not necessarily on every view.  I thought that the MAX() prevented that view from being indexed ... but I can't 100% for sure that it does.

Expert Comment

ID: 9275858
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question