?
Solved

I don't want to Group By!

Posted on 2015-02-03
8
Medium Priority
?
141 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
[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
  • 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
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
LVL 24

Accepted Solution

by:
Phillip Burton earned 2000 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 35

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 35

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this article I will describe the Backup & Restore 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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

765 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