Solved

Partition By?

Posted on 2013-01-27
4
202 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
  • 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 415 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

810 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