I have two queries:
SELECT s1.Employeenumber, s1.[Name], s2.Exceptiondate, code, s2.totalminutes into scratchpad4
FROM
(select distinct Employeenumber,[Name] from Scratchpad1) AS s1
inner JOIN
(select employeenumber, exceptiondate, code, sum(duration) as totalminutes
from scratchpad3
where exceptiondate between @payrollstartdate And @payrollenddate
group by employeenumber, exceptiondate, code) as s2
ON s1.Employeenumber = s2.Employeenumber
order by exceptiondate asc
and
SELECT [EmployeeNumber],[Name],SUM([Minutes]) AS summinutes,SUM([Minutes])/60 AS sumhours,
SUM(CASE WHEN Cat=2 THEN [Minutes] ELSE 0 END) AS specminutes
FROM
(
SELECT [EmployeeNumber],[Name],[Dateonly],[Minutes],1 AS Cat
FROM Scratchpad2
where dateonly between @payrollstartdate And @payrollenddate
UNION ALL
SELECT [EmployeeNumber],[Name],[ExceptionDate],[TotalMinutes],2
FROM ScratchPad4
)t
GROUP BY [EmployeeNumber],[Name]
and what these are doing is summing up values into "spec minutes" but what I'm looking to do is to sum for each type of code, either ETO, Vacation, Sick Time or Misc and show a value for each. Right now it's just showing spec minutes. In my first query, there is a code value for each. Can anyone offer a possible way to solve this issue?
Thank you
Doug
21 1777
You need to group by your code.
Rabbit,
Do you mean group by in the first query?
No, you need to group in your aggregate query.
Rabbit,
This is the only really aggregate I have ... this is the final query that I have that sums everything up and presents it to users:
SELECT DISTINCT
[ScratchPad5].EmployeeNumber,
SUM( case when [sumhours]>40
THEN 40
ELSE cast([sumhours] as numeric(12,2))
END ) AS TotalRegHours,
SUM( case when [sumhours]>40
THEN cast([sumhours]-40 as numeric(12,2))
ELSE 0
END ) AS TotalOT
into scratchpad7
FROM
ScratchPad5
GROUP BY
[ScratchPad5].EmployeeNumber,
sumhours
order by employeenumber asc
Yeah, you need to group that by code if you want to return a row for each code.
Rabbit,
At this point though in my calculation, all of the codes are "summed" which is why I thought I should do a group by before this.
here's a sample of my data from Scratchpad5:
8484 Adeline Gilliam 170.79 2.8465 0
8475 Andreika Houff 1533.27 25.5545 0
8467 Brenda Brown 2248.71 37.4785 0
8471 Brentton Thompson 2920.81 48.680166 0
Here's some data from scratchpad4:
8245 Yvonne Becerra 1/2/2011 Coaching Session 30
Even if you are pulling your data from an aggregated source, if you reaggregate like you are doing, the prior groupings are moot, it will regroup by your new group by settings. If you need the new aggregate to group by code then you need to tell it to group by code again. It doesn't matter that your source is grouped by code.
Ok so then I guess my question after that would be, since I'm going to be doing a groupby for the codes, can I then sum them so that it would look something like this:
From Scratchpad3
Group By
[Scratchpad3].Code,
but then I'm also trying to sum them by employeenumber.So my data would look like this (for agent 8245 in this case) -
Agent Number Regular Time Total O/T ETO Sick Time Vacation Misc
-
8245 18.01 0 0 0 0 30
-
Those fields don't exist in the code that you've shown. So I can't say whether or not you can achieve that layout.
You don't have to group by only one field. You can group by multiple fields.
Rabbit,
Those fields are represented in this line of my first query as
"code"
select employeenumber, exceptiondate, code, sum(duration) as totalminutes
A user would only have so many codes ... ETO, Vacation, Sick Time everything else would be summed as Misc.
To get them across the top like that you'll need to pivot the data.
I'm not aware of how to pivot the data. Can you offer some code as an example?
Thank you
Doug
Rabbit,
Ok so I now know that I have to pivot the data, but with the information that you've seen from my aggregate query, can I produce the results that I need?
Basically what I'm not sure of is how to filter all of the data so that if it doesnt match ETO, Vacation or Sick time, how to sum it as Misc.
You can use a CASE statement to group your data.
Rabbit,
With using CASE, wouldn't I have to do that in my first query? Because after that I've summed for "spec minutes" and because in the end, these things are broken out as individual elements rather than just a sum value. Or am I thinking incorrectly?
I don't know which level you would have to group your data at because I have no idea what your queries look like. You supplied two queries in the original question but from the sample results you've shown me, they have no connection to those queries. Plus, you've probably made changes and I have no idea as to what the current queries look like.
Rabbit,
The sample results come from this query:
SELECT DISTINCT
[ScratchPad5].EmployeeNumber,
SUM( case when [sumhours]>40
THEN 40
ELSE cast([sumhours] as numeric(12,2))
END ) AS TotalRegHours,
SUM( case when [sumhours]>40
THEN cast([sumhours]-40 as numeric(12,2))
ELSE 0
END ) AS TotalOT
into scratchpad7
FROM
ScratchPad5
GROUP BY
[ScratchPad5].EmployeeNumber,
sumhours
order by employeenumber asc
which produces this result: -
Agent Number Regular Time Total O/T
-
8245 18.01 0
-
what I need it to do is two things:
1. To show results like this like post #9 which is sorting all data based on "code" in my table and showing them to the user.
2. If the "code" doesnt match ETO, Vacation, Sick Time then it will be a Misc, which is then summed.
How are you going to group by code when you don't have code in your data source?
Rabbit,
In my first query you'll notice that I have this:
SELECT s1.Employeenumber, s1.[Name], s2.Exceptiondate, code, s2.totalminutes into scratchpad4
FROM
(select distinct Employeenumber,[Name] from Scratchpad1) AS s1
inner JOIN
(select employeenumber, exceptiondate, code, sum(duration) as totalminutes
from scratchpad3
where exceptiondate between @payrollstartdate And @payrollenddate
group by employeenumber, exceptiondate, code) as s2
ON s1.Employeenumber = s2.Employeenumber
order by exceptiondate asc
If that query is scratchpad5 (which I assume it is by the context but I don't know because you haven't said so) then you do the group by and you use the case statement on the aggregate level.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: vasanth kumar |
last post by:
Hi,
I have a problem in dealing with the return value of the SQL command. I know how to do, when SQL command returns Recordsets. But in my case it returns a single value. The following code fails...
|
by: Brett |
last post by:
I often have to change a single value in an xml file based off of a given ID.
Is there any easy way to do this through .net.xml?
Do I have to parse the file then write it out again?
|
by: Tom |
last post by:
Here is what I do to get a single value from my database (using Oracle ODP
as example):
Dim ID as Object
Dim cmdTest as New OracleCommand("select ID from MyTable where key = " &
KeySearch")...
|
by: Bill Nguyen |
last post by:
VS.NET 2003
SQLserver 2000
I need to create a function to return the single resulting value from an SQL
statement.
For example, "Select max(amount) from tableA"
I would like to reuse the...
|
by: Jim in Arizona |
last post by:
I'm having trouble pulling a single value from my SQL DB and filling a
string type with it.
Something like:
Dim strConnection As String =...
|
by: Peter |
last post by:
Gday,
I have a dataset with multiple tables, from which I want to access a
single value in one of those tables.
I know I can do something like:
Decimal myVar =...
|
by: aishah |
last post by:
hi
can anyody help me in giving and example of multiple queries and single query.
|
by: jenkinsloveschicken |
last post by:
Is it possible to compare a single value to many in a single statement? Here is what I am attempting to do:
1. Users lands on page and via a cookie check function they are identified.
2. A query...
|
by: bogdan |
last post by:
Hi,
I have a stored procedure that returns a single value. Example:
SELECT @RowCount = COUNT(*) FROM t WHERE
RETURN @RowCount
I created a data set, table adapter, and adapter's method...
|
by: psycho |
last post by:
How do we return a single value from a stored procedure.
Suppose I have a stored procedure like this:
create proc dbo.spInsertGroup
@ID uniqueidentifier
@GroupName varchar(100),
@IsActive...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |