?
Solved

Urgent help required for T-SQL queries and a Stored Procedure

Posted on 2011-03-04
8
Medium Priority
?
568 Views
Last Modified: 2012-05-11
Hi all,

I have some knowledge of database design and SQL but I have some problems completing some queries and a Stored Procedure. I have the following four tables for Recipe Database.


CREATE TABLE Recipe
(
 ReceipeID        INT IDENTITY NOT NULL,
 ReceipeName      VARCHAR(100) NOT NULL,
 ReceipeDate      DATETIME,
 ReceipeAuthorID  INT NOT NULL,

)

CREATE TABLE RecipeAuthor
(
RecipeAuthorID INT IDENTITY NOT NULL,
AuthorName      VARCHAR(50) NOT NULL,
Reference       VARCHAR(3000)
)

CREATE TABLE RecipeIngredients
(
 ReciepteIngredientID  INT IDENTITY NOT NULL,
 ReceipeID             INT NOT NULL,
 Quantity              Decimal,
 UnitofMeasurement     VARCHAR(1000),
 IngredientName        VARCHAR(1000)


)

CREATE TABLE RecipeDirection
(
RecipeDirectionID  INT IDENTITY NOT NULL,
RecipeID            INT NOT NULL,
StepProcess          VARCHAR(3000)

)


I have populated them with data and I have completed some queries. But I am unable to complete the following questions.

(1) Here are the questions:

What are the names of the three most used ingredients by quantity in all recipes authored by XYZ?
What is the name of all recipes that do not use egg as an ingredient?
What is the second word of each recipe title?


(2) Stored Procedure Question on the same database above.

Write a stored procedure that will add an ingredient to a recipe. The procedure should accept at a minimum the recipe name or ID, ingredient name or ID, ingredient quantity, and ingredient unit.

If the ingredient already exists, update the quantity; an error should occur if the new ingredient and quantity are the same. A return code of 0 should indicate success and -1 if an error was encountered.

Make use of an output parameter that contains a semi-colon separated list of ingredient name and units. The value should be structured similar to the following:

2 cups white sugar;1/2 cup butter;1/2 cup milk;3 tablespoons cocoa powder

Include at the bottom of the same file, SQL code that will execute the procedure and print or select the error code and ingredient list (output parameter).
0
Comment
Question by:Itudk_2010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 1000 total points
ID: 35040555
Is this an homework question? If not provide some sample data from your tables.
0
 

Author Comment

by:Itudk_2010
ID: 35040749
Hi Sharath_123,

Thanks for your reply. It is a project not a home work. Could you please help in this regard. Following is the data


Receipe  Table                                  
ReceipeID      Name           Date             AuthorID     createddate      
1                   Sandwich        1/1/2011      1               1/1/2011      
2                   Ham Burger      1/1/2011      2               1/1/2011      
                                   
                                   
                     
RecipeAuthor  Table                                  
AuthorID     AuthorName                            
1                   irene                              
2                    walsh                              
                                   
                             
ReciepeIngredients Table                                  
ReciepeIngredientsID     ReceipeID      Ingredeint Name      UnitofMeasurement      quantity          
1                                          1                Bread                          Cup                               2          
1                                          1                greens                       Tablespoon                     3        
2                                    
                                   
ReceipeDirection                                    
ReceipeStepsID      ReceipeID      StepProcess                
1                                   1                wash the greens                
2                                   1                  toast the bread                
3                                   1                   apply cheese on bread            

I really appreciate your help. Looking forward to your reply.

Best regards.  
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 1000 total points
ID: 35040967
You have asked lot of questions in one post. Please try to implement the queries and if you need help, we will help you.
I have answered the first part of your questions.

>> What are the names of the three most used ingredients by quantity in all recipes authored by XYZ?
SELECT TOP 3 ri.IngredientName 
    FROM RecipeAuthor ra 
         JOIN Recipe r 
           ON ra.RecipeAuthorID = r.RecipeAuthorID 
         JOIN RecipeIngredients ri 
           ON ri.RecipeID = r.RecipeID 
   WHERE ra.AuthorName = 'irene' -- replace irene with any other name 
ORDER BY ri.Quantity DESC

Open in new window

>> What is the name of all recipes that do not use egg as an ingredient?
SELECT r.RecipeName 
  FROM Recipe r 
       LEFT JOIN RecipeIngredients ri 
         ON r.RecipeID = ri.RecipeID 
            AND ri.IngredientName <> 'egg' 
 WHERE ri.RecipeID IS NULL

Open in new window

>> What is the second word of each recipe title?
SELECT CASE 
         WHEN CHARINDEX(' ',RecipeName) <> 0 THEN SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1,
                                                            LEN(RecipeName)) 
         ELSE 'No Second Word' 
       END Second_Word 
  FROM Recipe

Open in new window

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:Itudk_2010
ID: 35041284
Hi Sharath_123,

The first answer is correct But the following two questions give wrong output. The first one returns no results as it should return bread, greens etc which is in the table above except egg.

(1) What is the name of all recipes that do not use egg as an ingredient?

 SELECT r.RecipeName
  FROM Recipe r
       LEFT JOIN RecipeIngredients ri
         ON r.RecipeID = ri.RecipeID
            AND ri.IngredientName <> 'egg'
 WHERE ri.RecipeID IS NULL

(2) What is the second word of each recipe title?

 SELECT CASE
         WHEN CHARINDEX(' ',RecipeName) <> 0 THEN SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1,
                                                            LEN(RecipeName))
         ELSE 'No Second Word'
       END Second_Word
  FROM Recipe


This one returns the second and third and fourth words so on. It should return only the second word but not the remaining words. Is there any idea?

I really appreciate your help. Looking forward to your reply.
0
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 1000 total points
ID: 35041495
(1) What is the name of all recipes that do not use egg as an ingredient?

select RecipeName from Recipe where RecipeID not in  (select RecipeID from RecipeIngredients where IngredientName = 'egg')


(2) What is the second word of each recipe title?
SELECT
      CASE
         WHEN CHARINDEX(' ',RecipeName) <> 0 THEN
                  SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1,
                        case
                              when charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) = 0 then LEN(RecipeName)
                              else charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName)))
                        end)
         ELSE
                  'No Second Word'
       END Second_Word
FROM Recipe
0
 

Author Comment

by:Itudk_2010
ID: 35041528
@derekkromm,

Thanks a lot for your help. Your answer for question 2 is correct. But question 1 is still not working?

select RecipeName from Recipe where RecipeID not in  (select RecipeID from RecipeIngredients where IngredientName = 'egg')

any other ideas?

0
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 1000 total points
ID: 35041539
That query definitely works. See below code for example.

Now, its not going to detect "eggs" or "egg----", because that does not equal "egg". Nor should it, because then "egg noodles" would be detected as "egg", which would be undesirable.

declare @recipe table (
	RecipeID int identity,
	RecipeName varchar(100))
	
declare @recipeingredients table (
	RecipeIngredientsID int identity,
	RecipeID int,
	IngredientName varchar(100))
	
insert into @recipe(RecipeName) 
select 'Recipe 1' union select 'Recipe 2' union select 'Recipe 3'

insert into @recipeingredients(RecipeID, IngredientName)
select 1,'egg' union select 1,'flour' union select 1,'rice'
union select 2,'water' union select 2,'rice'
union select 3,'flour' union select 3,'milk'

select recipename from @recipe where RecipeID not in (select RecipeID from @recipeingredients where IngredientName = 'egg')

Open in new window

0
 

Author Closing Comment

by:Itudk_2010
ID: 35041587
No comments
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question