473,761 Members | 9,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Indexing a View

Hi All,

I'm hoping someone can help me. I think I'm missing something very
basic. I'm trying to put a clustered index on a view that I have
created. I keep getting the error:

Server: Msg 8668, Level 16, State 1, Line 1
An index cannot be created on the view 'cew_avwage_usc nty' because the
select list of the view contains a non-aggregate expression.

Here is the create view statement:

CREATE view dbo.cew_avwage_ uscnty
with schemabinding
as
select statefips, countyfips, naics_code, sum(disc * 0) as disc,
data_year, sum(qtr_payroll )/((sum(emp1+emp2 +emp3))/12) as avwage
from dbo.qcew_own_n where year= '2004'
and (ownership = '0'
and (naics_code like '__' or naics_code like '__-__')
or (ownership = '5' and naics_code = '10'))
and countyfips <> '999'
and naics_code <> '99'
and ((disc = '0') or (disc <> '0' and (emp1 <> 0 or emp2 <> 0 or emp3 <>
0)))
group by statefips, countyfips, naics_code, data_year

Here is the create index statement I am using:

create unique clustered index main_cdx
on dbo.cew_avwage_ uscnty (statefips, countyfips, naics_code, year)

Each field that I have listed in the select statement MUST appear in the
view. The disc field needs to be set = 0 and the data_year field needs
to be set = 2004. Initially I had select statement for disc and
data_year set as, disc = '0', year = '2004' .. but thought this was
giving me my problem. I changed it and am still getting the same error?
Is my problem because of the fields that I am grouping by? Any advice
would be very much appreciated. I'm trying to avoid creating an extract
table, as these data are updated regularly - so I would like to only
update the base table and have the view refresh itself.

Thanks again!

Bethany


*** Sent via Developersdex http://www.developersdex.com ***
Dec 14 '05 #1
6 3113
instead of

sum(qtr_payroll )/((sum(emp1+emp2 +emp3))/12) as avwage

use 2 columns

sum(qtr_payroll )
sum(emp1+emp2+e mp3)

also include

count_big(*)

in the view definition

Dec 14 '05 #2
Thanks for the tips. I'm a little confused about your advice to use 2
columns. Are you saying I should break it up - sum the payroll
sum(qtr_payroll ) .. and then sum the employment sum(emp1_emp2+e mp3) and
then derive my average wage from those 2 columns? My end result is that
I need an average wage. Average wage is the quarterly payroll, divided
by the sum of the monthly employment divided by 12 ..

Sorry if I missed something obvious .. I tried breaking it up and then
deriving the average wage in a 3 column from the 2 columns, that did not
work either:

sum(qtr_payroll ) as qtr_payrroll, sum(emp1_emp2+e mp3)/12 as qtr_emp,
sum(qtr_payroll / qtr_emp) as avwage
*** Sent via Developersdex http://www.developersdex.com ***
Dec 14 '05 #3
Bethany,

I would try this indexed view definition:

sum(qtr_payroll ) sq1,
sum(emp1) se1, sum(emp2) se2, sum(emp3) se3,
count_big(*) cb

and calculate the average when you select from the view.

BTW, what are the types of emp1, emp2, emp3?
You might get an 'imprecise expression in where clause' error if they
are float.

what do you need the condition for:

(emp1 <> 0 or emp2 <> 0 or emp3 <> 0
Why are you using the expression sum(disc * 0) as disc?

Dec 14 '05 #4
Alexander,

Thanks again. I will try your suggestions. I did, however, want to
answer your questions just in case I'm making this much more difficult
that I need to. ;)

The type for emp1, emp2 & emp3 is decimal.

I need the disc field to be equal to zero. The actual values in the
disclosure field in the base table are 0, 1, or 2. Intitially I had
this field in the select statement set as disc = 0, but because the
error I was receiving, I thought perhaps it didn't like my equal (=)
sign - setting the value to 0 - so I changed it to multiply the whatever
was there by zero to give me a value of zero.

As for the condition :
and ((disc = '0') or (disc <> '0' and (emp1 <> 0 or emp2 <> 0 or emp3 <>
0)))

That's a little more complicated but here's my attempt to explain it.
In the base table we have disclosure codes of 0, 1 or 2. In the case of
records where ownership = 0 (which is another condition in my where
clause). Generally - for most things, we only want to pull and show
data where disc = 0 - however, in this case, if there is a disc <> 0 AND
there are actual data values > 0 in any of the emp fields, we want to
pull that record and create average wage. The reason I need the
condition is that there can be true zeros in a field, but I don't want
to pull back any records where disc <> 0 and there are zeros in any of
the emp field. This gives me a divide by zero error.

Does that make sense?

Thanks!
Bethany


*** Sent via Developersdex http://www.developersdex.com ***
Dec 14 '05 #5
it isn't what you asked, but I hate using indexed views. It seems like
every time I tried to use one, the stupid engine goes and recreates the
index from scratch doing a giant hash table in an incredibly
inefficient way not using any half way intelligent existing indexes.
So, if you used the indexed view inside a cursor, or in a stored
procedure that gets called repeatedly, the goofy thing would rebuild
itself each time it was called.

made things sooooo slowwwww.

to fix, I copied the really ugly code from the view directly into the
SPROC, documented the heck out of everything, and then the engine would
actually use already existing indexes to make intelligent decisions.

YMMV.

Dec 16 '05 #6
Doug,

I don't understand what you are speaking about. I see 2 other problems
with indexed views:

1. sometimes the optimizer just won't use them

2. there may be a lot of lock contention on them.

Can you provide more details?

Dec 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2745
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array (i.e., a matrix) it seems to be trivial, with array indexing, to extract a subset of its *columns*. But it does not seem to be trivial to extract a subset of its *rows*. The code snippet below describes the problem (if it really is a problem)...
3
10846
by: C | last post by:
Hi, I currently use MS Indexing Service to search my htm and aspx pages. I recently downloaded the IFilter tool from Adobe. When I now do a search it still does not pick up any text in my PDF's.
108
6450
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the /start/ and /stop/ indices and the /step/ or stride length of the slice. Missing or out-of-bounds indices are handled in a manner...
7
1819
by: Ryan | last post by:
I have a bit of a problem with regards an indexing strategy. Well, basically there is no indexing strategy on a set of data I have at work. Now, I didn't create the design as I would have allowed for this. OK, so there is primary key (clustered) indexes (mainly composite keys), but no other indexes on the tables. As you would expect, the performance leaves a lot to be desired. A hell of a lot. We have several million rows in a lot of the...
0
1268
by: Owen Jenkins | last post by:
Hi, My application allows users to create a new back end for separate purposes. It does this by using Make Table Queries and Indexing via SQL. For example ... sqlString = "SELECT * INTO " & TableName & " IN '" & NewDBName & "' FROM " & TableName & " WHERE 1=2;" DoCmd.RunSQL sqlString 'The WHERE 1=2 ensures that no records are copied - only the
4
3266
by: Owen Jenkins | last post by:
Hi, No-one replied to this when I sent it last week. Any bites out there today?? ----- My application allows users to create a new back end for separate purposes. It does this by using Make Table Queries and Indexing via SQL. For example ...
6
3676
by: 78ncp | last post by:
hi... how to implementation algorithm latent semantic indexing in python programming...?? thank's for daniel who answered my question before.. -- View this message in context: http://www.nabble.com/how-to-implementation-latent-semantic-indexing-in-python..-tf4075439.html#a11582773 Sent from the Python - python-list mailing list archive at Nabble.com.
4
1312
by: pratimapaudel | last post by:
I have tables in my database, it's sql server 2005. I heard some of them have indexing and some of them doesnot have indexing. If i want to check whether it has indexing or not how do i do? And if i want to create indexing how do i do? Do i have to write script to create indexing or i can do from desing view of management studio? if i have to write script to create indexing where do i write?
2
8107
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a server 2008 IIS 7.0 with indexing service installed. I have created the catalog and have a test page using these posts: http://kbalertz.com/954822/install-configure-Indexing-Service-Windows-Server-computer.aspx http://kbalertz.com/820105/Application-Query-Indexing-Service-Catalog-Using-Visual-Basic.aspx I can search some files .xls .doc and others but It will not find anything in .pdf
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.