Monday, March 21, 2011

The Ice Cream Shop Revisited

In my 'The Ice Cream Shop' post I provided a query to create a simple database system for storing ice cream flavors and ice cream brands. I then provided queries to perform the following searches on our database:

  • Select all brands that sell a particular ice cream flavor
  • Select all flavors offered by a particular brand
Now we want to expand our Ice Cream Shop database to allow for the following actions/features:

  • Keep track of ice cream inventory. We want to know how much ice cream we have for each flavor and brand. We also want a run a report to tell us which ice cream's are low.
  • Keep track of ice cream distributors. We want to be able to find distributors that sell specific flavors and brands of ice cream.
  • Keep track of ice cream sales. We want to be able to keep track of which flavors and brands of ice cream were sold. We need to know the sale date, how many scoops were sold and what they sold for.
Like last time, I did the work of creating the tables and inserting the records (there was a lot of records in this one).
    Download the IceCreamStore Revisited Database:

    IceCreamStore Revisited for SQL Server 2008
    IceCreamStore Revisited for SQL Server 2008 R2


    Retrieving ice cream inventory that is low:
    To do this, we will employ a few techniques: table aliasing, multiple inner joins and conditional where statements.

    1. USE IceCreamStore;
    2. GO
    3. -- Retrieve the low ice cream inventory. Inventory is low when it is less than or equal to 1 tub.
    4. SELECT F.Name AS 'Flavor', B.Name AS 'Brand', I.stock AS 'Inventory'
    5. FROM Inventory AS I
    6. INNER JOIN xBrandFlavorInventory AS XI
    7. ON XI.inventoryID = I.inventoryID
    8. INNER JOIN Flavors AS F
    9. ON XI.flavorID = F.flavorID
    10. INNER JOIN Brands AS B
    11. ON XI.brandID = B.brandID
    12. WHERE I.stock <= 1;

    Our inner joins in the query above link the tables in this order:

    • Inventory
      • xBrandFlavorInventory
        • Flavors
        • Brands
    What it is doing is linking the Inventory table to the xBrandFlavorInventory table which acts as the 'middle-man' to our Flavors and Brands table.

    Retrieving distributors that sell an ice cream flavor/brand combination:
    This query employs the following techniques: table aliasing, multiple inner joins and conditional where statements:

    1. USE IceCreamStore;
    2. GO
    3. -- Search distributors for a flavor and brand combination.
    4. SELECT D.Name AS 'Distributor'
    5. FROM Distributors AS D
    6. INNER JOIN xBrandFlavorDistributors AS XD
    7. ON XD.distributorID = D.distributorID
    8. INNER JOIN Flavors AS F
    9. ON xd.flavorID = F.flavorID
    10. INNER JOIN Brands AS B
    11. ON xd.brandID = B.brandID
    12. WHERE F.Name = 'Caramel Ice Cream Bars' AND B.Name = 'Tip-Top';

    On line 13, in our WHERE statement, 'Caramel Ice Cream Bars' is our ice cream flavor and 'Tip-Top' is our brand name.

    Gathering sales for a specific month:
    This query employs the following techniques: table aliasing, multiple inner joins, conditional where statements and SQL Server Functions.

    1. -- Get all the sales for a specific month.
    2. SELECT F.Name AS 'Flavor', B.Name AS 'Brand', S.saleDate AS 'Sale Date', S.scoops AS 'Scroops', S.price AS 'Price'
    3. FROM Sales AS S
    4. INNER JOIN xBrandFlavorSales AS XS
    5. ON XS.saleID = S.saleID
    6. INNER JOIN Flavors AS F
    7. ON XS.flavorID = F.flavorID
    8. INNER JOIN Brands AS B
    9. ON XS.brandID = B.brandID
    10. WHERE DATEPART(MONTH, S.saleDate) = '03';

    In the query above, specifically on line 10, our WHERE statement is using an SQL Server Function called DATEPART(). This function pulls a part of a date form a date or datetime data type. We can then use the value returned by the DATEPART() function to do some conditional checking. In our case we are seeing if the MONTH is equal to '03', or the numeric equivalent to March.

    No comments:

    Post a Comment

    I Break Code Where code gets done.
    ASP.NET | HTML | SQL Server | VB.NET | Request A Topic |