Solved

SQL select statment (and more) question

Posted on 2011-03-25
3
279 Views
Last Modified: 2012-05-11
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)
0
Comment
Question by:mjburgard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 35220039
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
 
LVL 1

Author Closing Comment

by:mjburgard
ID: 35245012
Worked like a charm, and thanks for cleaning up some other areas of the code in the process.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 35245995
You are most welcome.  Glad I was able to help!
Best regards and happy coding,

Kevin
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Make the most of your online learning experience.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

691 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