Solved

# SQL Scripts

Posted on 2009-04-29
207 Views
Hi, I am trying to show the books that cost more than the average book price using variable. I am using Microsoft SQL Server 2005. I am getting the following error message:

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'from'.

``````DECLARE @X int;

SELECT @X = AVG(BKListPirce);

from tblbooks

WHERE BKListPrice < AVG(BKListPirce)

go
``````
0
Question by:iolike

LVL 75

Expert Comment

DECLARE @X int;
SELECT @X = AVG(BKListPirce)
from tblbooks
0

LVL 39

Expert Comment

DECLARE @X int;
SELECT @X = AVG(BKListPirce)
from tblbooks

WHERE BKListPrice < AVG(BKListPirce)

Remove ";"
0

LVL 39

Expert Comment

books that cost more than the average book price

you need to do this query
SELECT @X = AVG(BKListPirce);
from tblbooks

WHERE BKListPrice < AVG(BKListPirce)

Can you specify what exactly you want in variable , becuase abobe query may have multiple values so can't store in variable
0

Author Comment

1)I want first to declare the variable ex. @x
2) set @x to equal the average of book price

3) Have a restriction where the result only shows prices that are higher than the average price.  Does this make any sense?
0

LVL 39

Expert Comment

you are saying set @x to equal the average of book price
and also result only shows prices that are higher than the average price ?

Does you mean to say
You want to take the AVG of prices that are higher than the actual  average price

EX
Prices
10
20
30
40
50

Now Avg is is 30
Higher prices are
40 and 50
so you want result 45 ........is it ?

then try this

DECLARE @X int;
SELECT @X = AVG(BKListPirce)
from tblbooks

WHERE BKListPirce >( Select AVG(BKListPirce) from tblbooks)
0

Author Comment

No, I want to show the results 40 and 50
0

LVL 39

Accepted Solution

then you can't store it in one variable ...correct ?

SELECT BKListPirce
from tblbooks
WHERE BKListPirce >( Select AVG(BKListPirce) from tblbooks)
0

Author Comment

This is the error message I am getting
Msg 207, Level 16, State 1, Line 5
Invalid column name 'BKListPirce'.
Msg 207, Level 16, State 1, Line 5
Invalid column name 'BKListPirce'.
Msg 207, Level 16, State 1, Line 2
Invalid column name 'BKListPirce'.
0

LVL 39

Expert Comment

I have used the same name as you given "BKListPirce"

check the fieldname in database , may be it mis-spelled

BKListPrice
0

Author Comment

Pratima, Thanks I had mis-spelling in there.

Now it is working
0

## Featured Post

### Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…