Solved

SQL query including and excluding result

Posted on 2014-03-04
5
213 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

729 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