asked on
ASKER
declare @Customer as table (Customernum int, [total balance] money)
declare @customerrelation as table ([parent customer] int, childcustomer int)
Insert Into @Customer Values (10001,$50.00),(10011,$20),(10012,$30)
Insert Into @Customerrelation Values (10001,10011),(10001,10012)
Select COALESCE([parent customer], customernum) as customernum,
[total balance]
From @Customer Left Join @customerrelation On Customernum = childcustomer
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
ASKER
in customer relation table we have parent customer number as many time as its child customer e.g
Table 2 = customer.relation
Fields = parent customer , childcustomer
data = 10001 , 10011
10001, 10012
your query works but what it does is it adds parent balance 2 times or as many time as many child it has and it doesn't add balances for child.