Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

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

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
smhorton
Asked:
smhorton
  • 4
  • 3
  • 3
  • +3
1 Solution
 
deightonCommented:
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
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
Scott PletcherSenior DBACommented:
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
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

 
LowfatspreadCommented:
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
 
awking00Commented:
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
 
awking00Commented:
Also, if so, will all numbers be four digits?
0
 
smhortonAuthor Commented:
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
 
LowfatspreadCommented:
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
 
smhortonAuthor Commented:
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
 
LowfatspreadCommented:
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
 
awking00Commented:
select * from yourtable where clientid in
(select clientid from yourtable
 where eventid % 100 not between 70 and 79);
0
 
awking00Commented:
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
 
smhortonAuthor Commented:
thanks much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now