473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

two queries summing into a single value

347 100+
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
Feb 18 '11 #1
21 1777
Rabbit
12,516 Expert Mod 8TB
You need to group by your code.
Feb 18 '11 #2
dougancil
347 100+
Rabbit,

Do you mean group by in the first query?
Feb 18 '11 #3
Rabbit
12,516 Expert Mod 8TB
No, you need to group in your aggregate query.
Feb 18 '11 #4
dougancil
347 100+
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
Feb 18 '11 #5
Rabbit
12,516 Expert Mod 8TB
Yeah, you need to group that by code if you want to return a row for each code.
Feb 18 '11 #6
dougancil
347 100+
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
Feb 18 '11 #7
Rabbit
12,516 Expert Mod 8TB
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.
Feb 18 '11 #8
dougancil
347 100+
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)

Expand|Select|Wrap|Line Numbers
  1. Agent Number Regular Time  Total O/T ETO Sick Time Vacation Misc
  2.     8245       18.01          0       0      0        0      30
  3.  
Feb 18 '11 #9
Rabbit
12,516 Expert Mod 8TB
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.
Feb 18 '11 #10
dougancil
347 100+
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.
Feb 18 '11 #11
Rabbit
12,516 Expert Mod 8TB
To get them across the top like that you'll need to pivot the data.
Feb 18 '11 #12
dougancil
347 100+
I'm not aware of how to pivot the data. Can you offer some code as an example?

Thank you

Doug
Feb 21 '11 #13
Rabbit
12,516 Expert Mod 8TB
From Microsoft: http://msdn.microsoft.com/en-us/library/ms177410.aspx
Feb 21 '11 #14
dougancil
347 100+
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.
Feb 21 '11 #15
Rabbit
12,516 Expert Mod 8TB
You can use a CASE statement to group your data.
Feb 21 '11 #16
dougancil
347 100+
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?
Feb 22 '11 #17
Rabbit
12,516 Expert Mod 8TB
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.
Feb 22 '11 #18
dougancil
347 100+
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:

Expand|Select|Wrap|Line Numbers
  1. Agent Number Regular Time  Total O/T 
  2.     8245       18.01          0      
  3.  
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.
Feb 22 '11 #19
Rabbit
12,516 Expert Mod 8TB
How are you going to group by code when you don't have code in your data source?
Feb 22 '11 #20
dougancil
347 100+
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
Feb 22 '11 #21
Rabbit
12,516 Expert Mod 8TB
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.
Feb 22 '11 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
1
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?
8
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")...
3
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...
4
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 =...
7
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 =...
2
by: aishah | last post by:
hi can anyody help me in giving and example of multiple queries and single query.
1
jenkinsloveschicken
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...
3
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...
1
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...
1
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...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.