Solved

I don't want to Group By!

Posted on 2015-02-03
8
130 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
Comment Utility
HAVING forces you to use a GROUP BY.  Change HAVING to WHERE.
0
 

Author Comment

by:princeservice
Comment Utility
I tried that. Still doesn't work. See image.

screen
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
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
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
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
Comment Utility
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
Comment Utility
ste5an - How do I create a view without using the wizard?
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

11 Experts available now in Live!

Get 1:1 Help Now