SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


advanced subquery


advanced subquery

Author
Message
bj 76771
bj 76771
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 2
I would like to get a distinct RefNumber only if this RefNumber has EntryType 0 and 2 and 11. All 3 EntryTypes must be present and no other EntryTypes must be.

RefNumber 239000 must be returned as it contains 0, 2 and 11
RefNumber 236777 must not be returnedsince it also contains a EntryType 7
RefNumber 238888 must not be returned since it lacks the EntryType 2



RefNumber EntryType EntryAmount
----------....---------....-----------
239000............0.............200
239000............0............-200
239000............2............-200
239000............2.............200
239000............11..........-145
239000............11...........145

236777............0.............162
236777............0............-162
236777............2..............-45
236777............2...............45
236777.............7..............62
236777............7..............-62
236777............11...........-162
236777............11............162

238888............0.............345
238888............0............-345
238888............11..........-245
238888............11...........245


Hope somebody can help me, and I'm very thankfull if - thanks in advance!

Benny
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25964 Visits: 17521
Like this:


select RefNumber
from YourTable
where RefNumber not in
(
Select RefNumber
from YourTable
where EntryType not in (0, 2, 11)
)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
bj 76771
bj 76771
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 2
Hi SSCrazy Eights!

THanks for your fast reply Smile

I have just edited my post as I didn't mentioned that RefNumber must not be returned if one of the 3 EntryTypes is missing.

You query returns also RefNumber where e.g only 0 and 11 are present, and e.g only 0 and 2 is precent

Benny
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25964 Visits: 17521
It would much better if you could post your ddl and sample data in a consumable format. (create table and insert statements)

Also, are there any more rules or is this it?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
bj 76771
bj 76771
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 2
There is no more rules than this :-D
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25964 Visits: 17521
Since you are brand new around here I create the ddl and sample data for you.


if OBJECT_ID('tempdb..#Data') is not null
drop table #Data

create table #Data
(
RefNumber int,
EntryType tinyint,
EntryAmount int
)

insert #Data
select 239000,0,200 union all
select 239000,0,-200 union all
select 239000,2,-200 union all
select 239000,2,200 union all
select 239000,11,-145 union all
select 239000,11,145 union all
select 236777,0,162 union all
select 236777,0,-162 union all
select 236777,2,-45 union all
select 236777,2,45 union all
select 236777,7,62 union all
select 236777,7,-62 union all
select 236777,11,-162 union all
select 236777,11,162 union all
select 238888,0,345 union all
select 238888,0,-345 union all
select 238888,11,-245 union all
select 238888,11,245

select * from #Data



There are a number of ways this could be accomplished. Here is one of those ways.


;with HasAllThree as
(
Select RefNumber, COUNT(distinct EntryType) as MyCount
from #Data
where EntryType in (0, 2, 11)
group by RefNumber
having COUNT(distinct EntryType) = 3
)

select d.RefNumber
from #Data d
join HasAllThree h on h.RefNumber = d.RefNumber
where d.RefNumber not in
(
Select RefNumber
from #Data
where EntryType not in (0, 2, 11)
)
group by d.RefNumber



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
bj 76771
bj 76771
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 2
Hi SSCrazy Eights

Thank you so much! It is exactly what I was looking for. It is bit more advanced than I'm used to when it comes to sql Cool

I will remember to provide create tale + data next time

Best regards
Benny
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7841 Visits: 7145

SELECT RefNumber
FROM #Data d
GROUP BY RefNumber
HAVING
COUNT(DISTINCT EntryType) = 3 AND
MAX(CASE WHEN EntryType NOT IN ( 0, 2, 11 ) THEN 1 ELSE 0 END) = 0



SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25964 Visits: 17521
ScottPletcher (4/4/2013)

SELECT RefNumber
FROM #Data d
GROUP BY RefNumber
HAVING
COUNT(DISTINCT EntryType) = 3 AND
MAX(CASE WHEN EntryType NOT IN ( 0, 2, 11 ) THEN 1 ELSE 0 END) = 0




Nice one Scott. I knew something along these lines but it just wasn't coming to me.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7841 Visits: 7145
Sean Lange (4/4/2013)
ScottPletcher (4/4/2013)

SELECT RefNumber
FROM #Data d
GROUP BY RefNumber
HAVING
COUNT(DISTINCT EntryType) = 3 AND
MAX(CASE WHEN EntryType NOT IN ( 0, 2, 11 ) THEN 1 ELSE 0 END) = 0




Nice one Scott. I knew something along these lines but it just wasn't coming to me.


:-) Yeah, the query plan is rather ugly, but to me that's just a weakness of the optimizer: it should be able o better optimize that code, and resolve the query in a single pass of the table.

SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search