Solved

I don't want to Group By!

Posted on 2015-02-03
8
137 Views
Last Modified: 2015-02-03
I want to create a view in sql server that will give me the total number of hours worked based on the work center and a date range. When using the query designer it keeps forcing me to use Group By. I don't want to group my results. I want a single number. Please see attached image. The SQL Statement is:

SELECT     dbo.Job_Operation.Work_Center, SUM(dbo.Job_Operation_Time.Act_Run_Hrs) AS total, dbo.Job_Operation_Time.Work_Date
FROM         dbo.Job_Operation INNER JOIN
                      dbo.Job_Operation_Time ON dbo.Job_Operation.Job_Operation = dbo.Job_Operation_Time.Job_Operation
GROUP BY dbo.Job_Operation.Work_Center, dbo.Job_Operation_Time.Work_Date
HAVING      (dbo.Job_Operation_Time.Work_Date BETWEEN CONVERT(DATETIME, '2015-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2015-01-31 00:00:00', 102)) AND 
                      (dbo.Job_Operation.Work_Center = 'welding')

Open in new window

Capture.JPG
0
Comment
Question by:princeservice
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:David Kroll
ID: 40586537
HAVING forces you to use a GROUP BY.  Change HAVING to WHERE.
0
 

Author Comment

by:princeservice
ID: 40586549
I tried that. Still doesn't work. See image.

screen
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40586553
and change

dbo.Job_Operation_Time.Work_Date BETWEEN CONVERT(DATETIME, '2015-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2015-01-31 00:00:00', 102)

to

dbo.Job_Operation_Time.Work_Date >='20150101' and dbo.Job_Operation_Time.Work_Date <'20150201'
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40586558
It's because you have a SUM. You have to have a GROUP BY because you have a  SUM or other summarize function.

Why not do:

SELECT     SUM(dbo.Job_Operation_Time.Act_Run_Hrs) AS total
FROM         dbo.Job_Operation INNER JOIN
                      dbo.Job_Operation_Time ON dbo.Job_Operation.Job_Operation = dbo.Job_Operation_Time.Job_Operation
WHERE dbo.Job_Operation_Time.Work_Date BETWEEN CONVERT(DATETIME, '2015-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2015-01-31 00:00:00', 102) AND (dbo.Job_Operation.Work_Center = 'welding')

Open in new window


if you just want a single figure?
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40586559
Then don't rely on wizards and write your statements by hand. E.g.

SELECT  SUM(JOT.Act_Run_Hrs) AS total
FROM    dbo.Job_Operation JO
        INNER JOIN dbo.Job_Operation_Time JOT ON JO.Job_Operation = JOT.Job_Operation
WHERE   JOT.Work_Date BETWEEN '20150101' AND '20150131 '
        AND JO.Work_Center = 'welding' ;

Open in new window

0
 
LVL 11

Expert Comment

by:David Kroll
ID: 40586566
SELECT     dbo.Job_Operation.Work_Center, 
(SELECT SUM(dbo.Job_Operation_Time.Act_Run_Hrs) 
 FROM dbo.Job_Operation_Time 
 WHERE  dbo.Job_Operation_Time.Job_Operation = dbo.Job_Operation.Job_Operation
 AND dbo.Job_Operation_Time.Work_Date BETWEEN CONVERT(DATETIME, '2015-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2015-01-31 00:00:00', 102)
) AS total, dbo.Job_Operation_Time.Work_Date
FROM   dbo.Job_Operation 
WHERE   (dbo.Job_Operation.Work_Center = 'welding')

Open in new window

0
 

Author Comment

by:princeservice
ID: 40586948
ste5an - How do I create a view without using the wizard?
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40587122
E.g.

CREATE VIEW yourViewName
AS
SELECT  SUM(JOT.Act_Run_Hrs) AS total
FROM    dbo.Job_Operation JO
        INNER JOIN dbo.Job_Operation_Time JOT ON JO.Job_Operation = JOT.Job_Operation
WHERE   JOT.Work_Date BETWEEN '20150101' AND '20150131 '
        AND JO.Work_Center = 'welding' ;

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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