Solved

I don't want to Group By!

Posted on 2015-02-03
8
131 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
 
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
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.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading Integration Services 3 28
SQL profiler equivalent in MS-Access 3 45
1 FROM DUAL wont work with additional columns ?? 4 27
Sql query 107 30
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…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

27 Experts available now in Live!

Get 1:1 Help Now