February 5, 2014 at 2:33 pm
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?
February 5, 2014 at 3:45 pm
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.
February 5, 2014 at 9:23 pm
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
Change is inevitable... Change for the better is not.
February 5, 2014 at 9:40 pm
February 5, 2014 at 10:09 pm
Heh... Been there and done that. Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 5, 2014 at 10:25 pm
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[/url]
Learn Extended Events
February 5, 2014 at 10:40 pm
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
Change is inevitable... Change for the better is not.
February 5, 2014 at 10:44 pm
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 😀
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
February 6, 2014 at 8:30 am
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.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply