webForumDet fria alternativet

Medelvärde i SP

5 svar · 240 visningar · startad av ironhead

ironheadMedlem sedan mars 20011 346 inlägg
#1

Om man har 2 fält där ena fältet är summan av röstningar (int) och det andra fältet är antal röstande (int) kan man då göra nån slags sortering (ORDER BY) på medelvärdet av dessa?

Använder MSSQL 7.0 & SP

yohpopsMedlem sedan feb. 20011 198 inlägg
#2
select ([summan av röstningar]/[antal röstande]) as [medel] from tabell order by ([summan av röstningar]/[antal röstande])

?

ironheadMedlem sedan mars 20011 346 inlägg
#3

Jag har en himla massa tabeller som knyts samman med iRecipeID:

SELECT DISTINCT tRecipe.iRecipeID,tRecipe.sHeadline,tEdition.iPublishDate,RecipeRatePub.Rate,RecipeRatePub.Votes
FROM tRecipe LEFT OUTER JOIN
FaktaRecipe ON tRecipe.iRecipeID = FaktaRecipe.iRecipe LEFT OUTER JOIN
RecipeRatePub ON tRecipe.iRecipeID = RecipeRatePub.RecipeID LEFT OUTER JOIN
Faktamangd ON FaktaRecipe.iRecipeFakta = Faktamangd.iFaktaID LEFT OUTER JOIN
tMainIngredientGroup ON tRecipe.iRecipeID = tMainIngredientGroup.iRecipeID LEFT OUTER JOIN
tIngredientGroup ON tMainIngredientGroup.iIngredientGroupID = tIngredientGroup.iIngredientGroupID LEFT OUTER JOIN
tMealRecipe ON tRecipe.iRecipeID = tMealRecipe.iRecipeID LEFT OUTER JOIN
tMeal ON tMealRecipe.iMealID = tMeal.iMealID LEFT OUTER JOIN
tRecipeOccation ON tRecipe.iRecipeID = tRecipeOccation.iRecipeID LEFT OUTER JOIN
tOccasion ON tRecipeOccation.iOccasionID = tOccasion.iOccasionID LEFT OUTER
JOIN tRecipeType ON tRecipe.iRecipeID = tRecipeType.iRecipeID LEFT OUTER JOIN
tType ON tRecipeType.iTypeID = tType.iTypeID LEFT OUTER JOIN
tThemeIndex ON tRecipe.iRecipeID = tThemeIndex.iRecipeID LEFT OUTER JOIN
tTheme ON tThemeIndex.iThemeID = tTheme.iThemeID LEFT OUTER JOIN
tEditionIndex ON tTheme.iThemeID = tEditionIndex.iThemeID LEFT OUTER JOIN
tEdition ON tEditionIndex.iEditionID = tEdition.iEditionID LEFT OUTER JOIN
tRecipeProduct ON tRecipe.iRecipeID = tRecipeProduct.iRecipeID LEFT OUTER JOIN
tProduct ON tRecipeProduct.iProductID = tProduct.iProductID
WHERE tEdition.iPublishDate <='20041215'
and Faktamangd.iFaktaID = '49'
ORDER BY (RecipeRatePub.Rate/RecipeRatePub.Votes) DESC

rate = summa total poäng
votes = antal röstningar

jag vill sortera på medelvärdet i fälten rate/votes men får detta felmeddelande:

Server: Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

ironheadMedlem sedan mars 20011 346 inlägg
#4

yohpops skrev:

select ([summan av röstningar]/[antal röstande]) as [medel] from tabell order by ([summan av röstningar]/[antal röstande])

?

Om jag lägger på detta får jag:

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

yohpopsMedlem sedan feb. 20011 198 inlägg
#5

Du måste ha med (RecipeRatePub.Rate/RecipeRatePub.Votes) i fältlistan.

ironheadMedlem sedan mars 20011 346 inlägg
#6

Jag märkte det :)
Tack!

126 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
124 ms — hämta tråd, inlägg och bilagor (db)