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
204 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
  • 4
  • 3
  • 3
  • +3
13 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 500 total points
Comment Utility
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 65

Expert Comment

by:Jim Horn
Comment Utility
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:ScottPletcher
Comment Utility
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
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
Also, if so, will all numbers be four digits?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:smhorton
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
select * from yourtable where clientid in
(select clientid from yourtable
 where eventid % 100 not between 70 and 79);
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
thanks much
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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 utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

728 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

10 Experts available now in Live!

Get 1:1 Help Now