[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Partition By?

Posted on 2013-01-27
4
Medium Priority
?
208 Views
Last Modified: 2013-02-01
My database records each of our engineers weekly activities. The following SQL gives me the raw data, but I want to group by 4 levels, EngName, WEDate, NominalLabourCode and TotalAdjusted. Giving me the total hours worked against the nominal Labour Code for each engineer for the given week ending date.

i.e. John Smith
          19 January 2013
               3022
                        22
               3096
                        12
        26 January 2013
               3144
                        6
               3022
                        7
   Tom Baker
        19 January 2013
               3096
                       4
etc, etc          

I have used partition by (with the great assistance of experts) for a single partition, but can this be applied to give me the results I need?


SELECT     TOP (100) PERCENT dbo.[WEDates].EngName, dbo.[WEDates].[WEDate], ISNULL(dbo.[WeeklyReportSubForm].[OvertimeHours], 0) * 1.5 + ISNULL(dbo.[WeeklyReportSubForm].[StandardHours], 0) + ISNULL(dbo.[WeeklyReportSubForm].[TravelTime], 0) AS TotalAdjusted, dbo.[WeeklyReportSubForm].[StandardHours], dbo.[WeeklyReportSubForm].[OvertimeHours], dbo.[WeeklyReportSubForm].[TravelTime], dbo.[WeeklyReportSubForm].NominalLabourCode

FROM         dbo.[WeeklyReportSubForm] INNER JOIN dbo.[WEDates] ON dbo.WeeklyReportSubForm].ID = dbo.[WEDates].ID

ORDER BY dbo.[WeeklyReportSubForm].NominalLabourCode DESC
0
Comment
Question by:SparkyP
[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
4 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38824348
It sounds like you need something like GROUP BY more than PARTITION BY, as in something like this:
SELECT  d.EngName,
        d.WEDate,
        ISNULL(r.OvertimeHours, 0) * 1.5 + ISNULL(r.StandardHours, 0) + ISNULL(r.TravelTime, 0) AS TotalAdjusted,
        r.NominalLabourCode,
        COUNT(*)
FROM    dbo.WeeklyReportSubForm r
        INNER JOIN dbo.WEDates d ON dbo.WeeklyReportSubForm.ID = d.ID
GROUP BY  
	ROLLUP(d.EngName, d.WEDate, r.NominalLabourCode, TotalAdjusted)
ORDER BY
	r.NominalLabourCode DESC

Open in new window


Notes:
Using aliases improves readability.
Using TOP 100 PERCENT ... ORDER BY is pointless.  Lose the TOP 100 PERCENT.
0
 

Author Comment

by:SparkyP
ID: 38824577
Many thanks for your response.

When I run the code it converts from your code to:

SELECT     TOP (100) PERCENT d.EngName, d.WEDate, ISNULL(r.OvertimeHours, 0) * 1.5 + ISNULL(r.StandardHours, 0) + ISNULL(r.TravelTime, 0)

                      AS TotalAdjusted, r.NominalLabourCode, COUNT(*) AS Expr1

FROM         dbo.WeeklyReportSubForm AS r INNER JOIN

                      dbo.WEDates AS d ON r.ID = d.ID

GROUP BY ROLLUP (d.EngName, d.WEDate, r.NominalLabourCode, TotalAdjusted)

ORDER BY r.NominalLabourCode DESC

  and I get an error message:  Invalid column name “TotalAdjusted”.

Please bear with me, I lack a lot of experience.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 1245 total points
ID: 38825352
When I run the code it converts from your code to:
That is because you are using the Designer.  Try and wean yourself from that tool as it is severely limited and crippled, instead use the Query window instead.

and I get an error message:  Invalid column name “TotalAdjusted”.
Try it this way (untested):
SELECT  d.EngName,
        d.WEDate,
        ISNULL(r.OvertimeHours, 0) * 1.5 + ISNULL(r.StandardHours, 0) + ISNULL(r.TravelTime, 0) AS TotalAdjusted,
        r.NominalLabourCode,
        COUNT(*)
FROM    dbo.WeeklyReportSubForm r
        INNER JOIN dbo.WEDates d ON dbo.WeeklyReportSubForm.ID = d.ID
GROUP BY  
	ROLLUP(d.EngName, d.WEDate, r.NominalLabourCode, ISNULL(r.OvertimeHours, 0) * 1.5 + ISNULL(r.StandardHours, 0) + ISNULL(r.TravelTime, 0) )
ORDER BY
	r.NominalLabourCode DESC

Open in new window

0
 

Author Closing Comment

by:SparkyP
ID: 38843523
Many thanks and apologies for the delay.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

656 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