简体   繁体   中英

SQL query is not returning desired value

I have three Tables as follows:

CREATE Table [Product]( 
[product_id] int primary key,
[name] varchar(100) not null,
[date] varchar(20) not null,
[quantity] int not null,
[stock] int not null,
[category] varchar(50) not null,
[unitPrice] int not null,
[vendor_id] int not null
)


create table [distribution](
[distribution_id] int primary key,
[showroom] varchar(50) not null,
[quantity] int not null,
[date] varchar(20) not null,
[unitPrice] int not null,
[product_id] int not null
)

create table [sales](
[sales_id] int primary key,
[product_id] int not null, 
[date] varchar(20) not null,
[time] varchar(15) not null,
[quantitiy] int not null,
[cash] int not null,
[branch] varchar(50) not null
)

Now I want to make a query that will return for each Product ID

[product].product_id, 
[product].unitPrice as 'pUnitPrice', 
[product].quantity, 
[product].unitPrice*[product].quantity as "stockTotal",
SUM([sales].quantitiy) as 'salesUnit', 
[distribution].unitPrice as 'sUnitPrice',
SUM([sales].quantitiy)*[distribution].unitPrice as 'saleTotal', 
[product].quantity-SUM([sales].quantitiy) as 'balance'

I am doing my project in ASP.NET with Microsoft SQL Server 2008 R2. I have made a query(appended) but that results wrong may it is checking all the data for one product_id. I am very much disapponted...Please help me if anyone has time....Please please...

My query:

SELECT 
    [sales].product_id, 
    [product].unitPrice                             as 'pUnitPrice', 
    [product].quantity                              as 'stock', 
    [product].unitPrice * [product].quantity        as "stockTotal", 
    SUM([sales].quantitiy)                          as 'salesUnit',
    [distribution].unitPrice                        as 'sUnitPrice',
    SUM([sales].quantitiy)*[distribution].unitPrice as 'saleTotal',
    [product].quantity-SUM([sales].quantitiy)       as 'balance'
from sales 
JOIN product        ON sales.product_id     = product.product_id
JOIN [distribution] ON [product].product_id = [distribution].product_id 
group by [sales].product_id, 
         [product].unitPrice, 
         [product].quantity, 
         [distribution].unitPrice
order by [sales].product_id 

Only the

SUM([sales].quantity)

gives the error. It just added up to three times. Say, the desired quantity is 4, it became 12, just like for every product_id....

The values in product

(product_id, name, date ,quantity, stock, category, unitPrice, vendor_id)

(1,HP, 2013-03-15, 10, 6, Laptop, 55000, 2)

The values in Distribution table:

distribution_id   showroom   quantity      date     unitPrice    product_id
      1            Ritzy1        2      2013-03-02    55000          1
      2            Ritzy2        2      2013-03-02    55000          1
      3            Ritzy3        2      2013-03-02    55000          1

The values in Sales table:

 sales_id   product_id       date       time      quantitiy    cash     branch
    1           1         2013-03-29  7:26:22 PM      2   110000    Ritzy1

My query result

(product_id,    pUnitPrice, stock,  stockTotal, salesUnit,  sUnitPrice, saleTotal,  balance)


(1, 50000,  10, 500000, **6**,  55000,  330000, 4)

Desired Output:

 product_id  pUnitPrice  stock  stockTotal  salesUnit  sUnitPrice  saleTotal  balance
     1         50000       10     500000        2         55000      110000      8

You can't currently get the price from the Distribution table via the product_id, because there could be many different prices stored on the Distribution table for the same product_id. There is no way of knowing which distribution price should be used with each sale, for a given product.

In the sample data you have included, all of the distribution prices for the given product are the same. Is this how you intend to design your system - that a given product will always have the same price, no matter where it is distributed? If so, remove the price from the distribution table and store it on the product table only.

On the other hand, given that your current design includes the price on the Distribution table and your query attempts to derive the sale value from the distribution price, this implies that the sale price of the product may vary by showroom. Is this how you intend to design your system - that a product will have a different sale price, depending on where it is distributed? If so, you need to be able to link each sale record to the specific Distribution where it was sold - this would mean adding the distribution_id to the Sales table.

Then again, another way of determining the sale price would be to store the sale price on the Sales record - superficially, this looks like denormalisation, but in practice prices are likely to change over time; unless you want to build a distribution history table, holding details of what prices were applicable at what times in each showroom, this would be the simplest way to record which prices were actually charged on each sale.

The reason I asked whether this was homework (including end-of-semester projects) is that any of these scenarios are reasonable simplifications of what a real-life system would look like, for academic purposes - you have to decide which one you are using / intend to use. (A real-life system would need to be more complicated.)

The reason your query is returning sales values three times higher than the actual sales value is because you are linking all Sales records for a given product to all Distributions for the same product, then summing the results - since you have three Distribution records for the product on the Sales record, you are therefore seeing three times the actual Sales value. The query can be rewritten to work correctly, but only when you decide which way you wish to store prices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM