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


t-sql where statement


t-sql where statement

Author
Message
wendy elizabeth
wendy elizabeth
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9154 Visits: 903
In t-sql 2008 r2, I am trying to determine how to setup the sql to accomplish the following
goal:

select table1.customer_id,type,start_date,end_date,Program_id from table1
join table2 on table1.customer_id = table2.customer_id

1. where type not= ('aa','cc') and type not = 'g2' where code = 3
In table1 there are lots of records for each customer_id and there can be
lots of various values for type. I only want the customer_ids that do not contain
the values listed above.
and
2. table2 has only one customer_id. Customer_id is the key of table2.
I want customers that do not have a value in one of the 3 columns:
start_date, end_date, and program_id.

Both parts 1 and 2 listed above need to be true for the customer_id to be selected.
Thus can you tell me how to setup that sql?
Keith Tate
Keith Tate
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5394 Visits: 979
First please read the article in my signature about posting these types of questions to the forum. After you do that I could make sure that the answer is correct. Without the necessary information the following where statement should get you what you want:
where type not in ('aa', 'cc')
and type <> = 'g2'
and code = 3
and (table2.start_date is null
or table2.end_date is null
or table2.program_id is null)


Instead of all of the nulls you could use union all for each of the "is null" statements.



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)

Group: General Forum Members
Points: 504943 Visits: 44238
Keith Tate (2/5/2014)
First please read the article in my signature about posting these types of questions to the forum. After you do that I could make sure that the answer is correct. Without the necessary information the following where statement should get you what you want:
where type not in ('aa', 'cc')
and type <> = 'g2'
and code = 3
and (table2.start_date is null
or table2.end_date is null
or table2.program_id is null)


Instead of all of the nulls you could use union all for each of the "is null" statements.


Just a curiosity... why did you treat 'g2' separately instead of just using...

  WHERE [Type] NOT IN ('aa','cc','g2')




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Keith Tate
Keith Tate
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5394 Visits: 979
I simply glossed over that fact that it was the same column Hehe Just went off the OP request.



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)

Group: General Forum Members
Points: 504943 Visits: 44238
Heh... Been there and done that. Thanks.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)

Group: General Forum Members
Points: 143621 Visits: 18649
Jeff Moden (2/5/2014)
Keith Tate (2/5/2014)
First please read the article in my signature about posting these types of questions to the forum. After you do that I could make sure that the answer is correct. Without the necessary information the following where statement should get you what you want:
where type not in ('aa', 'cc')
and type <> = 'g2'
and code = 3
and (table2.start_date is null
or table2.end_date is null
or table2.program_id is null)


Instead of all of the nulls you could use union all for each of the "is null" statements.


Just a curiosity... why did you treat 'g2' separately instead of just using...

  WHERE [Type] NOT IN ('aa','cc','g2')





well there is that method of fixing the query. The <>= would have thrown an error ;-)

Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Learn Extended Events

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)

Group: General Forum Members
Points: 504943 Visits: 44238
I saw that and figured it was just a phat phinger mistake that anyone would be able to fix and so didn't bring it up.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)

Group: General Forum Members
Points: 143621 Visits: 18649
Jeff Moden (2/5/2014)
I saw that and figured it was just a phat phinger mistake that anyone would be able to fix and so didn't bring it up.


Knowing Keith, it was most likely just a fta fingre mistake. Afterall, he knows better than that :-D

Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Learn Extended Events

wendy elizabeth
wendy elizabeth
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9154 Visits: 903
Thank you for your help! I read the post about having well formatted code. I will definitely do that the next time of having well formatted code.
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