SQL Scripts

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

Open in new window

iolikeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
DECLARE @X int;
SELECT @X = AVG(BKListPirce)
from tblbooks
0
Pratima PharandeCommented:
DECLARE @X int;
SELECT @X = AVG(BKListPirce)
from tblbooks
 
WHERE BKListPrice < AVG(BKListPirce)
 
Remove ";"
0
Pratima PharandeCommented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

iolikeAuthor Commented:
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
Pratima PharandeCommented:
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 ?
These are two contradictory statements

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
iolikeAuthor Commented:
No, I want to show the results 40 and 50
0
Pratima PharandeCommented:
then you can't store it in one variable ...correct ?

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
iolikeAuthor Commented:
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
Pratima PharandeCommented:
I have used the same name as you given "BKListPirce"

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

BKListPrice
0
iolikeAuthor Commented:
Pratima, Thanks I had mis-spelling in there.

Now it is working
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.