• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

SQL query including and excluding result

HI,

I have a couple of tables that I want to query in my SQL Server database.

customer
customerID, int
firstname, nvarchar
lastname, nvarchar

customerCategory
customerCategoryID, int
customerCategory
parentID, int (customerCategoryID of the parent to this row)

If a parentID is NULL then it is a parent and then of course those that have a parentID is a child to that parent.

customerCategoryMember
memberID, int
customerCategoryID, int
customerID, int

I would like a query that:
Selects all customers in customerCategoryMember that are in a (or several) customerCategories and their childs.
but excludes those that are in another chosen category and its childs.

For example,
I want all members of customerCategories 5 and 10 with their respective childs (for 5 it is 6 and 7 and for 10 it's 11 and 12)
But I would like to exclude those members in the above query that is part of category 13 or 15, with their childs (14 and 16)

Is it possible for me to form such a query?

Thanks for help (and hopefully the question makes sense)!

Peter
0
peternordberg
Asked:
peternordberg
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
yes, it is possible, using CTE / hierachical queries:
http://technet.microsoft.com/en-us/library/ms186243%28v=sql.105%29.aspx

you will need 2 CTE expressions to build the "inclusion" and "exclusion" list, and use them in the main query.
if you need more detailed help, i can try to build the sql up
0
 
peternordbergAuthor Commented:
I would be most grateful for an example!

Peter
0
 
SharathData EngineerCommented:
Provide some sample data from your tables and expected result
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
here we go with a data sample + query script:
declare @customer table ( customerID int, firstname nvarchar(10), lastname nvarchar(10) )
declare @customerCategory table ( customerCategoryID int, customerCategory nvarchar(10), parentID int  )

declare @customerCategoryMember table ( memberID int, customerID int, customerCategoryID int)

insert into @customer values ( 1, 'smith', 'john' )
insert into @customer values ( 2, 'braun', 'marcel' )
insert into @customer values ( 3, 'jones', 'indiana' )

insert into @customerCategory values ( 5, 'cat 5' , null )
insert into @customerCategory values ( 6, 'cat 6' , 5 )
insert into @customerCategory values ( 7, 'cat 7' , 5 )


insert into @customerCategory values ( 10, 'cat 10' , null )
insert into @customerCategory values ( 11, 'cat 11' , 10 )
insert into @customerCategory values ( 12, 'cat 12' , 10 )

insert into @customerCategory values ( 13, 'cat 13' , null )
insert into @customerCategory values ( 14, 'cat 14' , 13 )

insert into @customerCategory values ( 15, 'cat 15' , null )
insert into @customerCategory values ( 16, 'cat 16' , 15 )


insert into @customerCategoryMember values ( 1005, 1, 5 )

insert into @customerCategoryMember values ( 2005, 2, 5 )
insert into @customerCategoryMember values ( 2013, 2, 13 )



insert into @customerCategoryMember values ( 3016, 3, 16 )


;with included_categories as ( 
   select customerCategoryID , customerCategory , parentID
      from @customerCategory c
   where customerCategoryID in ( 5, 10 ) -- included parents
   union all
   select c.customerCategoryID , c.customerCategory , c.parentID
      from included_categories p
	  join @customerCategory c
	    on c.parentID = p.customerCategoryID 
)
,  excluded_categories as ( 
   select customerCategoryID , customerCategory , parentID
      from @customerCategory c
   where customerCategoryID in ( 13, 15 ) -- excluded parents
   union all
   select c.customerCategoryID , c.customerCategory , c.parentID
      from excluded_categories p
	  join @customerCategory c
	    on c.parentID = p.customerCategoryID 
)
select ic.*, c.*, cm.customerCategoryID
from included_categories ic
join @customerCategoryMember cm
  on ic.customerCategoryID = cm.customerCategoryID
join @customer c on c.customerID = cm.customerID

where not exists ( select null 
				from excluded_categories ec
				join @customerCategoryMember em
				  on em.customerCategoryID = ec.customerCategoryID
                where em.customerID = cm.customerID

	)

Open in new window

output is:
customerCategoryID customerCategory parentID    customerID  firstname  lastname   customerCategoryID
------------------ ---------------- ----------- ----------- ---------- ---------- ------------------
5                  cat 5            NULL        1           smith      john       5

hope this helps
0
 
peternordbergAuthor Commented:
Super! Excellent!
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now