Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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

0
iolike
Asked:
iolike
  • 5
  • 4
1 Solution
 
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
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!

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

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now