SQL query is not returning desired value

rissee

New Member
I have three Tables as follows:\[code\]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)\[/code\]Now I want to make a query that will return for each Product ID\[code\][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'\[/code\]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: \[code\]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_idJOIN [distribution] ON [product].product_id = [distribution].product_id group by [sales].product_id, [product].unitPrice, [product].quantity, [distribution].unitPriceorder by [sales].product_id \[/code\]
 
Back
Top