Solved

SQL Where statment, AND x IN (a, b, c)

Posted on 2011-03-01
8
557 Views
Last Modified: 2012-05-11
So I'm guessing the solution to this is reasonably easy and I'm just missing something.  I have a SQL query set up to output some data in a table, and everything works fine.  I'm making another query that is similar except for one little difference.  The where statement in the first query looks like so:

WHERE
      UpdateDate BETWEEN @StartDate AND @EndDate
      AND LCCN IN (52015, 55215, 39010, 39100)
-- followed by
GROUP BY
      CASE WHEN LCCN = 52015 THEN 'Item A'
                  WHEN LCCN = 55215 THEN 'Item B'
                  WHEN LCCN = 39010 THEN 'Item C'
                  WHEN LCCN = 39100 THEN 'Item D'
            END,
      YEAR(UpdateDate),
      MONTH(UpdateDate),
      DATENAME(mm,UpdateDate)

The second query change will be to use ProductCode instead of LCCN, but ProductCode would also need to pull multiple numbers per group.  So say the group by statement would look something like this (assuming this works)

GROUP BY
      CASE WHEN ProductCode between 1 and 10 THEN 'Item A'
                  WHEN ProductCode between 11 and 20 THEN 'Item B'
                  WHEN ProductCode between 21 and 30 'Item C'
                  WHEN ProductCode between 31 and 40 THEN 'Item D'
            END,
      YEAR(UpdateDate),
      MONTH(UpdateDate),
      DATENAME(mm,UpdateDate)

(As I said, I'm not sure that would work, but thats not my concern right now).  The problem is I can't figure out how to do that in the Where statment.  Something like

AND ProductCode IN (1 to 10, 11 to 20, 21 to 30, 31 to 40)

but I can't figure out what the correct way to do that would be (or for that matter if it is possible).

Thanks for the help let me know if more information is needed (sorry typed this up in a hurry).
0
Comment
Question by:mjburgard
8 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 150 total points
ID: 35013262
AND ProductCode between 1 and 40
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 35013437
Try
 
WHERE (UpdateDate BETWEEN @StartDate AND @EndDate)
AND (ProductCode BETWEEN 1 AND 40)
--for group by

 GROUP BY
      CASE WHEN ProductCode < 11 THEN 'Item A'
                  WHEN ProductCode < 21 THEN 'Item B'
                  WHEN ProductCode < 31 THEN 'Item C'
                  WHEN ProductCode < 41 THEN 'Item D'
            END,
      YEAR(UpdateDate),
      MONTH(UpdateDate),
      DATENAME(mm,UpdateDate)

Open in new window

0
 
LVL 5

Expert Comment

by:Kelmen
ID: 35013736
select * from (
select * ...
,      CASE WHEN ProductCode between 1 and 10 THEN 'Item A'
                  WHEN ProductCode between 11 and 20 THEN 'Item B'
                  WHEN ProductCode between 21 and 30 'Item C'
                  WHEN ProductCode between 31 and 40 THEN 'Item D'
            END
  As PCode ...
Where ...
)
Group by  ... PCode ...
0
 
LVL 45

Expert Comment

by:aikimark
ID: 35013978
@mjburgard

What kind of database is this?
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 4

Expert Comment

by:samijsr
ID: 35015796
having ProductCode between 1 and 40
0
 
LVL 1

Author Comment

by:mjburgard
ID: 35040035
Ok that appears to be easier then I thought, didn't think about just calling all the product codes and letting the GROUP BY take care of it.

One more little question though, is there a way to not show any NULL's that show up?  Say if we changed things around so I was calling product code 1 through 50, but 35 through 40 wasn't in any of the groups.  Right now anything in there shows up as NULL, is there an easy way to not show that? Its not a huge deal, but would be nice for the event that this is changed around for additions to the database.
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 100 total points
ID: 35040095
Add a WHERE clause.

WHERE ProductCode IS NOT NULL
0
 
LVL 1

Author Closing Comment

by:mjburgard
ID: 35040154
First solution was correct, but second was more detailed and easier to follow, which is why it got more points.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

914 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

17 Experts available now in Live!

Get 1:1 Help Now