1

I have 6 tables with IDs of customers. It is posible, in one query, to get the customers ids that coincide in all tables? In my example, the customers ids that results are 8 and 9 (it must be taken into account, that in practice, any of the tables may have values ​​or not)

Many thanks Best regards

DECLARE @TCustomerAge  TABLE(
CustomerID  INT not null ); 


DECLARE @TCustomerLocalization  TABLE(
CustomerID  INT not null ); 


DECLARE @TCustomerGroup  TABLE(
CustomerID  INT not null ); 


DECLARE @TCustomerStore  TABLE(
CustomerID  INT not null ); 


DECLARE @TCustomerSales  TABLE(
CustomerID  INT not null ); 


DECLARE @TCustomerPayment  TABLE(
CustomerID  INT not null ); 


--@TCustomerAge empty
insert into @TCustomerLocalization values (8), (9), (11)
insert into @TCustomerGroup values (8), (9), (10), (11)
insert into @TCustomerStore values (8), (9), (11)
--@@TCustomerSales empty
insert into @TCustomerPayment values (2), (3), (5), (8), (9), (10)

-- Result -> 8, 9
4
  • 1
    If the ID had to be in all tables would the result of this query be nothing as you have 2 empty tables? And why would 8,9,11 be the right answer as only 8 and 9 show up in all tables that have data? Commented May 23, 2019 at 14:35
  • in those tables, I save the ids that meet certain rules, for that reason it is possible that exists empty tables. Sorry for the result mistake, I correct it Commented May 23, 2019 at 14:42
  • Seems to me this would be a full outer join between all the tables on customerID where customerID is not null in any of the tables. Commented May 23, 2019 at 14:48
  • And what do you want returned if a table has no rows? Commented May 23, 2019 at 14:52

2 Answers 2

2

To handle empty tables, perhaps union would be a better approach:

with t as (
      select id, '@TCustomerAge' as tablename from @TCustomerAge 
      union  -- uses union to remove duplicates within tables
      select id, '@TCustomerLocalization' from @TCustomerLocalization
      union
      . . .
     )
select t.id
from t cross join
     (select count(distinct tablename) as n from t) n
group by t.id, n.n
having count(*) = n.n;

Empty tables are not represented in t, so the count(distinct) only counts tables that are present.

Sign up to request clarification or add additional context in comments.

Comments

0
;with CteAllIDs as (
    SELECT CustomerID FROM @TCustomerAge UNION
    SELECT CustomerID FROM @TCustomerLocalization UNION
    SELECT CustomerID FROM @TCustomerGroup UNION
    SELECT CustomerID FROM @TCustomerStore UNION
    SELECT CustomerID FROM @TCustomerSales UNION
    SELECT CustomerID FROM @TCustomerPayment
)
SELECT CteAllIDs.CustomerID
FROM CteAllIDs
WHERE (EXISTS (SELECT * FROM @TCustomerAge WHERE CustomerID = CteAllIDs.CustomerID) 
    OR NOT EXISTS (SELECT * FROM @TCustomerAge))
AND (EXISTS (SELECT * FROM @TCustomerLocalization WHERE CustomerID = CteAllIDs.CustomerID) 
    OR NOT EXISTS (SELECT * FROM @TCustomerLocalization))
AND (EXISTS (SELECT * FROM @TCustomerGroup WHERE CustomerID = CteAllIDs.CustomerID) 
    OR NOT EXISTS (SELECT * FROM @TCustomerGroup))
AND (EXISTS (SELECT * FROM @TCustomerStore WHERE CustomerID = CteAllIDs.CustomerID) 
    OR NOT EXISTS (SELECT * FROM @TCustomerStore))
AND (EXISTS (SELECT * FROM @TCustomerSales WHERE CustomerID = CteAllIDs.CustomerID) 
    OR NOT EXISTS (SELECT * FROM @TCustomerSales))
AND (EXISTS (SELECT * FROM @TCustomerPayment WHERE CustomerID = CteAllIDs.CustomerID) 
    OR NOT EXISTS (SELECT * FROM @TCustomerPayment))

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.