Solved

SQL query including and excluding result

Posted on 2014-03-04
5
208 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 VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 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