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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Homework Help

Author  Topic 

scottw1988
Starting Member

4 Posts

Posted - 2012-04-14 : 13:30:18
Hello all, I need some help with my homework.

The question:

List all suppliers (animals and merchandise) who sold us items in June. Identify whether they sold us animals or merchandise.

I can do the first part of the question. But the second part, Identify whether they sold us animals or merchandise is throwing me for a loop. Any hints or suggestions anyone can offer would be much appreciated.

Here is my query so far:

SELECT S.NAME
FROM SUPPLIER S INNER JOIN ANIMALORDER AO ON S.SUPPLIERID = AO.SUPPLIERID
INNER JOIN MERCHANDISEORDER MO ON S.SUPPLIERID = MO.SUPPLIERID
WHERE MONTH(AO.ORDERDATE) = 6 OR MONTH(MO.ORDERDATE) = 6

The website that uses the same database ERD and questions we are using in my class are located at this URL.
http://jerrypost.com/DBMS/Exercises/Ch05DBExercises.html
The question this post pertains to is number 19.

Thanks,
Scott

Scott Williamson

scottw1988
Starting Member

4 Posts

Posted - 2012-04-14 : 13:33:00
Oh an by the way, we have an output format we have to follow. That is why so far I have only selected the name for the output.

Our output should look like this:

Name | Type of Sale

I am having trouble identifying the type of sale. How would one go about doing this?

Scott Williamson
Go to Top of Page

scottw1988
Starting Member

4 Posts

Posted - 2012-04-14 : 15:33:42
Figured it out. Posting the Solution so maybe others can benefit.

USE PET
SELECT S.NAME, 'ANIMAL' AS [Type of Sale]
FROM SUPPLIER S INNER JOIN ANIMALORDER AO ON S.SUPPLIERID = AO.SUPPLIERID
WHERE MONTH(AO.ORDERDATE) = 6

UNION

SELECT S.NAME, 'MERCHANDISE' AS [Type of Sale]
FROM SUPPLIER S INNER JOIN MERCHANDISEORDER MO ON S.SUPPLIERID = MO.SUPPLIERID
WHERE MONTH(MO.ORDERDATE) = 6

Scott Williamson
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-15 : 13:20:39
you can also use


SELECT S.NAME, CASE WHEN COUNT(AO.SUPPLIERID)> 0 THEN 'ANIMAL' WHEN COUNT(MO.SUPPLIERID)> 0 THEN 'MERCHANDISE' END AS [Type of Sale]
FROM SUPPLIER S
LEFT JOIN ANIMALORDER AO
ON S.SUPPLIERID = AO.SUPPLIERID
AND MONTH(AO.ORDERDATE) = 6
LEFT JOIN MERCHANDISEORDER MO
ON S.SUPPLIERID = MO.SUPPLIERID
AND MONTH(MO.ORDERDATE) = 6
GROUP BY S.NAME


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -