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 |
|
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.NAMEFROM SUPPLIER S INNER JOIN ANIMALORDER AO ON S.SUPPLIERID = AO.SUPPLIERID INNER JOIN MERCHANDISEORDER MO ON S.SUPPLIERID = MO.SUPPLIERIDWHERE MONTH(AO.ORDERDATE) = 6 OR MONTH(MO.ORDERDATE) = 6The 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.htmlThe question this post pertains to is number 19.Thanks,ScottScott 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 SaleI am having trouble identifying the type of sale. How would one go about doing this?Scott Williamson |
 |
|
|
scottw1988
Starting Member
4 Posts |
Posted - 2012-04-14 : 15:33:42
|
| Figured it out. Posting the Solution so maybe others can benefit.USE PETSELECT S.NAME, 'ANIMAL' AS [Type of Sale]FROM SUPPLIER S INNER JOIN ANIMALORDER AO ON S.SUPPLIERID = AO.SUPPLIERIDWHERE MONTH(AO.ORDERDATE) = 6UNION SELECT S.NAME, 'MERCHANDISE' AS [Type of Sale]FROM SUPPLIER S INNER JOIN MERCHANDISEORDER MO ON S.SUPPLIERID = MO.SUPPLIERIDWHERE MONTH(MO.ORDERDATE) = 6Scott Williamson |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-15 : 13:20:39
|
you can also useSELECT 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.SUPPLIERIDAND MONTH(AO.ORDERDATE) = 6LEFT JOIN MERCHANDISEORDER MO ON S.SUPPLIERID = MO.SUPPLIERIDAND MONTH(MO.ORDERDATE) = 6GROUP BY S.NAME ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|