dbTalk Databases Forums  

An aggregate may not appear in the WHERE clause

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss An aggregate may not appear in the WHERE clause in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
TEKJunky
 
Posts: n/a

Default An aggregate may not appear in the WHERE clause - 09-02-2010 , 02:25 PM






I am a novice at SQL and cannot figure out an effective way to export
a product file that only has products over 25% margin.

SELECT
p.ProductCode AS 'Mfr Part Number'
, p.ProductName AS 'Title'
, pd.ProductDescriptionShort AS [stripHTML-Description]
, pe.ProductManufacturer AS 'Brand'
, pe.UPC_code AS 'UPC'
, pe.ProductPrice AS 'Price'
, pe.ProductWeight AS 'Shipping Weight'
, 'Config_FullStoreURLProductDetails.asp?ProductCode ='
+ p.ProductCode + '&click=53249' AS 'Link'
, 'Config_FullStoreURLConfig_ProductPhotosFolder/'
+ p.ProductCode + '-2.jpg' AS 'Image'

FROM Products p
INNER JOIN Products_Descriptions pd ON p.ProductID = pd.ProductID
INNER JOIN Products_Extended pe ON pd.ProductID = pe.ProductID

WHERE (p.IsChildOfProductCode is NULL OR p.IsChildOfProductCode = '')
AND (p.HideProduct is NULL OR p.HideProduct <> 'Y')
AND (pe.ProductPrice > 0 AND pe.ProductPrice IS NOT NULL)
AND (SUM(pe.Vendor_Price/pe.ProductPrice) > '.25')

ORDER BY p.ProductCode

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: An aggregate may not appear in the WHERE clause - 09-02-2010 , 03:54 PM






TEKJunky wrote:
Quote:
I am a novice at SQL and cannot figure out an effective way to export
a product file that only has products over 25% margin.

SELECT
p.ProductCode AS 'Mfr Part Number'
, p.ProductName AS 'Title'
, pd.ProductDescriptionShort AS [stripHTML-Description]
, pe.ProductManufacturer AS 'Brand'
, pe.UPC_code AS 'UPC'
, pe.ProductPrice AS 'Price'
, pe.ProductWeight AS 'Shipping Weight'
, 'Config_FullStoreURLProductDetails.asp?ProductCode ='
+ p.ProductCode + '&click=53249' AS 'Link'
, 'Config_FullStoreURLConfig_ProductPhotosFolder/'
+ p.ProductCode + '-2.jpg' AS 'Image'

FROM Products p
INNER JOIN Products_Descriptions pd ON p.ProductID = pd.ProductID
INNER JOIN Products_Extended pe ON pd.ProductID = pe.ProductID

WHERE (p.IsChildOfProductCode is NULL OR p.IsChildOfProductCode = '')
AND (p.HideProduct is NULL OR p.HideProduct <> 'Y')
AND (pe.ProductPrice > 0 AND pe.ProductPrice IS NOT NULL)
AND (SUM(pe.Vendor_Price/pe.ProductPrice) > '.25')

ORDER BY p.ProductCode
I'm puzzled as to why you need the SUM. In order to SUM, you have to
GROUP BY something. But from the looks of it, you are getting individual
records so what are you intending to SUM? It would likely help if you
gave us some more details, such as what the primary keys of these tables
are. Specifically, is this query intended to return a single row per
product code? Do any of the tables in the FROM clause contain multiple
rows per product code?
It never hurts to show us a few rows of sample data followed by the
desired results from that sample data.
--
HTH,
Bob Barrows

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.