我有三个表如下:
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
)
现在我想做一个查询,将返回每个产品 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'
我正在使用 Microsoft SQL Server 2008 R2 在 ASP.NET 中做我的项目。我做了一个查询(附加),但结果错误可能是检查一个product_id的所有数据。我非常失望...如果有人有时间请帮助我...请...
我的查询:
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
只有
SUM([sales].quantity)
给出错误。它只是增加了三倍。比如说,想要的数量是 4,它变成了 12,就像每个 product_id....
产品价值
(product_id, name, date ,quantity, stock, category, unitPrice, vendor_id)
(1,HP, 2013-03-15, 10, 6, 笔记本电脑, 55000, 2)
表中的值Distribution
:
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
表中的值Sales
:
sales_id product_id date time quantitiy cash branch
1 1 2013-03-29 7:26:22 PM 2 110000 Ritzy1
我的查询结果
(product_id, pUnitPrice, stock, stockTotal, salesUnit, sUnitPrice, saleTotal, balance)
(1, 50000, 10, 500000, **6**, 55000, 330000, 4)
期望的输出:
product_id pUnitPrice stock stockTotal salesUnit sUnitPrice saleTotal balance
1 50000 10 500000 2 55000 110000 8