0

I have a table with values and 1 with identifiers and I am trying to Left join the identifier table to the values table.

The issue is that in the identifiers table, it is not a 1:1 match.

I need the Left Join to match everything by cost center first and foremost, but the ones that have a specific Account Category attached, I would want only those account categories to be mapped within the cost centers.

Tables

How would I structure my join statement?

Currently I have it set up as:

`SELECT * FROM [TABLE 1] t1
LEFT JOIN [TABLE 2]  t2
ON t1.[Cost Center] = t2.[Cost Center]
AND **NEED SOMETHING TO FILTER OUT SELECTED ACCOUNT CATEGORIES`
4
  • 1
    Can you please share the output you're trying to get for this sample input?
    – Mureinik
    Mar 8, 2016 at 17:09
  • Something like: SELECT * FROM [Table1] LEFT JOIN [Table2] ON [TABLE1].[Cost Center] = [Table 2].[Cost Center] AND ([Table1].[Account Category] = [Table2].[Account Category] OR ([Table1].[Account Category] IS NULL AND [Table2].[Account Category] IS NULL)), perhaps? Mar 8, 2016 at 17:16
  • is "table 3" the desired output?
    – Jayvee
    Mar 8, 2016 at 17:32
  • yes table 3 is what i would like the end product to look like. @Diamond, that did not work. It still does not capture everything Mar 8, 2016 at 18:04

3 Answers 3

0

You can add the conditions in the join or the where.

SELECT * FROM [TABLE 1] t1
LEFT JOIN [TABLE 2]  t2
ON t1.[Cost Center] = t2.[Cost Center]
AND t1.Category = 'bleh'

or

SELECT * FROM [TABLE 1] t1
LEFT JOIN [TABLE 2]  t2
ON t1.[Cost Center] = t2.[Cost Center]
WHERE t1.Category = 'bleh'
1
  • 1
    this would give different result sets. putting it on the where makes it basically an inner join of sorts.
    – Ricardo C
    Mar 8, 2016 at 17:59
0

if I understood the problem correctly, this should work:

SELECT t1.[Cost Center], t1.[Account Category],t2.[Flag]
FROM [TABLE 1] t1
LEFT JOIN [TABLE 2]  t2
ON t1.[Cost Center] = t2.[Cost Center]
AND t1.[Account Category] = t2.[Account Category]
2
  • This is not working because there are some cost centers that I would want to include in their entirety, regardless of the Account Category. This limits it to just where there is a match. Mar 8, 2016 at 18:08
  • but since it's a left join it will include all the Cost Centres present in table 1; it's not limiting to match only, that would be an inner join
    – Jayvee
    Mar 8, 2016 at 18:22
0

If I understand your question correctly, the trick here is that you need 2 LEFT JOIN statements and an ISNULL in the select. Below, the 2nd LEFT JOIN only looks at rows with no AccountCategory. This yields your Table3. This solution assumes Table1's AccountCategory cannot be null/empty.

Setup:

DECLARE @Table1 TABLE 
(
    CostCenter INT,
    AccountCategory VARCHAR(10)
)

DECLARE @Table2 TABLE 
(
    CostCenter INT,
    AccountCategory VARCHAR(10),
    Flag VARCHAR(10)
)

INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8601, 'ABC1');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8601, 'ABC1');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8601, 'ABC2');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8601, 'ABC3');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8601, 'ABC4');

INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8602, 'ABC1');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8602, 'ABC1');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8602, 'ABC2');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8602, 'ABC3');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8602, 'ABC4');

INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8603, 'ABC1');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8603, 'ABC1');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8603, 'ABC2');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8603, 'ABC3');
INSERT INTO @Table1 (CostCenter, AccountCategory)   VALUES (8603, 'ABC4');

INSERT into @Table2 (CostCenter, AccountCategory, Flag) VALUES (8601, 'ABC1', 'Yes');
INSERT into @Table2 (CostCenter, AccountCategory, Flag) VALUES (8602, null, 'Yes');
INSERT into @Table2 (CostCenter, AccountCategory, Flag) VALUES (8603, null, 'Yes');

Query:

SELECT  t1.CostCenter, t1.AccountCategory, ISNULL(t2a.Flag, t2b.Flag) AS Flag
FROM    @Table1 t1
LEFT JOIN   @Table2 t2a ON ISNULL(t2a.AccountCategory,'') = t1.AccountCategory AND t1.CostCenter = t2a.CostCenter 
LEFT JOIN   @Table2 t2b ON ISNULL(t2b.AccountCategory,'') = '' AND t1.CostCenter = t2b.CostCenter

Results:

CostCenter  AccountCategory Flag
8601    ABC1    Yes
8601    ABC1    Yes
8601    ABC2    NULL
8601    ABC3    NULL
8601    ABC4    NULL
8602    ABC1    Yes
8602    ABC1    Yes
8602    ABC2    Yes
8602    ABC3    Yes
8602    ABC4    Yes
8603    ABC1    Yes
8603    ABC1    Yes
8603    ABC2    Yes
8603    ABC3    Yes
8603    ABC4    Yes
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.