Solved

Partition By?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

911 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

18 Experts available now in Live!

Get 1:1 Help Now