Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ive Four Tables
1 Sale(saleID int Identity,AID)--Aid From Allocation Table
2 Allocation(AID int Identity, PurchaseID int)--PurchaseID From Purchase Table
3 Purchase(PurchaseID int,ProductID int)--ProductID From Product Table
4 Prodcut(ProductID int Identity,ProductName)

I want To Get ProductName From ProductTable Base On SaleID

i.e.
sale Table

saleID--AID
--1-----2

Allocation Table

AID---PurChaseID
1-----2
2-----1

Purchase Table

PurchaseID-ProductID
----1-------2
----2-------1

Product Table
ProductID-ProductName
---1-------CellPhone
---2-------Laptop

I want Result as

SaleID--ProductName
---1----Laptop

I can do This Using Table Variable but It will Take more Execution Time
So I Want a Small query....Im Quiet Confused What to do and What to not Please Help.......


Logic I used For Table Variable is
First I took All AID From Sale Master in Squence
Based On these AID's I'm Collecting All PurchaseID's From Allocation Table in Sequence
Based On These PurchaseID's I'm Collecting All ProductID's From Purchase Table in Squence
and Using these ProductID's I get their Names in Squence
After that I'm Inserting All SaleID's and All ProductName in One Table Variable and Selecting it as a Result
Posted
Updated 3-Jul-14 3:44am
v2
Comments
W Balboos, GHB 3-Jul-14 10:11am    
What (SQL) have you tried?
prashantttt 4-Jul-14 0:54am    
as Ive Mentioned,Ive Done It Using Table Variable............

1 solution

You need to learn about JOIN [^]

Or use the tools provided in Visual Studio to build queries for you.

Like this
SELECT sale.SaleID, Product.ProductName
FROM sale 
INNER JOIN Allocation ON Allocation.AID = sale.AID 
INNER JOIN Purchase ON Allocation.PurChaseID = Purchase.PurchaseID 
INNER JOIN Product ON Purchase.ProductID = Product.ProductID
 
Share this answer
 

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