Solved

sql query that excludes if the number is xx70 if it is the only one but keeps it if there are more numbers that are xxx0

Posted on 2013-02-06
13
208 Views
Last Modified: 2013-02-24
I need to select from a table and exclude the entries per client if they only xx70, but if they have more entries say xx40 and xx50 then I need to include the xx70.  Lord I hope that makes sense.

Thanks
Steve
0
Comment
Question by:smhorton
[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
  • 4
  • 3
  • 3
  • +3
13 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 500 total points
ID: 38860014
select Y1.* from yourtable Y1 where Y1.yourfield = 'xx70' AND  
EXISTS(
  SELECT 0 FROM yourtable Y2 WHERE Y1.ClientId = Y2.ClientId AND Y2.yourfield IN('xx40','xx50')
      );
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38860118
The Lord won't help you on this one.

Define the xx's, is this a char/varchar field, or is it all numeric?

Looks like deighton has the char answer.
If it's numeric you can also do this...

Select * FROM YourTable WHERE YourNumber % 100 = 70
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38860149
SELECT
    tn1.client_id, tn1.<col_name>, tn1....
FROM dbo.tablename tn1
WHERE
    EXISTS (
        SELECT 1
        FROM dbo.tablename tn2
        WHERE
            tn2.client_id = tn1.client_id AND
            tn2.entry NOT LIKE '%70' --find any number for same client NOT ending in 70
    )


I think that will do what you want functionally.  If performance then becomes an issue, we can deal with that too :-) .
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38860279
something like this then


you haven't made it very clear if you meant all data was xx70
or if xx70 was to be display only if you had some xx40 and xx50 entries

so join to an aggregate query that identifies the cases where the xx70 entries
should be shown.... (adjust the having clause to your actual requirements)

select ,,,,,
 from clientdata as c
 left outer join (select clientid
                         from clientdata as x
                        group by clientid
                        having sum(case when yourcol = 'xx70' then 1 else 0 end)
                                  <> count(*)
                  ) as x
on c.clientid=x.clientid
where x.clientid is not null
0
 
LVL 32

Expert Comment

by:awking00
ID: 38860573
Can I assume that by xx70 you mean the number could be like 2170 or 4870 and by xxx0 could be any number ending with 0?
0
 
LVL 32

Expert Comment

by:awking00
ID: 38860575
Also, if so, will all numbers be four digits?
0
 

Author Comment

by:smhorton
ID: 38864057
To answer the questions asked above.

1).  they are all 4 place numbers and are always 4 places
2).  Examples:  2070, 3070, 4070, each of the series go like 2010, 2020, 2030. through 2090 and they are client assignments to programs depending on location.  The xx70 ones are like welcome we will give you this assignment just to get you in the door.  So if they only have a xx7x assignment they don't really care but if they have the xx7x and some of the others to go with it like xx7x, xx5x etc. they want it in the report.

You guys are really helping I am working through the entries above.

Thanks Steve
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38864164
sorry is this a further complication

if   we have 2070  and 2170 and 2150  
are you saying you only want 2170 and 2150 shown and the 2070 excluded?

ie  the other (leading) digits are significant/related to the problem/other events

can you provide some example data and show us what the expected result would be.
0
 

Author Comment

by:smhorton
ID: 38864770
The series go like this 2010, 2020, 2030, 2020, 2030, 2040, 3010, 3020, 3030.  when you look at the 4 place numbers, the first two, xx is the location, the next two is the type of program at the location, xxxx.  1010 - 10 is clinic # 10, the second 10, 1010 is the wellness program at clinic 10.  The xx70 means they are in the introductory program at that clinic.

In only want xx7x included if they are any other combinations.  Include xx7x if xx2x, or xx3x or xx4x are there, etc.  I do need to pull the other numbers but I don't want xx7x unless there are other numbers.  Was hoping to end up with a view that will let me run a reporting tool against it.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38864828
select *
  from yourtable as a
 where eventdata not like '__7_'
 or (eventdata like '__7_' and exists (select * from yourtable as x
               where (x.eventdata between (a.eventdata / 100) * 100 and 69+((a.eventdata / 100) * 100)
                      or  x.eventdata between 80+((a.eventdata / 100) * 100) and 99+((a.eventdata / 100) * 100)
        ) and x.clientid = a.clientid
               )
      )
order by clientid
0
 
LVL 32

Expert Comment

by:awking00
ID: 38865240
select * from yourtable where clientid in
(select clientid from yourtable
 where eventid % 100 not between 70 and 79);
0
 
LVL 32

Expert Comment

by:awking00
ID: 38865244
I'm always forgetting the required subquery alias -
select * from yourtable where clientid in
(select clientid from yourtable
 where eventid % 100 not between 70 and 79) as x;
0
 

Author Closing Comment

by:smhorton
ID: 38924125
thanks much
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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…

726 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