SQL select statment (and more) question

I have the following SQL query set up to keep track of info on our presses.  I recently added in the "Hours of Work" (marked by a **) into the output of the select statement.  The only issue is it turns out that for the hours of work on each press we need to call from LCCNs 52000 to 52099 for the 6/c and from 55200 to 55299 for the PM and so on.  But I can't change the entire code to call from that cause the other variables need to call from only 52015 and 55215 and so on.

So my question is, is there an easy way to have just the Hours call from all the LCCN's they need?  Or I guess if there's not an easy way, is there a hard way?  Thanks for the help

/*  Change @CountType to 0 for regular counts; 2 for rework; any other number (i.e. 1) for total count.
      Change @StartDate and @EndDate to the start and end dates of the search, respectivly.

                   Press Chart
            LCCN                  Press
            52015                  6/c Press
            55215                  PM
            39010                  M700
            39100                  DE
*/
DECLARE @StartDate DATETIME, @EndDate DATETIME, @CountType INT
SET @StartDate = '2011-02-01'
SET @EndDate = '2011-02-28'
SET @CountType = 0
SELECT
Press = CASE WHEN LCCN = 52015 THEN '6/c Press'
                  WHEN LCCN = 55215 THEN 'PM'
                  WHEN LCCN = 53015 THEN 'DC'
                  WHEN LCCN = 39010 THEN 'M700'
                  WHEN LCCN = 39110 THEN 'DE'
            END,
      YEAR(UpdateDate) as 'Year',
      DATENAME(mm,UpdateDate) as 'Month',
      SUM(LaborQuantity) as 'Total',
**      SUM(Hours) as 'Hours of Work',
      COUNT(DISTINCT JobN) as 'Number of Jobs'
FROM
      JobLabor
WHERE
      UpdateDate BETWEEN @StartDate AND @EndDate
      AND LCCN IN (52015, 55215, 53015, 39010, 39110)
      AND (prefix = @CountType
            OR @CountType NOT IN (0,2))
GROUP BY
      CASE WHEN LCCN = 52015 THEN '6/c Press'
                  WHEN LCCN = 55215 THEN 'PM'
                  WHEN LCCN = 53015 THEN 'DC'
                  WHEN LCCN = 39010 THEN 'M700'
                  WHEN LCCN = 39110 THEN 'DE'
            END,
      YEAR(UpdateDate),
      MONTH(UpdateDate),
      DATENAME(mm,UpdateDate)
ORDER BY
      CASE WHEN LCCN = 52015 THEN '6/c Press'
                  WHEN LCCN = 55215 THEN 'PM'
                  WHEN LCCN = 53015 THEN 'DC'
                  WHEN LCCN = 39010 THEN 'M700'
                  WHEN LCCN = 39110 THEN 'DE'
            END,
      YEAR(UpdateDate),
      MONTH(UpdateDate),
      DATENAME(mm,UpdateDate)
LVL 1
mjburgardAsked:
Who is Participating?
 
Kevin CrossConnect With a Mentor Chief Technology OfficerCommented:
Try something like this:
SELECT CASE 
          WHEN LCCN BETWEEN 52000 AND 52099 THEN '6/c Press'
          WHEN LCCN BETWEEN 55200 AND 55299 THEN 'PM'
          WHEN LCCN BETWEEN 53000 AND 53099 THEN 'DC'
          WHEN LCCN BETWEEN 39000 AND 39099 THEN 'M700'
          WHEN LCCN BETWEEN 39100 AND 39199 THEN 'DE'
       END AS [Press]
     , YEAR(UpdateDate) as [Year]
     , DATENAME(mm,UpdateDate) as [Month]
     , SUM(CASE 
              WHEN LCCN IN (52015, 55215, 53015, 39010, 39110)
                 THEN LaborQuantity
              ELSE 0
           END) as [Total]
     , SUM(Hours) as [Hours of Work]
     , COUNT(DISTINCT CASE 
                WHEN LCCN IN (52015, 55215, 53015, 39010, 39110)
                   THEN JobN
             END) as [Number of Jobs]
FROM JobLabor
WHERE UpdateDate BETWEEN @StartDate AND @EndDate
  AND (LCCN BETWEEN 52000 AND 52099 /*52015*/
       OR LCCN BETWEEN 55200 AND 55299 /*55215*/
       OR LCCN BETWEEN 53000 AND 53099 /*53015*/
       OR LCCN BETWEEN 39000 AND 39099 /*39010*/
       OR LCCN BETWEEN 39100 AND 39199 /*39110*/)
  AND (prefix = @CountType OR @CountType NOT IN (0,2))
GROUP BY CASE 
            WHEN LCCN BETWEEN 52000 AND 52099 THEN '6/c Press'
            WHEN LCCN BETWEEN 55200 AND 55299 THEN 'PM'
            WHEN LCCN BETWEEN 53000 AND 53099 THEN 'DC'
            WHEN LCCN BETWEEN 39000 AND 39099 THEN 'M700'
            WHEN LCCN BETWEEN 39100 AND 39199 THEN 'DE'
         END
       , YEAR(UpdateDate)
       , MONTH(UpdateDate)
       , DATENAME(mm,UpdateDate)
ORDER BY Press, [Year], MONTH(UpdateDate)
;

Open in new window

0
 
mjburgardAuthor Commented:
Worked like a charm, and thanks for cleaning up some other areas of the code in the process.
0
 
Kevin CrossChief Technology OfficerCommented:
You are most welcome.  Glad I was able to help!
Best regards and happy coding,

Kevin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.