?
Solved

Partition By?

Posted on 2013-01-27
4
Medium Priority
?
206 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

770 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