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
206 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
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 65

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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