SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Server View Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Jmekubo
Starting Member

Kenya
2 Posts

Posted - 06/19/2013 :  06:04:02  Show Profile  Reply with Quote
Kindly help on sql query to create a view that merges data from two tables with same columns but second table (sales) has missing rows that correspond first table(Inventory)

In essense: I need those items that are in Inventory table but don’t have entries in sales table to be included in my sales view with (0) quantities.

Inventory
ItemID ItemName Qty
1 Bread 5
2 Soda 10
3 Cocoa 4
4 Blueband 15
5 Omo 20
6 Biscuits 30
7 Pens 50
8 Note book 5

Sales
ItemID ItemName Qty
1 Bread 3
2 Soda 5
5 Omo 10
6 Biscuits 15
8 Note book 2

I want my sales view to look like below
Sales View
ItemID ItemName Qty
1 Bread 3
2 Soda 5
3 Cocoa 0
4 Blueband 0
5 Omo 10
6 Biscuits 15
7 Pens 0
8 Note book 5

bandi
Flowing Fount of Yak Knowledge

India
2215 Posts

Posted - 06/19/2013 :  06:07:46  Show Profile  Reply with Quote
SELECT I.ItemID, I.ItemName, COALESCE(s.Qty, 0) Qty
FROM Inventory I
LEFT JOIN Sales s ON I.ItemID = s.ItemId

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/19/2013 :  06:08:03  Show Profile  Reply with Quote
sorry reading again i think i misunderstood

can you explain that qty part

why its comes from Sales in some case and Inventory in someother?

Edited by - visakh16 on 06/19/2013 06:10:21
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/19/2013 :  06:11:30  Show Profile  Reply with Quote
for example for Note book 5
it comes from Inventory while

for 5 Omo 10
its from sales

so whats the rule?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2215 Posts

Posted - 06/19/2013 :  06:15:28  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

for example for Note book 5
it comes from Inventory while

for 5 Omo 10
its from sales

so whats the rule?

I think there was a typo in Qty value for NoteBook.. Remaining item's quantities are from Sales table only...

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/19/2013 :  06:16:31  Show Profile  Reply with Quote
ideally as per your table names, this is logical

CREATE VIEW SalesView
AS
SELECT I.ItemID, I.ItemName, i.Qty AS QtyInStock,
COALESCE(s.Qty, 0) AS SalesQty,
i.Qty - COALESCE(s.Qty, 0) AS ResidualQty
FROM Inventory I 
LEFT JOIN Sales s ON I.ItemID = s.ItemId


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000