Solved

SQL query including and excluding result

Posted on 2014-03-04
5
212 Views
Last Modified: 2014-03-18
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
Comment
Question by:peternordberg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39903288
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
 

Author Comment

by:peternordberg
ID: 39903303
I would be most grateful for an example!

Peter
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39907942
Provide some sample data from your tables and expected result
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39908759
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
 

Author Closing Comment

by:peternordberg
ID: 39937062
Super! Excellent!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question