Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

SQL Left Join Help

sinner231_100

Proficient
Advanced Member
Messages
298
Reaction score
3
Points
28
Mga sir at mam patulong naman po sa SQL Query ko po.. May dalawang database po kase ako na laman ay stock inventory, gusto ko sana na icompare ang stocks nilang dalawa. gagamit po ako ng left join statement pero diko po kase makuha yung required na rows na dapat na results nya..

ganito po ang query ko

select a.itemid, b.ItemID, a.Quantity as Table1Balance, b.Quantity as Table2Balance
from tblStockInventory a
left join SecondDB.dbo.tblStockInventory b on a.ItemID = b.ItemID
where a.BranchID = '007' and b.BranchID = '007'

kung gagamit po ako ng select statement sa unang table ang results po nya ay 100 rows, na sya rin pong dapat maging results sana nya pag ginamit ko na yung unang query sa taas,. Kung may hindi man nameet na itemid eh di mag 'NULL' sana.. pero ang kabuuan na results ay 100 rows padin dapat sana... pero ang nagpapakita lang pag ginamit ko yang query sa taas ay 50 rows lang, hindi nagkakaron ng 'NULL' sa second itemid..

pasensya na po mejo magulo... maraming salamat po sa mga tutulong...
 
almost same ba yung lahat ng field ts? kung ako gagawa nyan ganito naisip ko why not retrieve list of table 1 and also retrieve list of table 2 then you can easily compare then put mo sa map para wala kang maging problema sa speed kapag i compare mo yung dalawa
 
same lang sila ng fields sir... pero yung mga ibang itemid wala sa second table.. pag icompare ko mag 'NULL' sana sa second table, pero hindi nagpapakita sa query na ginamit ko sa taas.
 
Sir natry mo na mag LEFT OUTER JOIN or similar to that? Experiment mo sir, baka makuha mo gusto mo mangyari. :)
 
Mga sir at mam patulong naman po sa SQL Query ko po.. May dalawang database po kase ako na laman ay stock inventory, gusto ko sana na icompare ang stocks nilang dalawa. gagamit po ako ng left join statement pero diko po kase makuha yung required na rows na dapat na results nya..

ganito po ang query ko

select a.itemid, b.ItemID, a.Quantity as Table1Balance, b.Quantity as Table2Balance
from tblStockInventory a
left join SecondDB.dbo.tblStockInventory b on a.ItemID = b.ItemID
where a.BranchID = '007' and b.BranchID = '007'

kung gagamit po ako ng select statement sa unang table ang results po nya ay 100 rows, na sya rin pong dapat maging results sana nya pag ginamit ko na yung unang query sa taas,. Kung may hindi man nameet na itemid eh di mag 'NULL' sana.. pero ang kabuuan na results ay 100 rows padin dapat sana... pero ang nagpapakita lang pag ginamit ko yang query sa taas ay 50 rows lang, hindi nagkakaron ng 'NULL' sa second itemid..

pasensya na po mejo magulo... maraming salamat po sa mga tutulong...


Try mo sir remove mo muna ung where clause mo, tapos try mo ilabas ung a.BranchID at b.BranchID, para mapadali investigation mo
 
natry ko na din left outer join sir, sige sir subukan ko yung advice ni sir inchard..
 
ito try mo muna

select a.itemid, b.ItemID, a.Quantity as Table1Balance, b.Quantity as Table2Balance
from tblStockInventory a
left join SecondDB.dbo.tblStockInventory b on a.ItemID = b.ItemID
where a.BranchID = '007'

- - - Updated - - -

Hindi marestore, access denied daw.. may nagtry nabang iba jan?
 
more than sa expected result ang nagpapakita sir...

1425 expected
3297 rows ang nagpakita
 
select a.itemid, b.ItemID, a.Quantity as Table1Balance, b.Quantity as Table2Balance
from tblStockInventory a
left join SecondDB.dbo.tblStockInventory b on a.ItemID = b.ItemID
where a.BranchID = '007' OR b.BranchID='007'


Patry lang ulit. hahaha... Diko marestore yung bak file mo eh.
 
3297 rows padin sir ang output, ayaw ata talaga marestore pag number yung unang letter sa database name...
 
Try mo sir remove mo muna ung where clause mo, tapos try mo ilabas ung a.BranchID at b.BranchID, para mapadali investigation mo

Dalwang DB or 2 tables?
i try mo muna mag query ng pang isang table, then sa isa..try to check relational fields :)
 
Pakitry to

create a scalar function or paste this code to sql query editor then click Execute...

Palitan mo yung USE [firstDB] at secondDB .. ilagay mo yung name ng first and second db mo.
Code:
USE [firstDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[fnGetValueFromDB2]
(
	@category varchar(50),
	@itemID int
)
RETURNS int
AS
BEGIN
	DECLARE @return int
	if @category='itemID'
		BEGIN
			SELECT @return = itemID from secondDB.dbo.tblStockInventory WHERE ItemID=@itemID
		END
	else 
	BEGIN
			SELECT @return = Quantity from secondDB.dbo.tblStockInventory WHERE ItemID=@itemID
	END
		
	RETURN @return
END

Then ito yung query kapag mag cocompare kana:
Code:
select a.ItemID, dbo.fnGetValueFromDB2('itemID',a.ItemID) as item2, a.Quantity, 
dbo.fnGetValueFromDB2('quantity',a.Quantity) as Quantity2
from tblStockInventory a

where  a.BranchID='007'

Palitan mo nalang yung mga database name na nasa query at sa function.
 
Last edited:
Back
Top Bottom