| Author |
Topic |
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2005-08-31 : 16:20:15
|
| I'm trying to identify employees who have been paid their regular shift and who have also picked up someone elses shift on the same date. The two paid codes are 'REG' and 'CSW'.Here is the basic query for the Table:SELECT AutoTACurrentPayDaily.EmployeeID,AutoTACurrentPayDaily.PayDate,AutoTACurrentPayDaily.PayCode, AutoTACurrentPayDaily.PayHoursFROM AutoTACurrentPayDailyWHERE AutoTACurrentPayDaily.PayCode IN ('REG', 'CSW')This will give me a list of hours associated with the paycodes by employee and date, I'm stuck at getting the query to give me "only" the employees who have "both" paycodes on the same date.Any help is greatly apprecciated as alwaysThx.GC |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-08-31 : 16:33:13
|
| where AutoTACurrentPayDaily.EmployeeID in(SELECT AutoTACurrentPayDaily.EmployeeIDFROM AutoTACurrentPayDailyWHERE AutoTACurrentPayDaily.PayCode IN ('REG', 'CSW')group by AutoTACurrentPayDaily.EmployeeIDhaving count(distinct AutoTACurrentPayDaily.PayCode) = 2)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2005-08-31 : 16:34:03
|
SELECT EmployeeID, PayDate, PayCode, PayHours, COUNT(*)FROM AutoTACurrentPayDailyWHERE PayCode IN ('REG', 'CSW')GROUP BY EmployeeID, PayDate, PayCode, PayHoursHAVING COUNT(*) > 2Edit: Damn! again. NR's method will give you more flexibility to retrieve other fields.---------------------------EmeraldCityDomains.com |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-08-31 : 16:44:28
|
| [code]select a.EmployeeID, a.PayDate, a.PayCode, a.PayHoursfrom AutoTACurrentPayDaily regjoin AutoTACurrentPayDaily cswon reg.EmployeeID=csw.EmployeeID and reg.PayDate=csw.PayDate and reg.PayCode='REG' and csw.PayCode='CSW'join AutoTACurrentPayDaily aon reg.EmployeeID=a.EmployeeID and reg.PayDate=a.PayDate [/code] |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2005-08-31 : 18:46:03
|
| No luck with any of the replys. Syntax errors or result comes up empty.Here is the query adjusted for one employee for the month of July:SELECT [AutoTACurrentPayDaily].[EmployeeID] as 'EMPID',[AutoTACurrentPayDaily].[CostCenter] as 'CENTER', [AutoTACurrentPayDaily].[PayDate] as 'DATE',[AutoTACurrentPayDaily].[PayCode] as 'PAYCODE',[AutoTACurrentPayDaily].[PayHours] as 'HOURS'FROM [AutoTACurrentPayDaily]WHERE [AutoTACurrentPayDaily].[CompanyCode] = 'AA'AND [AutoTACurrentPayDaily].[PayCode]IN('REG', 'CSW')AND [AutoTACurrentPayDaily].[PayHours]>'0'AND [AutoTACurrentPayDaily].[PayDate]<=[AutoTACurrentPayDaily].[PayPeriodEnd]AND DATEPART (MM,AutoTACurrentPayDaily.PayDate)= '07'AND DATEPART (YY,PayDate)= '2005'AND [AutoTACurrentPayDaily].[EmployeeID]= '00045932'AND SUBSTRING ([AutoTACurrentPayDaily].[CostCenter], 1, 4) IN ('0600', '0691', '0752')AND SUBSTRING ([AutoTACurrentPayDaily].[CostCenter], 6, 4) IN ('7930', '7935')GROUP BY [AutoTACurrentPayDaily].[EmployeeID],[AutoTACurrentPayDaily].[CostCenter], [AutoTACurrentPayDaily].[PayDate],[AutoTACurrentPayDaily].[PayCode],[AutoTACurrentPayDaily].[PayHours]Here are the results for the employee: EMPID ...... CENTER ............ DATE ................ PayCode..Hours00045932 ...... 0600/7935 ..... 2005-07-22 00:00:00.000 .. CSW ..... 800045932 ...... 0600/7935 ..... 2005-07-23 00:00:00.000 .. REG ..... 800045932 ...... 0600/7935 ..... 2005-07-29 00:00:00.000 .. CSW ..... 800045932 ...... 0600/7935 ..... 2005-07-29 00:00:00.000 .. REG ..... 800045932 ...... 0600/7935 ..... 2005-07-30 00:00:00.000 .. CSW ..... 800045932 ...... 0600/7935 ..... 2005-07-30 00:00:00.000 .. REG ..... 8I would only like to see the results for July 29 and 30 due to the two shifts worked on those days.Thanks again.GC |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-08-31 : 19:16:53
|
| [code]declare @t table(EmployeeID varchar(10),CostCenter varchar(10),PayDate datetime,PayCode varchar(3),PayHours int)insert @tselect '00045932','0600/7935' ,'20050722 00:00:00.000' ,'CSW',8 unionselect '00045932','0600/7935' ,'20050723 00:00:00.000','REG',8 unionselect '00045932','0600/7935','20050729 00:00:00.000','CSW' ,8 unionselect '00045932' ,'0600/7935','20050729 00:00:00.000','REG',8 unionselect '00045932' ,'0600/7935','20050730 00:00:00.000','CSW',8 unionselect '00045932' ,'0600/7935','20050730 00:00:00.000','REG',8select a.EmployeeID, a.CostCenter, a.PayDate, a.PayCode, a.PayHoursfrom @t regjoin @t cswon reg.EmployeeID=csw.EmployeeID and reg.PayDate=csw.PayDate and reg.PayCode='REG' and csw.PayCode='CSW'join @t aon reg.EmployeeID=a.EmployeeID and reg.PayDate=a.PayDate [/code]result from QA00045932 0600/7935 2005-07-29 00:00:00.000 CSW 800045932 0600/7935 2005-07-29 00:00:00.000 REG 800045932 0600/7935 2005-07-30 00:00:00.000 CSW 800045932 0600/7935 2005-07-30 00:00:00.000 REG 8what is wrong ? |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2005-08-31 : 19:44:58
|
| Gary, In your GROUP BY you left off the having count(distinct AutoTACurrentPayDaily.PayCode) = 2 clause.---------------------------EmeraldCityDomains.com |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2005-09-01 : 17:22:44
|
| Still not having any luck.Mark, I added the "having count distinct" line as suggested and the result comes back zero. I have also tried Paydate as well.Vlad, your code gives me results, but I get an echo of the same rows for 7/30. Here is the code you gave me and modified to pull what I need.select a.EmployeeID, a.PayDate, a.PayCode, a.PayHoursfrom AutoTACurrentPayDaily regjoin AutoTACurrentPayDaily cswon reg.EmployeeID=csw.EmployeeID and reg.PayDate=csw.PayDate and reg.PayCode='REG' and csw.PayCode='CSW'join AutoTACurrentPayDaily aon reg.EmployeeID=a.EmployeeID and reg.PayDate=a.PayDate WHERE a.CompanyCode = 'AA'AND a.PayCode IN ('REG', 'CSW')AND a.PayHours >'0'AND a.PayDate <= a.PayPeriodEndAND DATEPART (MM,a.PayDate)= '07'AND DATEPART (YY,a.PayDate)= '2005'AND a.EmployeeID = '00000702'AND SUBSTRING (a.CostCenter, 1, 4) IN ('0600', '0691', '0752')AND SUBSTRING (a.CostCenter, 6, 4) IN ('7930', '7935')Here are the results.00000702 2005-07-29 00:00:00.000 CSW 800000702 2005-07-29 00:00:00.000 REG 800000702 2005-07-30 00:00:00.000 CSW 800000702 2005-07-30 00:00:00.000 CSW 800000702 2005-07-30 00:00:00.000 REG 800000702 2005-07-30 00:00:00.000 REG 800000702 2005-07-30 00:00:00.000 CSW 800000702 2005-07-30 00:00:00.000 CSW 800000702 2005-07-30 00:00:00.000 REG 800000702 2005-07-30 00:00:00.000 REG 8Thanks again for everyones help.Gary |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-09-02 : 06:03:15
|
| 2 GaryI think that this because of field CostCentertryselect distinct a.EmployeeID, a.PayDate, a.PayCode, a.PayHoursFROM ... |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2005-09-02 : 13:04:58
|
| Vlad,The distinct worked!! Thank you very much for all your help with this. There are still a couple of rows being returned that don't have the REG / CSW match, but I can delete them if needed. Here is an example of one employee:00054515 .. 0600/7930 .. 2005-07-03 00:00:00.000 .. CSW .. 100054515 .. 0600/7930 .. 2005-07-03 00:00:00.000 .. REG .. 700054515 .. 0600/7930 .. 2005-07-05 00:00:00.000 .. REG .. 700054515 .. 0600/7930 .. 2005-07-10 00:00:00.000 .. REG .. 700054515 .. 0600/7930 .. 2005-07-11 00:00:00.000 .. REG .. 700054515 .. 0600/7930 .. 2005-07-13 00:00:00.000 .. REG .. 700054515 .. 0600/7930 .. 2005-07-19 00:00:00.000 .. REG .. 700054515 .. 0600/7930 .. 2005-07-23 00:00:00.000 .. REG .. 700054515 .. 0600/7930 .. 2005-07-25 00:00:00.000 .. REG .. 700054515 .. 0600/7930 .. 2005-07-27 00:00:00.000 .. REG .. 7As you can see they had the two codes on July 03 and for some reason I am also getting the other work days as well but only if their hours worked for the day are 7. Any days where they worked 8 hours or more is being ignored which is strange??If I can ask one more favor, is it possible to add a HAVING SUM filter so that I can only see employees who have a combined total of 80 hours or more for the two paycodes? Right now I get a list of 894 employees who have doubled up on the two paycodes, but some have only done it once for a total of 9 hours (reg = 8 / csw = 1) and I would like to eliminate them and only focus on the bigger numbers for the month. Ie (reg = 46 / csw = 42)Thanks again Vlad for your help, all the best!Gary |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2005-09-02 : 14:08:05
|
| Gary, the reason that the HAVING COUNT did not work is that you are GROUPING BY PayCode and then COUNTING PayCode and so the count was always coming out as 1.From looking over the code you have been using, it looks like your requirements have expanded from just identifying those employees who have done this, to then showing the individual lines (NOT aggregated totals) of their schedule for those days where it happened. Is that correct? What exactly are you looking for? If you'll give us some DML and DDL to create a test table with data, and identify the expected results, we could really whip something up good for you.I think your joining the table a third time is giving you excess results records.---------------------------EmeraldCityDomains.com |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-09-02 : 16:26:46
|
| [code]declare @e table(EmployeeID varchar(10),CostCenter varchar(10),PayDate datetime,PayCode varchar(3),PayHours int)insert @eselect a.EmployeeID, a.CostCenter, a.PayDate, a.PayCode, a.PayHoursfrom AutoTACurrentPayDaily ajoin ( select EmployeeID,PayDate from AutoTACurrentPayDaily where PayCode in ('REG','CSW') group by EmployeeID,PayDate having count(distinct paycode)=2 ) bon a.EmployeeID=b.EmployeeID and a.PayDate=b.paydateselect a.EmployeeID, a.CostCenter, a.PayDate, a.PayCode, a.PayHoursfrom @e ajoin (select EmployeeID,month(paydate)mo, sum(payhours) h from @e group by EmployeeID,month(paydate) having sum(payhours)>=80 ) bon a.EmployeeID=b.EmployeeID and month(a.paydate)=b.mo[/code]If I have correctly understoodP.S I agree with AjarnMark :"If you'll give us some DML and DDL to create a test table with data, and identify the expected results, we could really whip something up good for you." |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2005-09-02 : 16:55:41
|
| Mark, Vlad,Thank you both for your assistance with this! Yes, you are correct. I'm looking for only the days where the employee has worked a REG shift and CSW shift on the same day. In the example below employee # 12824 has "doubled up" on July 03, 10, 11, 17, 18, 24, 25, and 31. While employee # 54515 has only done it once on July 3rd. The query / example that VIG "VLAD" sent started working once I put the word 'distinct' in by the EmployeeID. But, as you can see above I am still getting a little "bleed" of Reg hours that have a value of 7 for some reason?? (I just tested the DDL / DML below on another server and didn't get the bleed data. The original table has several million rows of data so maybe the "Cruching" is causing the extra data to appear??) If at all possible I would only like to see employees who have a "combined' hours value that is greater than 80 hours. Right now with Vlad's query I get a list of 894 employees who have doubled up on the two paycodes, but some have only done it once in July for a total of 9 hours (reg = 8 / csw = 1) and I would like to eliminate them and only focus on the bigger numbers for the month. Ie (reg = 46 / csw = 46) Here is the DDL and DML.CREATE TABLE AutoTACurrentPayDaily (CompanyCode CHAR(2) NOT NULL,EmployeeID INT NOT NULL,CostCenter CHAR(9) NOT NULL,PayPeriodStart DateTime NOT NULL,PayPeriodEnd DateTime NOT NULL,PayDate DateTime NOT NULL,PayCode CHAR(3) NOT NULL,PayHours INT NOT NULL)INSERT AutoTACurrentPayDaily SELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-03 00:00:00.000','CSW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-03 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-04 00:00:00.000','CSW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-04 00:00:00.000','HW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-05 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-06 00:00:00.000','CSO','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-07 00:00:00.000','CSO','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-10 00:00:00.000','CSW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-10 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-11 00:00:00.000','CSW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-11 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-12 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-13 00:00:00.000','CSO','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-14 00:00:00.000','CSO','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-17 00:00:00.000','CSW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-17 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-18 00:00:00.000','CSW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-18 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-19 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-20 00:00:00.000','CSO','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-21 00:00:00.000','CSO','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-24 00:00:00.000','CSW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-24 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-25 00:00:00.000','CSW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-25 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-26 00:00:00.000','REG','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-27 00:00:00.000','CSO','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-28 00:00:00.000','CSO','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-30 00:00:00.000','2005-08-12 00:00:00.000','2005-07-31 00:00:00.000','CSW','8' UNIONSELECT 'AA','00012824','0600/7930','2005-07-30 00:00:00.000','2005-08-12 00:00:00.000','2005-07-31 00:00:00.000','REG','8' UNIONSELECT 'AA','00054515','0600/7930','2005-06-18 00:00:00.000','2005-07-01 00:00:00.000','2005-07-01 00:00:00.000','HC','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-02 00:00:00.000','HC','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-03 00:00:00.000','CSO','1' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-03 00:00:00.000','CSW','1' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-03 00:00:00.000','REG','7' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-04 00:00:00.000','HO','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-05 00:00:00.000','REG','7' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-06 00:00:00.000','REG','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-09 00:00:00.000','REG','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-10 00:00:00.000','REG','7' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-11 00:00:00.000','REG','7' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-12 00:00:00.000','REG','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-02 00:00:00.000','2005-07-15 00:00:00.000','2005-07-13 00:00:00.000','REG','7' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-16 00:00:00.000','HC','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-17 00:00:00.000','HC','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-18 00:00:00.000','REG','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-19 00:00:00.000','REG','7' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-20 00:00:00.000','REG','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-23 00:00:00.000','REG','7' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-24 00:00:00.000','REG','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-25 00:00:00.000','REG','7' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-26 00:00:00.000','REG','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-16 00:00:00.000','2005-07-29 00:00:00.000','2005-07-27 00:00:00.000','REG','7' UNIONSELECT 'AA','00054515','0600/7930','2005-07-30 00:00:00.000','2005-08-12 00:00:00.000','2005-07-30 00:00:00.000','CSO','8' UNIONSELECT 'AA','00054515','0600/7930','2005-07-30 00:00:00.000','2005-08-12 00:00:00.000','2005-07-31 00:00:00.000','CSO','8'Here is the basic query for the data.SELECT [AutoTACurrentPayDaily].[CompanyCode] as 'CompanyCode',[AutoTACurrentPayDaily].[EmployeeID] as 'EMPID',[AutoTACurrentPayDaily].[CostCenter] as 'CENTER',[AutoTACurrentPayDaily].[PayPeriodStart] as 'StartDate',[AutoTACurrentPayDaily].[PayPeriodEnd] as 'EndDate', [AutoTACurrentPayDaily].[PayDate] as 'DATE',[AutoTACurrentPayDaily].[PayCode] as 'PAYCODE',[AutoTACurrentPayDaily].[PayHours] as 'HOURS'FROM [AutoTACurrentPayDaily]WHERE [AutoTACurrentPayDaily].[CompanyCode] = 'AA'AND [AutoTACurrentPayDaily].[PayHours]>'0'AND [AutoTACurrentPayDaily].[PayDate]<=[AutoTACurrentPayDaily].[PayPeriodEnd]AND DATEPART (MM,AutoTACurrentPayDaily.PayDate)= '07'AND DATEPART (YY,PayDate)= '2005'AND [AutoTACurrentPayDaily].[EmployeeID]= '00012824'AND SUBSTRING ([AutoTACurrentPayDaily].[CostCenter], 1, 4) IN ('0600', '0691', '0752')AND SUBSTRING ([AutoTACurrentPayDaily].[CostCenter], 6, 4) IN ('7930', '7935')GROUP BY [AutoTACurrentPayDaily].[CompanyCode], [AutoTACurrentPayDaily].[EmployeeID],[AutoTACurrentPayDaily].[CostCenter],[AutoTACurrentPayDaily].[PayPeriodStart],[AutoTACurrentPayDaily].[PayPeriodEnd], [AutoTACurrentPayDaily].[PayDate],[AutoTACurrentPayDaily].[PayCode],[AutoTACurrentPayDaily].[PayHours]ORDER BY [AutoTACurrentPayDaily].[PayDate] ASCHere is Vlad's query:SELECT DISTINCT a.EmployeeID as 'EMPID',a.CostCenter as 'COSTCENTER',a.PayDate as 'DATE',a.PayCode as 'CODE',a.PayHours as 'HOURS'FROM AutoTACurrentPayDaily regJOIN AutoTACurrentPayDaily csw ON reg.EmployeeID=csw.EmployeeID and reg.PayDate=csw.PayDate and reg.PayCode='REG' and csw.PayCode='CSW'JOIN AutoTACurrentPayDaily a ON reg.EmployeeID=a.EmployeeID and reg.PayDate=a.PayDate WHERE a.CompanyCode = 'AA'AND a.PayCode IN ('REG', 'CSW')AND a.PayHours >'0'AND a.PayDate <= a.PayPeriodEndAND DATEPART (MM,a.PayDate)= '07'AND DATEPART (YY,a.PayDate)= '2005'AND SUBSTRING (a.CostCenter, 1, 4) IN ('0600', '0691', '0702', '0752', '0801', '0841')AND SUBSTRING (a.CostCenter, 6, 4) IN ('7930', '7935')GROUP BY a.EmployeeID,a.CostCenter,a.PayDate,a.PayCode,a.PayHoursThanks again guys for all your help with this!Gary |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2005-09-02 : 17:00:47
|
| Vlad,I just tried your new code and this is what came back.Server: Msg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'table'.Server: Msg 170, Level 15, State 1, Line 27Line 27: Incorrect syntax near '@e'.Server: Msg 170, Level 15, State 1, Line 30Line 30: Incorrect syntax near '@e'.Thanks.Gary |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-09-02 : 17:42:29
|
It's very strange. I have checked my last query with your data and here that I have receiveddeclare @e table(EmployeeID varchar(10),CostCenter varchar(10),PayDate datetime,PayCode varchar(3),PayHours int)insert @eselect a.EmployeeID, a.CostCenter, a.PayDate, a.PayCode, a.PayHoursfrom AutoTACurrentPayDaily ajoin ( select EmployeeID,PayDate from AutoTACurrentPayDaily where PayCode in ('REG','CSW') group by EmployeeID,PayDate having count(distinct paycode)=2 ) bon a.EmployeeID=b.EmployeeID and a.PayDate=b.paydateselect a.EmployeeID, a.CostCenter, a.PayDate, a.PayCode, a.PayHoursfrom @e ajoin (select EmployeeID,month(paydate)mo, sum(payhours) h from @e group by EmployeeID,month(paydate) having sum(payhours)>=80 ) bon a.EmployeeID=b.EmployeeID and month(a.paydate)=b.moEmployeeID CostCenter PayDate PayCode PayHours ---------- ---------- ------------------------------------------------------ ------- ----------- 12824 0600/7930 2005-07-03 00:00:00.000 CSW 812824 0600/7930 2005-07-03 00:00:00.000 REG 812824 0600/7930 2005-07-10 00:00:00.000 CSW 812824 0600/7930 2005-07-10 00:00:00.000 REG 812824 0600/7930 2005-07-11 00:00:00.000 CSW 812824 0600/7930 2005-07-11 00:00:00.000 REG 812824 0600/7930 2005-07-17 00:00:00.000 CSW 812824 0600/7930 2005-07-17 00:00:00.000 REG 812824 0600/7930 2005-07-18 00:00:00.000 CSW 812824 0600/7930 2005-07-18 00:00:00.000 REG 812824 0600/7930 2005-07-24 00:00:00.000 CSW 812824 0600/7930 2005-07-24 00:00:00.000 REG 812824 0600/7930 2005-07-25 00:00:00.000 CSW 812824 0600/7930 2005-07-25 00:00:00.000 REG 812824 0600/7930 2005-07-31 00:00:00.000 CSW 812824 0600/7930 2005-07-31 00:00:00.000 REG 8(16 row(s) affected)You should add corresponding restrictions only to where clause |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2005-09-06 : 17:55:15
|
| Vlad,Thanks again for all your help with this. I tried your code and there is still a repeat (echo) effect of some paydates which may be caused by having duplicate data for the same PayDate but with different PayPeriodStart / PayPeriodEnd dates which I am trying to filter out in the Where clause.Here is another series of DDL and DML code for four employees.CREATE TABLE AutoTACurrentPayDaily (CompanyCode CHAR(2) NOT NULL,EmployeeID INT NOT NULL,CostCenter CHAR(9) NOT NULL,PayPeriodStart DateTime NOT NULL,PayPeriodEnd DateTime NOT NULL,PayCode CHAR(3) NOT NULL,PayDate DateTime NOT NULL,PayHours INT NOT NULL)INSERT AutoTACurrentPayDaily SELECT 'AA','00000702','0600/7935','06/18/2005','07/01/2005','S2T','07/01/2005','8' UNIONSELECT 'AA','00000702','0600/7935','06/18/2005','07/01/2005','VC','07/01/2005','8' UNIONSELECT 'AA','00000702','0600/7935','06/18/2005','07/01/2005','S2T','07/04/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/04/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/04/2005','8' UNIONSELECT 'AA','00000702','0600/7935','06/18/2005','07/01/2005','VC','07/04/2005','8' UNIONSELECT 'AA','00000702','0600/7935','06/18/2005','07/01/2005','S2T','07/05/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/05/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/05/2005','8' UNIONSELECT 'AA','00000702','0600/7935','06/18/2005','07/01/2005','VC','07/05/2005','8' UNIONSELECT 'AA','00000702','0600/7935','06/18/2005','07/01/2005','S2T','07/06/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/06/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/06/2005','8' UNIONSELECT 'AA','00000702','0600/7935','06/18/2005','07/01/2005','VC','07/06/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/07/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/07/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/08/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/08/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/11/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/11/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/12/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/12/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/13/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/13/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/14/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/14/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/15/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','VC','07/15/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','AAU','07/16/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','AAU','07/16/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','AAU','07/17/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','AAU','07/17/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','CSO','07/18/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSO','07/18/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','CSO','07/19/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSO','07/19/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSO','07/20/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','HO','07/20/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/02/2005','07/15/2005','S2T','07/20/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSO','07/21/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSW','07/22/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','HW','07/22/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','S2T','07/22/2005','16' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','HC','07/23/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','REG','07/23/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','S2T','07/23/2005','16' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','HC','07/24/2005','16' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','S2T','07/24/2005','16' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSO','07/27/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSO','07/28/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSW','07/29/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','REG','07/29/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','S2T','07/29/2005','16' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSW','07/30/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/30/2005','08/12/2005','CSW','07/30/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/30/2005','08/12/2005','REG','07/30/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','REG','07/30/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','S2T','07/30/2005','16' UNIONSELECT 'AA','00000702','0600/7935','07/30/2005','08/12/2005','S2T','07/30/2005','16' UNIONSELECT 'AA','00000702','0600/7935','07/16/2005','07/29/2005','CSO','07/31/2005','8' UNIONSELECT 'AA','00000702','0600/7935','07/30/2005','08/12/2005','CSO','07/31/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','CSO','07/01/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','MRT','07/02/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','MRT','07/02/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','PV','07/02/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','PV','07/02/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','S4T','07/02/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','S4T','07/02/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','MRT','07/03/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','MRT','07/03/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','PV','07/03/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','PV','07/03/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','S4T','07/03/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','S4T','07/03/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSO','07/04/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','HO','07/04/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','MRT','07/04/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','S4T','07/04/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','HW','07/05/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','MRT','07/05/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','MRT','07/05/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','REG','07/05/2005','8' UNIONSELECT 'AA','00000958','0752/7930','06/18/2005','07/01/2005','S4T','07/05/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','S4T','07/05/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSO','07/08/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSO','07/09/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','MRT','07/10/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','REG','07/10/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','S4T','07/10/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSW','07/11/2005','4' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','MRT','07/11/2005','12' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','REG','07/11/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','S4T','07/11/2005','12' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSW','07/12/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','MRT','07/12/2005','16' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','REG','07/12/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','S4T','07/12/2005','16' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSW','07/13/2005','3' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','MRT','07/13/2005','11' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','REG','07/13/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','S4T','07/13/2005','11' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSO','07/16/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSO','07/16/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSO','07/17/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSO','07/17/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSO','07/18/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSO','07/18/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSW','07/18/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSW','07/18/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','MRT','07/18/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','MRT','07/18/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','S4T','07/18/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','S4T','07/18/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','CSW','07/19/2005','7' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSW','07/19/2005','7' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','MRT','07/19/2005','15' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','MRT','07/19/2005','15' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','REG','07/19/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','REG','07/19/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','S4T','07/19/2005','15' UNIONSELECT 'AA','00000958','0752/7930','07/02/2005','07/15/2005','S4T','07/19/2005','15' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','MRT','07/20/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','REG','07/20/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','S4T','07/20/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSO','07/23/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','MRT','07/24/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','REG','07/24/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','S4T','07/24/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSW','07/25/2005','4' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','MRT','07/25/2005','12' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','REG','07/25/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','S4T','07/25/2005','12' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','MRT','07/26/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','REG','07/26/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','S4T','07/26/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSW','07/27/2005','7' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','MRT','07/27/2005','15' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','REG','07/27/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','S4T','07/27/2005','15' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSO','07/30/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/30/2005','08/12/2005','CSO','07/30/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/16/2005','07/29/2005','CSO','07/31/2005','8' UNIONSELECT 'AA','00000958','0752/7930','07/30/2005','08/12/2005','CSO','07/31/2005','8' UNIONSELECT 'AA','00012486','0600/7930','06/18/2005','07/01/2005','REG','07/01/2005','8' UNIONSELECT 'AA','00012486','0600/7930','06/18/2005','07/01/2005','VC','07/04/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','VC','07/04/2005','8' UNIONSELECT 'AA','00012486','0600/7930','06/18/2005','07/01/2005','VC','07/05/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','VC','07/05/2005','8' UNIONSELECT 'AA','00012486','0600/7930','06/18/2005','07/01/2005','VC','07/06/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','VC','07/06/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','VC','07/07/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','VC','07/08/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','HW','07/11/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','HC','07/12/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','REG','07/12/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','CSO','07/13/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','REG','07/14/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','CSW','07/15/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','REG','07/15/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','REG','07/18/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','REG','07/18/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','CSW','07/19/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','CSW','07/19/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','REG','07/19/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/02/2005','07/15/2005','REG','07/19/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','CSO','07/20/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','CSW','07/21/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','REG','07/21/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','TRD','07/22/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','REG','07/25/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','CSW','07/26/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','REG','07/26/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','CSO','07/27/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','CSW','07/28/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','REG','07/28/2005','8' UNIONSELECT 'AA','00012486','0600/7930','07/16/2005','07/29/2005','REG','07/29/2005','8' UNIONSELECT 'AA','00054515','0600/7930','06/18/2005','07/01/2005','HC','07/01/2005','8' UNIONSELECT 'AA','00054515','0600/7930','06/18/2005','07/01/2005','HC','07/02/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','HC','07/02/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSO','07/03/2005','1' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSW','07/03/2005','1' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/03/2005','7' UNIONSELECT 'AA','00054515','0600/7930','06/18/2005','07/01/2005','HO','07/04/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','HO','07/04/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSO','07/05/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSW','07/05/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/05/2005','7' UNIONSELECT 'AA','00054515','0600/7930','06/18/2005','07/01/2005','REG','07/06/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/06/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/09/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSO','07/10/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSW','07/10/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/10/2005','7' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','SKL','07/10/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSO','07/11/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSW','07/11/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/11/2005','7' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/12/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSO','07/13/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSW','07/13/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/13/2005','7' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','HC','07/16/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','HC','07/16/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','HC','07/17/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','HC','07/17/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/18/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','REG','07/18/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSO','07/19/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSO','07/19/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSW','07/19/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','CSW','07/19/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/19/2005','7' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','REG','07/19/2005','7' UNIONSELECT 'AA','00054515','0600/7930','07/02/2005','07/15/2005','REG','07/20/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','REG','07/20/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSO','07/23/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSW','07/23/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','REG','07/23/2005','7' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','REG','07/24/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSO','07/25/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSW','07/25/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','REG','07/25/2005','7' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','REG','07/26/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSO','07/27/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSW','07/27/2005','0' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','REG','07/27/2005','7' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSO','07/30/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/30/2005','08/12/2005','CSO','07/30/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/16/2005','07/29/2005','CSO','07/31/2005','8' UNIONSELECT 'AA','00054515','0600/7930','07/30/2005','08/12/2005','CSO','07/31/2005','8'Here is your code where I have altered the where clause, but I'm still getting some extra rows with paycodes that we have not asked for.declare @e table(EmployeeID varchar(10),CostCenter varchar(10),PayDate datetime,PayCode varchar(3),PayHours int)insert @eselect a.EmployeeID, a.CostCenter, a.PayDate, a.PayCode, a.PayHoursfrom AutoTACurrentPayDaily ajoin ( select EmployeeID,PayDate from AutoTACurrentPayDaily WHERE a.CompanyCode = 'AA' AND a.PayCode IN ('REG', 'CSW') AND a.PayHours >'0' AND a.PayDate <= a.PayPeriodEnd AND DATEPART (MM,a.PayDate)= '07' AND DATEPART (YY,a.PayDate)= '2005' AND SUBSTRING (a.CostCenter, 1, 4) IN ('0600', '0691', '0702', '0752', '0801', '0841') AND SUBSTRING (a.CostCenter, 6, 4) IN ('7930', '7935') group by EmployeeID,PayDate having count(distinct paycode)=2 ) bon a.EmployeeID=b.EmployeeID and a.PayDate=b.paydateselect a.EmployeeID, a.CostCenter, a.PayDate, a.PayCode, a.PayHoursfrom @e ajoin (select EmployeeID,month(paydate)mo, sum(payhours) h from @e group by EmployeeID,month(paydate) having sum(payhours)>=80 ) bon a.EmployeeID=b.EmployeeID and month(a.paydate)=b.moHere is what I am getting back from the query.EmployeeID CostCenter PayDate PayCode PayHours ---------- ---------- ------------------------------------------------------ ------- ----------- 00000702 0600/7935 2005-07-29 00:00:00.000 CSW 800000702 0600/7935 2005-07-29 00:00:00.000 REG 800000702 0600/7935 2005-07-29 00:00:00.000 S2T 1600000702 0600/7935 2005-07-30 00:00:00.000 CSW 800000702 0600/7935 2005-07-30 00:00:00.000 REG 800000702 0600/7935 2005-07-30 00:00:00.000 S2T 1600000702 0600/7935 2005-07-30 00:00:00.000 CSW 800000702 0600/7935 2005-07-30 00:00:00.000 REG 800000702 0600/7935 2005-07-30 00:00:00.000 S2T 1600000958 0752/7930 2005-07-25 00:00:00.000 CSW 400000958 0752/7930 2005-07-25 00:00:00.000 MRT 1200000958 0752/7930 2005-07-25 00:00:00.000 REG 800000958 0752/7930 2005-07-25 00:00:00.000 S4T 1200000958 0752/7930 2005-07-27 00:00:00.000 CSW 700000958 0752/7930 2005-07-27 00:00:00.000 MRT 1500000958 0752/7930 2005-07-27 00:00:00.000 REG 800000958 0752/7930 2005-07-27 00:00:00.000 S4T 1500000958 0752/7930 2005-07-11 00:00:00.000 CSW 400000958 0752/7930 2005-07-11 00:00:00.000 MRT 1200000958 0752/7930 2005-07-11 00:00:00.000 REG 800000958 0752/7930 2005-07-11 00:00:00.000 S4T 1200000958 0752/7930 2005-07-12 00:00:00.000 CSW 800000958 0752/7930 2005-07-12 00:00:00.000 MRT 1600000958 0752/7930 2005-07-12 00:00:00.000 REG 800000958 0752/7930 2005-07-12 00:00:00.000 S4T 1600000958 0752/7930 2005-07-13 00:00:00.000 CSW 300000958 0752/7930 2005-07-13 00:00:00.000 MRT 1100000958 0752/7930 2005-07-13 00:00:00.000 REG 800000958 0752/7930 2005-07-13 00:00:00.000 S4T 1100000958 0752/7930 2005-07-19 00:00:00.000 CSW 700000958 0752/7930 2005-07-19 00:00:00.000 MRT 1500000958 0752/7930 2005-07-19 00:00:00.000 REG 800000958 0752/7930 2005-07-19 00:00:00.000 S4T 1500000958 0752/7930 2005-07-19 00:00:00.000 CSW 700000958 0752/7930 2005-07-19 00:00:00.000 MRT 1500000958 0752/7930 2005-07-19 00:00:00.000 REG 800000958 0752/7930 2005-07-19 00:00:00.000 S4T 1500012486 0600/7930 2005-07-19 00:00:00.000 CSW 800012486 0600/7930 2005-07-19 00:00:00.000 REG 800012486 0600/7930 2005-07-19 00:00:00.000 CSW 800012486 0600/7930 2005-07-19 00:00:00.000 REG 800012486 0600/7930 2005-07-21 00:00:00.000 CSW 800012486 0600/7930 2005-07-21 00:00:00.000 REG 800012486 0600/7930 2005-07-15 00:00:00.000 CSW 800012486 0600/7930 2005-07-15 00:00:00.000 REG 800012486 0600/7930 2005-07-28 00:00:00.000 CSW 800012486 0600/7930 2005-07-28 00:00:00.000 REG 800012486 0600/7930 2005-07-26 00:00:00.000 CSW 800012486 0600/7930 2005-07-26 00:00:00.000 REG 8The restrictions need to be added as the table has millions of rows of data and I need to be able to focus on a specific date (Month / Year) and workgroup (CostCenter) that can be broken down into two parts. Have I put the restrictions in the right area??Thanks again.Gary |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-09-06 : 21:18:15
|
quote: Originally posted by Gary Costigan Vlad,I just tried your new code and this is what came back.Server: Msg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'table'.Server: Msg 170, Level 15, State 1, Line 27Line 27: Incorrect syntax near '@e'.Server: Msg 170, Level 15, State 1, Line 30Line 30: Incorrect syntax near '@e'.Thanks.Gary
I suspect Gary is using a sql version prior to 2000. (or his database has a compatability level is set to 70 or less) It doesn't know how to deal with table variablesBe One with the OptimizerTG |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-09-07 : 00:58:03
|
Gary,you wrote quote: which may be caused by having duplicate data for the same PayDate but with different PayPeriodStart / PayPeriodEnd dates
But what is necessary to do in this case?for example we have:AA 958 0752/7930 2005-07-02 00:00:00.000 2005-07-15 00:00:00.000 CSW 2005-07-19 00:00:00.000 7AA 958 0752/7930 2005-07-16 00:00:00.000 2005-07-29 00:00:00.000 CSW 2005-07-19 00:00:00.000 7AA 958 0752/7930 2005-07-16 00:00:00.000 2005-07-29 00:00:00.000 REG 2005-07-19 00:00:00.000 8AA 958 0752/7930 2005-07-02 00:00:00.000 2005-07-15 00:00:00.000 REG 2005-07-19 00:00:00.000 8Whether it is necessary to summarize payhours or not?declare @e table(EmployeeID varchar(10),CostCenter varchar(10),PayDate datetime,PayCode varchar(3),PayHours int)insert @eselect a.EmployeeID,a.CostCenter,a.PayDate,a.PayCode,sum(a.PayHours) PayHours /* or a.PayHours */from AutoTACurrentPayDaily ajoin(select EmployeeID,PayDatefrom AutoTACurrentPayDaily a1WHERE a1.CompanyCode = 'AA'AND a1.PayCode IN ('REG', 'CSW')AND a1.PayHours >'0'AND a1.PayDate <= a1.PayPeriodEndAND DATEPART (MM,a1.PayDate)= '07'AND DATEPART (YY,a1.PayDate)= '2005'AND SUBSTRING (a1.CostCenter, 1, 4) IN ('0600', '0691', '0702', '0752', '0801', '0841')AND SUBSTRING (a1.CostCenter, 6, 4) IN ('7930', '7935')group by EmployeeID,PayDate having count(distinct paycode)=2) bon a.EmployeeID=b.EmployeeID and a.PayDate=b.paydate AND a.PayCode IN ('REG', 'CSW')group by a.EmployeeID,a.CostCenter,a.PayDate,a.PayCode /*,a.PayHours */ select a.EmployeeID,a.CostCenter,a.PayDate,a.PayCode,a.PayHoursfrom @e ajoin(select EmployeeID,month(paydate)mo, sum(payhours) hfrom @egroup by EmployeeID,month(paydate)having sum(payhours)>=80) bon a.EmployeeID=b.EmployeeID and month(a.paydate)=b.moP.S Sorry for my bad english |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2005-09-07 : 16:49:59
|
| Vlad,I only need the data where a1.PayDate <= a1.PayPeriodEnd which would be:AA 958 0752/7930 2005-07-16 00:00:00.000 2005-07-29 00:00:00.000 CSW 2005-07-19 00:00:00.000 7AA 958 0752/7930 2005-07-16 00:00:00.000 2005-07-29 00:00:00.000 REG 2005-07-19 00:00:00.000 8and doesn't seem to be working.If the query can ignore:AA 958 0752/7930 2005-07-02 00:00:00.000 2005-07-15 00:00:00.000 CSW 2005-07-19 00:00:00.000 7AA 958 0752/7930 2005-07-02 00:00:00.000 2005-07-15 00:00:00.000 REG 2005-07-19 00:00:00.000 8because the Paydate is greater than the PayPeriodEnd date the query is perfect.I have done a test on the main table and everything is working with the exception of the duplicates. The having sum(payhours)>=80 is working perfectly as well!Can't thank you enough for helping me with this.GaryP.s. Your english is great!! |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-09-07 : 17:42:56
|
Gary,add restriction to first query insert @eselect a.EmployeeID,a.CostCenter,a.PayDate,a.PayCode,a.PayHours from AutoTACurrentPayDaily ajoin(select EmployeeID,PayDatefrom AutoTACurrentPayDaily a1WHERE a1.CompanyCode = 'AA'AND a1.PayCode IN ('REG', 'CSW')AND a1.PayHours >'0'AND a1.PayDate <= a1.PayPeriodEndAND DATEPART (MM,a1.PayDate)= '07'AND DATEPART (YY,a1.PayDate)= '2005'AND SUBSTRING (a1.CostCenter, 1, 4) IN ('0600', '0691', '0702', '0752', '0801', '0841')AND SUBSTRING (a1.CostCenter, 6, 4) IN ('7930', '7935')group by EmployeeID,PayDate having count(distinct paycode)=2) bon a.EmployeeID=b.EmployeeID and a.PayDate=b.paydate AND a.PayCode IN ('REG', 'CSW') AND a.PayHours >'0' AND a.CompanyCode = 'AA'AND a.PayDate <= a.PayPeriodEndAND SUBSTRING (a.CostCenter, 1, 4) IN ('0600', '0691', '0702', '0752', '0801', '0841')AND SUBSTRING (a.CostCenter, 6, 4) IN ('7930', '7935') or distinct to second query |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2005-09-08 : 11:33:23
|
| Vlad,I tried adding distinct to the EmployeeID like you suggested earlier in this posting and the results did not change, still get the double up on the paydates. I even tried adding distinct into both queries. The query you gave me earlier in the posting with distinct added in gave me what I needed and I can manually delete what I don't need. Unless you have a quick solution, I really don't want you to waste anymore time on this as I'm sure you are a very busy person. Thank you again for all your help with this, it is greatly appreciated.Best wishes to you and your family.Gary |
 |
|
|
Next Page
|
|
|