Author |
Topic |
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-16 : 12:48:11
|
I have the following query:select name, employeenumber, summinutes, sum(summinutes/60) as hours from (select scratchpad2.name, scratchpad2.employeenumber, SUM(scratchpad2.minutes) + SUM(scratchpad4.totalminutes) as summinutesfrom scratchpad2inner join scratchpad4on scratchpad2.employeenumber = scratchpad4.employeenumbergroup by scratchpad2.name, scratchpad2.employeenumber) tgroup by name, employeenumber, summinutesand looking at both scratchpad2 and scratchpad4, I'm seeing more data than is being returned to me. Sample data from scratchpad2: Eric Edwards 8247 10/1/2010 222.67 Eric Edwards 8247 10/2/2010 428.74 Eric Edwards 8247 10/4/2010 108.41 Eric Edwards 8247 10/5/2010 317.33 Gerald Stephen 8389 10/1/2010 505.92 Gerald Stephen 8389 10/2/2010 458.01 Timothy Bedard 8433 10/1/2010 372.87 Timothy Bedard 8433 10/2/2010 338.46 Timothy Bedard 8433 10/3/2010 139.81 Timothy Bedard 8433 10/4/2010 430.32and when I run the above query, here is the data that's being returned to me:Samantha Balash 8442 1508.93 25.148833Christy Clayton 8455 3045.81 50.763500Akieva Saunders 8466 1751.11 29.185166Brenda Brown 8467 2797.97 46.632833but I'm not sure why this query isn't pulling all of the data. Can anyone tell me why?Thank youDoug |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2010-11-17 : 04:48:46
|
It'd be hard t otell since the names in your sample data don't match uyp with your displayed results. Can you post "data insert" statements per th eadvised style in the FAQ....so that somebody here can build your tables locally with content. then they could see your code in execution. |
|
|
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-17 : 09:28:04
|
Tara,I have this query:SELECT name, employeenumber, summinutes, sum(summinutes/60) as hours, specminutes, sum(specminutes/60) as sphoursFROM(SELECT scratchpad2.name, scratchpad2.employeenumber, SUM(scratchpad2.minutes) + SUM(scratchpad2.totalminutes) as summinutes, SUM(scratchpad2.minutes) + SUM(scratchpad4.totalminutes) as specminutes,FROM scratchpad2INNER JOIN scratchpad4ON scratchpad2.employeenumber = scratchpad4.employeenumberGROUP BY scratchpad2.name, scratchpad2.employeenumber) tGROUP BY name, employeenumber, summinutesbut when I try to run it, I get the following error:Column 't.specminutes' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-17 : 12:04:33
|
Tara,I've looked for what Andrew posted in the FAQ's and can't find that. Can you please post the link.Thank you. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-17 : 14:26:18
|
Tara,I have sample data, is there any way I can e mail that? I have quite a bit of it and I don't want to post it here.Thank youDoug |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-17 : 15:19:14
|
Here is some sample data then from scratchpad2: Name Employeenumber Dateonly minutes Eric Edwards 8247 10/1/2010 222.67 Eric Edwards 8247 10/2/2010 428.74 Eric Edwards 8247 10/4/2010 108.41 Eric Edwards 8247 10/5/2010 317.33 Eric Edwards 8247 10/6/2010 332.7 Eric Edwards 8247 10/7/2010 59.82 Xavier Oaks 8378 10/1/2010 223.46 Xavier Oaks 8378 10/2/2010 145.2 Xavier Oaks 8378 10/3/2010 380.39 Xavier Oaks 8378 10/4/2010 337.92 Xavier Oaks 8378 10/5/2010 227.53 Gerald Stephen 8389 10/1/2010 505.92 Gerald Stephen 8389 10/2/2010 458.01 Gerald Stephen 8389 10/3/2010 70.32 Gerald Stephen 8389 10/4/2010 124.91 Gerald Stephen 8389 10/7/2010 294.96 Michelle Mayes 8428 10/2/2010 203.35 Michelle Mayes 8428 10/3/2010 469.9 Michelle Mayes 8428 10/5/2010 231.41 Michelle Mayes 8428 10/6/2010 231.72 Michelle Mayes 8428 10/7/2010 322.32 Timothy Bedard 8433 10/1/2010 372.87 Timothy Bedard 8433 10/2/2010 338.46 Timothy Bedard 8433 10/3/2010 139.81 Timothy Bedard 8433 10/4/2010 430.32 Timothy Bedard 8433 10/5/2010 229.47 Timothy Bedard 8433 10/6/2010 386.95from scratchpad4:Employeenumber Name Exceptiondate Code Totalminutes 8455 Christy Clayton 10/1/2010 Special Project 60 8455 Christy Clayton 10/2/2010 Approved Technical Reason 60 8466 Akieva Saunders 10/2/2010 Supervisor Meeting 60 8467 Brenda Brown 10/3/2010 Coaching Session 7 8442 Samantha Balash 10/4/2010 Approved Technical Reason 20 8455 Christy Clayton 10/5/2010 Special Project 15I hope that this helps.Thanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-17 : 16:55:50
|
Here's the DDL for scratchpad2:CREATE TABLE [dbo].[scratchpad2] ( [name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [employeenumber] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [dateonly] [datetime] NULL , [minutes] [decimal](10, 2) NULL and scratchpad4:CREATE TABLE [dbo].[scratchpad4] ( [Employeenumber] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Exceptiondate] [datetime] NULL , [code] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [totalminutes] [int] NULL What I'm looking to have happen is that upon execution of the query that it will sum for all in scratchpad2, and also if a name is in scratchpad4, it adds those as well. So in the case of Christy Clayton her total would be 970.27 (if you add all from both scratchpad2 and 4) and give you a total of both, but in the case of someone like Eric, since he doesnt have any values in scratchpad4, his sum would only be for scratchpad2 which would be 1469.67. Does this make sense? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-18 : 11:13:18
|
Tara,I hope that this is more what you were asking forSELECTname,employeenumber,summinutes,sum(summinutes/60) as hours,specminutes,FROM(SELECTscratchpad2.name,scratchpad2.employeenumber,SUM(scratchpad2.minutes) + SUM(scratchpad2.totalminutes) as summinutes,SUM(scratchpad2.minutes) + SUM(scratchpad4.totalminutes) as specminutes,FROM scratchpad2WHERE DATE between '10/1/2010' and '10/8/2010'INNER JOIN scratchpad4ON scratchpad2.employeenumber = scratchpad4.employeenumberGROUP BYscratchpad2.name, scratchpad2.employeenumber) tGROUP BYname,employeenumber,summinutesCREATE TABLE [dbo].[scratchpad2] ([name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[employeenumber] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[dateonly] [datetime] NULL ,[minutes] [decimal](10, 2) NULL and scratchpad4:CREATE TABLE [dbo].[scratchpad4] ([Employeenumber] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[Exceptiondate] [datetime] NULL ,[code] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[totalminutes] [int] NULLfrom scratchpad2: Name Employee Number Date MinutesSELECT Eric Edwards 8247 10/1/2010 222.67SELECT Eric Edwards 8247 10/2/2010 428.74SELECT Eric Edwards 8247 10/4/2010 108.41SELECT Eric Edwards 8247 10/5/2010 317.33SELECT Eric Edwards 8247 10/6/2010 332.7SELECT Eric Edwards 8247 10/7/2010 59.82SELECT Xavier Oaks 8378 10/1/2010 223.46SELECT Xavier Oaks 8378 10/2/2010 145.2SELECT Xavier Oaks 8378 10/3/2010 380.39SELECT Xavier Oaks 8378 10/4/2010 337.92SELECT Xavier Oaks 8378 10/5/2010 227.53SELECT Gerald Stephen 8389 10/1/2010 505.92SELECT Gerald Stephen 8389 10/2/2010 458.01SELECT Gerald Stephen 8389 10/3/2010 70.32SELECT Gerald Stephen 8389 10/4/2010 124.91SELECT Gerald Stephen 8389 10/7/2010 294.96SELECT Michelle Mayes 8428 10/2/2010 203.35SELECT Michelle Mayes 8428 10/3/2010 469.9SELECT Michelle Mayes 8428 10/5/2010 231.41SELECT Michelle Mayes 8428 10/6/2010 231.72SELECT Michelle Mayes 8428 10/7/2010 322.32SELECT Timothy Bedard 8433 10/1/2010 372.87SELECT Timothy Bedard 8433 10/2/2010 338.46SELECT Timothy Bedard 8433 10/3/2010 139.81SELECT Timothy Bedard 8433 10/4/2010 430.32SELECT Timothy Bedard 8433 10/5/2010 229.47SELECT Timothy Bedard 8433 10/6/2010 386.95SELECT Wendy Castellanos 8454 10/1/2010 198.1SELECT Wendy Castellanos 8454 10/2/2010 176.98SELECT Wendy Castellanos 8454 10/4/2010 342.52SELECT Wendy Castellanos 8454 10/5/2010 34.92SELECT Wendy Castellanos 8454 10/6/2010 280.39SELECT Christy Clayton 8455 10/1/2010 118.36SELECT Christy Clayton 8455 10/2/2010 192.46SELECT Christy Clayton 8455 10/4/2010 102.04SELECT Christy Clayton 8455 10/7/2010 422.41SELECT Jordan Campusano 8462 10/1/2010 175.95SELECT Jordan Campusano 8462 10/2/2010 92.21SELECT Jordan Campusano 8462 10/3/2010 117.28SELECT Jordan Campusano 8462 10/5/2010 410.94SELECT Jordan Campusano 8462 10/6/2010 168.13SELECT Jordan Campusano 8462 10/7/2010 205.82SELECT Chanel Jones 8464 10/1/2010 178.97SELECT Chanel Jones 8464 10/2/2010 179.94SELECT Chanel Jones 8464 10/3/2010 80.04SELECT Chanel Jones 8464 10/4/2010 357.81SELECT Chanel Jones 8464 10/6/2010 179SELECT Chanel Jones 8464 10/7/2010 179.23SELECT Leticia Guerrero 8464 10/1/2010 178.97SELECT Leticia Guerrero 8464 10/2/2010 179.94SELECT Leticia Guerrero 8464 10/3/2010 80.04SELECT Leticia Guerrero 8464 10/4/2010 357.81SELECT Leticia Guerrero 8464 10/6/2010 179SELECT Leticia Guerrero 8464 10/7/2010 179.23SELECT Alisha Byrd 8465 10/1/2010 122.38SELECT Alisha Byrd 8465 10/2/2010 211.38SELECT Alisha Byrd 8465 10/3/2010 580.95SELECT Alisha Byrd 8465 10/4/2010 160.36SELECT Alisha Byrd 8465 10/5/2010 72.79SELECT Alisha Byrd 8465 10/7/2010 236.18SELECT Akieva Saunders 8466 10/1/2010 251.88SELECT Akieva Saunders 8466 10/2/2010 414.79SELECT Akieva Saunders 8466 10/3/2010 93.63SELECT Akieva Saunders 8466 10/4/2010 251.64SELECT Akieva Saunders 8466 10/7/2010 439.17SELECT Brenda Brown 8467 10/1/2010 355.32SELECT Brenda Brown 8467 10/2/2010 139.49SELECT Brenda Brown 8467 10/3/2010 376.58SELECT Brenda Brown 8467 10/4/2010 598.56SELECT Brenda Brown 8467 10/5/2010 394.66SELECT Brenda Brown 8467 10/6/2010 432.6SELECT Brenda Brown 8467 10/7/2010 451.76 from scratchpad4:Employeenumber Name ExceptionDate Code TotalMinutes SELECT 8455 Christy Clayton 10/1/2010 Special Project 60SELECT 8455 Christy Clayton 10/2/2010 Approved Technical Reason 60SELECT 8466 Akieva Saunders 10/2/2010 Supervisor Meeting 60SELECT 8467 Brenda Brown 10/3/2010 Coaching Session 7SELECT 8442 Samantha Balash 10/4/2010 Approved Technical Reason 20SELECT 8455 Christy Clayton 10/5/2010 Special Project 15SUM Scratchpad2.minutes as summinutes, sum Scratchpad2.Minutes + scratchpad4.Totalminutes as Specminutes /60 as sumhourswhere date is between 10/1/2010 and 10/8/2010, JOIN INNER JOIN scratchpad4ON scratchpad2.employeenumber = scratchpad4.employeenumberGROUP BYscratchpad2.name, scratchpad2.employeenumberResult set:Name EmployeeNumber summinutes sumhours specminutesEric Edwards 8247 1469.67 24.4945 NULLXavier Oaks 8378 1314.5 21.9083 NULLGerald Stephen 8389 1454.12 24.2353 NULLMichelle Mayes 8428 1458.7 24.3116 NULLTimothy Bedard 8433 1897.88 31.6313 NULL Samantha Balash 8442 2817.86 46.964333 20Wendy Castellanos8454 1032.91 17.2151 NULLChristy Clayton 8455 5011.62 83.52700 75 Jordan Campusano8462 1170.33 19.5055 NULL Chanel Jones 8464 1154.99 19.2498 NULL Alisha Byrd 8465 1384.04 23.0673 NULL Akieva Saunders 8466 2902.22 48.370333 60 Brenda Brown 8467 5497.94 91.632333 7What I'm looking to have happen is that upon execution of the query that it will sum for all in scratchpad2, and also if a name is in scratchpad4, it adds those as well. So in the case of Christy Clayton her total would be 970.27 (if you add all from both scratchpad2 and 4) and give you a total of both, but in the case of someone like Eric, since he doesnt have any values in scratchpad4, his sum would only be for scratchpad2 which would be 1469.67. Does this make sense?Thank you,Doug |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-11-18 : 11:23:24
|
the best way to do this is likeSELECT [Employee Number],[Name],[Date],SUM([Minutes]) AS summinutes,SUM([Minutes])/60 AS sumhours,SUM(CASE WHEN Cat=2 THEN [Minutes] ELSE 0 END) AS specminutesFROM(SELECT [Employee Number],[Name],[Date],[Minutes],1 AS CatFROM Scratchpad2UNION ALLSELECT [EmployeeNumber],[Name],[ExceptionDate],[TotalMinutes],2FROM ScratchPad4)tGROUP BY [Employee Number],[Name],[Date] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-18 : 12:09:48
|
Visakh,That sums them for each day individually but I need a sumtotal of all times for each for the week. What I get when I use your query is as follows:Result Set:8247 Eric Edwards 2010-10-01 00:00:00.000 222.67 3.711166 .008247 Eric Edwards 2010-10-02 00:00:00.000 428.74 7.145666 .008247 Eric Edwards 2010-10-04 00:00:00.000 108.41 1.806833 .008247 Eric Edwards 2010-10-05 00:00:00.000 317.33 5.288833 .008247 Eric Edwards 2010-10-06 00:00:00.000 332.70 5.545000 .008247 Eric Edwards 2010-10-07 00:00:00.000 59.82 .997000 .00What I'd like is Result Set:Name EmployeeNumber summinutes sumhours specminutes8247 Eric Edwards 1469.67 24.4945 NULL |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-11-18 : 12:12:09
|
ok then a small tweakSELECT [Employee Number],[Name],SUM([Minutes]) AS summinutes,SUM([Minutes])/60 AS sumhours,SUM(CASE WHEN Cat=2 THEN [Minutes] ELSE 0 END) AS specminutesFROM(SELECT [Employee Number],[Name],[Date],[Minutes],1 AS CatFROM Scratchpad2UNION ALLSELECT [EmployeeNumber],[Name],[ExceptionDate],[TotalMinutes],2FROM ScratchPad4)tGROUP BY [Employee Number],[Name] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-18 : 14:15:13
|
visakh,That does work but one other thing, I need it to match a where clausewhere date between '10/1/2010' and '10/8/2010' |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-11-19 : 11:00:40
|
[code]SELECT [Employee Number],[Name],SUM([Minutes]) AS summinutes,SUM([Minutes])/60 AS sumhours,SUM(CASE WHEN Cat=2 THEN [Minutes] ELSE 0 END) AS specminutesFROM(SELECT [Employee Number],[Name],[Date],[Minutes],1 AS CatFROM Scratchpad2where date between '10/1/2010' and '10/8/2010'UNION ALLSELECT [EmployeeNumber],[Name],[ExceptionDate],[TotalMinutes],2FROM ScratchPad4where ExceptionDate between '10/1/2010' and '10/8/2010')tGROUP BY [Employee Number],[Name][/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
dougancil
Posting Yak Master
217 Posts |
Posted - 2010-11-23 : 15:21:00
|
Visakh,Thank you. That's what I was looking for.Doug |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-11-24 : 10:33:58
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Next Page
|