Solved

SQL query including and excluding result

Posted on 2014-03-04
5
201 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
  • 2
  • 2
5 Comments
 
LVL 142

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 40

Expert Comment

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

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now