Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-10-24 : 09:45:59
|
| Mikael writes "Hi I'm wondering if someone can help me with the following sql-problem...I have three tables, with their respective attributes:RawMaterial-----------RMname(pk) quantityunitRecipe-------cookieName(pk, fk)RMname(pk, fk)quantityunitCookie-------cookieName(pk)Rawmaterial holds information about raw materials for cookies, that is the raw materials name, quantity in stock and the unit, e.g. grams, liters, pieces etc.Cookie simply holds names of cookies, e.g. butter cake, chocolate dream etc.Recipe is a relational table and describes the relation between RawMaterial and Cookie.Each tuple (row) is identified by the cookieName together with RMname, that is to say both cookieName and RMname form the primary key.Question: I need to show ALL cookies and the amount of sugar for each cookie (null for the ones that doesn't have sugar in them)This query work fine, only problem is it only shows the cookies which has sugar in them:SELECT Cookie.cookieName, RawMaterial.RMname, Recipe.quantityFROM RawMaterial, RecipeLEFT OUTER JOIN CookieON Cookie.cookieName = Recipe.cookieNameWHERE Recipe.RMname = RawMaterial.RMnameAND RawMaterial.RMname = 'Sugar'The above select-statement would show:Children Egg Sugar 600.00Lemon Sugar 225.00Nutty Cake Sugar 375.00Nutty Grutty Sugar 190.00and I can't use ..."RawMaterial.RMname != 'Sugar'" because that would list all rows in Recipe that that doesn't include sugar (see below).Berserk Cookie Apple flavouring 160.00Berserk Cookie Butter 500.00Children Egg Butter 200.00Lemon Butter 125.00Nutty Grutty Butter 450.00Berserk Cookie Chocolate 80.00Children Egg Chocolate 100.00Nutty Cake Chocolate 50.00Children Egg Crap toy 215.00Nutty Cake Egg 250.00Berserk Cookie Flour 300.00Children Egg Flour 215.00Lemon Flour 100.00Nutty Grutty Flour 450.00Children Egg Fried crushed nuts 620.00Nutty Cake Fried crushed nuts 625.00Nutty Grutty Fried crushed nuts 225.00Berserk Cookie Hazelnuts 75.00Children Egg Hazelnuts 100.00Lemon Lemon 50.00Lemon Meal 450.00Children Egg Milk 1.50Nutty Cake Nuts 750.00Any suggestions?Any help would be greatly appreciated.Regards/Mikael W" |
|
|
|
|
|
|
|