Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 table in database 1.Warehouse, 2.Product, 3. Stock and i want to fetch the Data from these three Table like.
Frirst Column are the Warehouses names want be the header of available Quantity left in Stock.
Second Column is The Product Names which can not be duplicated.
Third Column is Available Quantity in Stock which have to convert as row along with First Column warehouse names.

output

Warehouse Name   |  Product Name  |  Available Quantity
Warehouse 1      |   Shampoo     |    50
Warehouse 2      |   Shampoo     |    100
Warehouse 3      |   Shampoo     |    40
Warehouse 1      |   Soup        |    50
Warehouse 2      |   Soup        |    80
Warehouse 3      |   Soup        |    120


I want expected Output

Product Name |   Warehouse1 Qty | Warehouse2 Qty | Warehouse3 Qty |  Total 
Shamoo       |    50            |       100      |     40        |  190
Soup         |    40            |       80       |     120        |  240


What I have tried:

SQL
SELECT       WareHouse.WareHouse_Name,
              Product.Cat_Name 'Product Name',
               SUM(Stock.Stock_In-Stock.Stock_Out) 'Available Quantity'

FROM            Stock INNER JOIN
                         Product_Catagory ON Stock.Cat_Id = Product_Catagory.Cat_ID INNER JOIN
                         WareHouse ON Stock.Warehouse_ID = WareHouse.WareHouse_ID

			GROUP BY    Product_Catagory.Cat_Name,WareHouse.WareHouse_Name
Posted
Updated 9-Apr-19 8:13am
v2

1 solution

You need to use the PIVOT SQL function. Google is your friend.
 
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