473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with joins and aggregate functions

2 New Member
Hi all, and thanks ahead to the helpers.
for several hours im trying to overcome on an error im stuck with..
that's the issue:
i have 3 table:
1. Recipes
2. RegisteredUsers
3. RecipeRating

the searching value would be Recipes.RecipeName.
i would like to retrieve the following:
1. Recipes.*
2. RegisteredUsers.Username
3. AVG(Rating) , SUM(Rating)

that's the query i've tried to run:

Expand|Select|Wrap|Line Numbers
  1. SELECT     Recipes.Id, Recipes.RecipeName, Recipes.DateAdded, Recipes.Show, Recipes.Category, Recipes.ShortDescription, Recipes.PrepareTime_Secs, 
  2.                       Recipes.Level, RegisteredUsers.Username AS Uploader, AVG(Rate.Rating) AS r1, SUM(Rate.Rating) AS r2
  3.  
  4. FROM         Recipes INNER JOIN
  5.                       RegisteredUsers ON Recipes.Uploader = RegisteredUsers.Id INNER JOIN
  6.                           (SELECT     Id AS rateId,  Rating
  7.                             FROM          RecipeRating
  8.                             GROUP BY rateId
  9.                             ) AS Rate ON Recipes.Id =Rate.RecipeId
  10. WHERE     (Recipes.RecipeName = @input)
  11.  
however, i keep getting the same annoying error: The multi-part identifier "Rate.rateId" could not be bound.
i tried several variety of changes but nothing much helped.
Apr 6 '09 #1
4 2716
code green
1,726 Recognized Expert Top Contributor
Not familiar with the error message but pretty sure you cannot use an alias within a GROUP BY
Apr 7 '09 #2
Tomerz
2 New Member
ok,
i changed the query as follows:
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT     Recipes.Id, Recipes.RecipeName, Recipes.DateAdded, Recipes.Show, Recipes.Category, Recipes.ShortDescription, Recipes.PrepareTime_Secs, 
  3.                       Recipes.Level, RegisteredUsers.Username AS Uploader, AVG(Rate.Rating) AS r1, SUM(Rate.Rating) AS r2
  4. FROM         Recipes INNER JOIN
  5.                       RegisteredUsers ON Recipes.Uploader = RegisteredUsers.Id INNER JOIN
  6.                           (SELECT     Id, RecipeId, Rating
  7.                             FROM          RecipeRating
  8.                             GROUP BY RecipeId) AS Rate ON Recipes.Id = Rate.RecipeId
  9. WHERE     (Recipes.RecipeName = @input)
  10.  
  11.  
im receiving the below error:
column 'RecipeRating.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause..

when i add it to the 'group by' clause, i get the same message for 'Rating'.
when i add 'Rating' to the clause, im receiving the error message for 'Recipes.Id'

so i've added 'GROUP BY Recipes.Id' to the outer table, and the same message appears for the 'Recipes.RecipeName'

it doesn't look like it gonna end..
Apr 7 '09 #3
code green
1,726 Recognized Expert Top Contributor
Yup,that is how it is.
If one field is aggregated or in a GROUP BY then all fields must comply.
I have studied the reasoning but would not attempt to explain it here.
I have tried ways around but have not been successful.
MySql for example lets you get away with it, but this is simply the engine
allowing short-hand.
Apr 7 '09 #4
ck9663
2,878 Recognized Expert Specialist
On your first post, you have this portion "GROUP BY rateId". This is wrong because you don't really have a column named rateId. It's an alias. You can not use alias in any other part of the query, including WHERE, GROUP BY, ORDER...

On your second post it's because you have to include Rating on your group by.

The question, however, is why do you need it grouped? If you're concerned with duplicate, just use DISTINCT.

--- CK
Apr 7 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
2
by: jc | last post by:
Hi. Just as we have AVG(COLUMN_NAME) and MAX(COLUMN_NAME) how can I write my own variation of a such a function. If I can appreciate how to do this, then I hopefully I can write a...
14
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN...
2
by: JJA | last post by:
Please advise on how to get the GROUP BY coded in an acceptable way: DECLARE @LO INT DECLARE @HI INT DECLARE @StartDate varchar(10) DECLARE @EndDate varchar(10) SELECT @StartDate =...
1
by: sausage31 | last post by:
I have a table as follows.... Device LotID Result1 Result2 Result3 aaa 1 5 10 15 bbb 1 2 4 6 aaa 2 ...
10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
5
by: David Garamond | last post by:
What do people think of adding some more aggregate functions. These are the ones that MySQL has and PG doesn't: - STD/STDDEV - VARIANCE - BIT_OR - BIT_AND - GROUP_CONCAT (for strings, added...
8
by: jefftyzzer | last post by:
The current issue of "Oracle Magazine" has an article on creating custom aggregate functions, which naturally got me thinking about how to do this in DB2. I found some articles on creating...
8
by: Matt C | last post by:
I have rather complicated join I can't figure out (been mostly using trial-and-error method...). I'll just explain with words first: (table layout below) Users are in categories. Depending on...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.