SQL Query

-- The code below works as it is but I would like to add another condition.
                  --   when            m1.Question in ('Silky', 'Hard')  and    m1.Value = 'Y'  are true then I want  to check for       
              --                        and    m1.Question in ('Misc')  and  m1.Value = 'N'

              --                        how do I add this condition?

Here is the code:
            Select    m1.*, m2.*    

            from Livedb.dbo.Main   m1 with (NOLOCK)

            left join Livedb.dbo.Main m2  with (nolock)
            on m1.OrdID = m2.OrdID

            where isnull(m1.DateTime)   between @BeginDate and @EndDate  
            and livedb.dbo.fxAge(m1.Orderdate,m1.DateTime)  > 7
            and m1.ItemNo = '100740'  

            and    m1.Question in ('Silky', 'Hard')
            and    m1.Value = 'Y'

      new condition  --> and  m1.Question in   ('Misc')        and        m1.Value =  'N'
Abdul KhanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Add an OR condition, but be sure to wrap each SETof conditions in parenthesis so it evaluates correctly

        WHERE .....
        AND ....
        AND      and m1.ItemNo = '100740'  
        AND  (    
 m1.Question in ('Silky', 'Hard') and    m1.Value = 'Y' )
                         ( m1.Question in   ('Misc')  and        m1.Value =  'N' )

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
where isnull(m1.DateTime)   between @BeginDate and @EndDate  
             and livedb.dbo.fxAge(m1.Orderdate,m1.DateTime)  > 7
             and m1.ItemNo = '100740'  
             and ((m1.Question in ('Silky', 'Hard') and m1.Value = 'Y')
               or (m1.Question in ('Misc') and m1.Value =  'N'))
awking00Information Technology SpecialistCommented:
Sorry, _agx_, Didn't see your comment before I posted :-)
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

awking00 - Great minds think alike :-)
@Abdul Khan

Nothing to do with your question, but if "DateTime" column is indexed, it's better not to use functions on it, since they'll prevent the db from using the index. Instead, use a comparison like this:

WHERE  m1.DateTime >=  @BeginDate
AND     m1.DateTime <= @EndDate

The exact syntax depends on what type of comparison you're performing and whether or not the values include a "Time" portion
Abdul KhanAuthor Commented:
@_agx_  and @awking00 Thank you so much for your quick assistance. And I appreciate the insight regarding the functions.
Abdul KhanAuthor Commented:
@_agx_  and @awking00  thanks both!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.