Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In SQL SSMS I am trying to write a query following these instructions: Youve been asked to send thank you gifts to customers who have

In SQL SSMS I am trying to write a query following these instructions:

Youve been asked to send thank you gifts to customers who have purchased large quantities of your recently released chocolate animal series (stock item ids 222, 223, 224, 225). Your query needs to list the customer ID, the customer Name, have a column that totals units of each animal type, and a grand total for all animals. To qualify for the gift, the customer must have purchased at least 300 of any given animal type, or a total of 500 across all animal types. Your final query should sort by the total largest to smallest. You should get 20 rows the first two look like this -

CustomerID

CustomerName

Beetles

Echidnas

Frogs

Sharks

Total

441

Wingtip Toys (Keosauqua, IA)

216

432

0

96

744

573

Wingtip Toys (Marin City, CA)

264

0

312

168

744

This is my query so far.

SELECT SC.CustomerID, CustomerName, SUM(CASE WHEN WSI.StockItemID = 222 THEN (Quantity * -1) ELSE 0 END) AS Beetles, SUM(CASE WHEN WSI.StockItemID = 223 THEN (Quantity * -1) ELSE 0 END) AS Echidnas, SUM(CASE WHEN WSI.StockItemID = 224 THEN (Quantity * -1) ELSE 0 END) AS Frogs, SUM(CASE WHEN WSI.StockItemID = 225 THEN (Quantity * -1) ELSE 0 END) AS Sharks

FROM Sales.Customers SC INNER JOIN Warehouse.StockItemTransactions WSIT ON SC.CustomerID = WSIT.CustomerID INNER JOIN Warehouse.StockItems WSI ON WSIT.StockItemID = WSI.StockItemID

WHERE WSI.StockItemID = 222 OR WSI.StockItemID = 223 OR WSI.StockItemID = 224 OR WSI.StockItemID = 225

GROUP BY SC.CustomerID, CustomerName, WSI.StockItemID

ORDER BY CustomerName DESC

I can't figure out how to do the total, how to add the three values in each and how to only show the lines that are high enough values. Here are a few lines of the results I am getting right now just to illustrate.

549 Wingtip Toys (Lake Ronkonkoma, NY) 0.000 96.000 0.000 0.000 441 Wingtip Toys (Keosauqua, IA) 216.000 0.000 0.000 0.000 441 Wingtip Toys (Keosauqua, IA) 0.000 432.000 0.000 0.000 441 Wingtip Toys (Keosauqua, IA) 0.000 0.000 0.000 96.000 488 Wingtip Toys (Isabela, PR) 0.000 0.000 0.000 120.000 548 Wingtip Toys (Indian Creek, IL) 0.000 192.000 0.000 0.000

I need you to help me fix this query

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions