Solved

Partition By?

Posted on 2013-01-27
4
196 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

760 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

21 Experts available now in Live!

Get 1:1 Help Now