Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select within a select problem

Status
Not open for further replies.

paulosinuk

IS-IT--Management
Feb 8, 2001
105
AU
Hi,

Here's my issue.

I have a table that contains current discounts on products by centre. Table structure

CentreNumber - branch/site/centre
ProductID - Product on discount
EffectiveDate - The date that discount becomes available
SMOffers - contains Tier 1 discounts
SDOffers - contains Tier 2 discounts
RMOffers - contains Tier 3 discounts

I want to be able to pull the average discount for all products within a centre for the maximum effective date. That should give me the average discounts at this point in time.

I'm trying to use the following select statement in SQL server 2000 query analyser but get a very naff error message.

SELECT CentreNumber, MAX(EffectiveDate) AS Effect

(SELECT avg(SMOffers) FROM PRICINGAPP_ProductOffers WHERE CentreNumber=C.CentreNumber and effectivedate >= C.Effect) as AvgTier1

FROM PRICINGAPP_ProductOffers as C
group BY CentreNumber, Effect


I get the error message:

Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'as'.

Any help would be greatly appreciated.

Thanks

Paul
 
First off, I see a comma missing:

SELECT CentreNumber, MAX(EffectiveDate) AS Effect,
(SELECT avg(SMOffers) FROM PRICINGAPP_ProductOffers WHERE CentreNumber=C.CentreNumber and effectivedate >= C.Effect) as AvgTier1

FROM PRICINGAPP_ProductOffers as C
group BY CentreNumber, Effect

Try that (comma after Effect and before (SELECT...)and let us know if it worked.

-SQLBill
 
Hi,

We're making progress but now getting the error

Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'Effect'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'Effect'.

Thanks

Paul
 
Try taking 'Effect' out of your GROUP BY clause.

-dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top