0

I want to create a table [Top Stores] that has 10 most active stores in terms of quantity of product sold. Each store sells 3 products. I just need to add the quantity sold for each product to arrive at StoreSale. The problem is when there is no sale for a product, the quantity value is left blank and not 0. I try to change the blank to 0 by the isnull tests. Please point out the error in my code below:

SELECT top 10  StoreName, StoreSale = (iif (isnull(Product1), 0, Product1) + iif (isnull(Product2), 0, Product2) + iif (isnull(Product3), 0, Product3)) INTO [Top Stores]
FROM SaleTable ORDER BY StoreSale DESC;
4

1 回答 1

1

Two things:

  1. if you use =, you will be doing an equality test, you can't have an assignment like that in SQL.
    You will need to name your calculated result AS StoreSale instead.

  2. Use Nz() to get a 0.00 when the field value is NULL

Result:

SELECT TOP 10 StoreName,
              Nz(product1) + Nz(Product2) + Nz(Product3) AS StoreSale
INTO   [Top Stores]
FROM   SaleTable
ORDER  BY StoreSale DESC; 
于 2013-03-31T06:33:58.840 回答