Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I am developing Online Billing software for college project.Everything is working fine but I am facing one problem. Customer may purchase more than 1 item.suppose customer purchase 5 items so how can I store all these purchased Items in same column and Make a total of all these items.

I hope you understand what I am trying to say..
Posted

You can Create 2 tables tblBill and tblBillDetail

tblBill will store information like customerID, BillDate or any other field that will be same for a bill.

tblBillDetail will store the Bill details like no. of items purchased (Quantiy), unit price of each item(UnitPrice), Total amount of each item purchased etc. The BillID column in table tblBillDetail links the Purchased items to the customer.

CREATE TABLE tblBill
(
	BillID INT IDENTITY(1,1) CONSTRAINT PK_tblBill PRIMARY KEY,
	CustomerID INT,
	BillDate DATETIME
)


CREATE TABLE tblBillDetail
(
	BillDetail INT IDENTITY(1,1) CONSTRAINT PK_BillDetail PRIMARY KEY,
	BillID INT CONSTRAINT FK_tblBillDetail_tblBill FOREIGN KEY REFERENCES tblBill(BillID),
	ProductDescription VARCHAR(100),
	Quantity DECIMAL(16,2),
	UnitPrice DECIMAL(16,2),
	UOM VARCHAR(50),
	ExtendedAmount DECIMAL(16,2),
)


Consider an example where a customer purchases 5 Tooth brushes and 2 Tooth paste.
The data will be inserted into the tables as shown below

SQL
INSERT INTO tblBill (CustomerID, BillDate) VALUES (1,GETDATE())

--Get the BillID that was generated and assign it to @BillID
SELECT @BillID = SCOPE_IDENTITY()

INSERT INTO tblBillDetail (BillID, ProductDescription, Quantity, UnitPrice, UOM, ExtendedAmount) VALUES (@BillID, 'Tooth Brush', 5, 3.45, 'EACH', 5 * 3.45)

INSERT INTO tblBillDetail (BillID, ProductDescription, Quantity, UnitPrice, UOM, ExtendedAmount) VALUES (@BillID, 'Tooth Paste', 2,12.95, 'EACH', 2 * 12.95)


Query the two tables to retrieve the information you need.

SQL
SELECT B.BillID, CustomerID, BillDate, BillTotal, ProductDescription, Quantity, UnitPrice, UOM, ExtendedAmount FROM tblBill B
INNER JOIN tblBillDetail D ON B.BillID = D.BillID
INNER JOIN
(
    SELECT BillID, SUM(ExtendedAmount) AS BillTotal FROM tblBillDetail
    GROUP BY BillID
) X ON B.BillID = X.BillID
 
Share this answer
 
v2
Comments
Pankil_Plus 15-Jul-12 23:10pm    
Thank you very much your help sir.
__TR__ 16-Jul-12 0:24am    
You are welcome.
Create table like this
Create table purchase
(Billid int Primarykey,
CustomerId int int,
ItemId int,
NoOfItems int
)


After that you can insert multiple rows for single customer.
Insert into Purchase(1,1,123,2)
Insert into Purchase(2,1,125,3)

and the below query will give the total no of items for a single customer
Select customerId,sum(NoofItems) from Purchase group by CustomerId.
 
Share this answer
 
Comments
Pankil_Plus 15-Jul-12 23:11pm    
this works... thank you very much
can you explain your question i can't understand your requirement. You need to calculate the items cost in db or UI page.
 
Share this answer
 
Comments
Pankil_Plus 15-Jul-12 23:11pm    
want to calulate items in UI then I want to insert them to my databse

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900