How to combine several rows into one with a total figure - sum

Hope someone can help with this. as you can see from the table below I have a staff member with 5 "Contract" what I would like to do is add together all the "Hours" relating to "Contract" 1, and the same for contract 2, 3, 4 & 5.
for example I just want one line for contract 1 with a total of 8.5.
If you are able to help with this it would be much appreciated, Thank you.

this should be a simple group by select.
Something like:
select CarerCode, Contract, ContractHours, SUM(Hours) TotalHours
from YOUR_TABLE
group by CarerCode, Contract, ContractHours

Related

Adding Sum into Details view for Crystal Report

I am trying to update a Crystal Report with a new column. This report ("Employee Roster") looks at one table (PREH) and puts out some details, the important ones being Employee, Company, and Craft. The user specifies parameters (Company, Craft, specific Employee [optional]) and the report spits out all employees for that craft/date range and a few other details for them.
I want to add another column for their "HoursWorked" in a given craft, the details of which are found in a different table, PRTH. In PRTH there is one line for each "hours added" entry, and there may be hundreds or thousands of these for a given employee. The SQL for this would be something like:
SELECT SUM(Hours)
FROM dbo.PRTH
WHERE PRTH.Employee=%Employee
AND PRTH.Craft=%Craft
AND PRTH.Company=%Company
AND PRTH.EarnCode NOT IN ('5','6','52','60','100','103')
The main problem I'm finding is that I can't just do a simple join, as that causes a lot of row bloat. Right now the report puts out one line for each employee (grouping by craft) - if I join the table I need, then it makes a LOT of lines for each employee. I want to add just the summary of Hours, based off the current employee that's being looked at in the Details section. I can do what I want in SQL but am not sure how to pass that on in Crystal Reports, ultimately trying to use results of the main report as parameters in this second search.
Any help is greatly appreciated, thank you.
SAMPLE OUTPUT FOR CURRENT REPORT (Parameters=Company 1, Craft=46T)
EE# SortName FullName Co Craft
1553 BOBJONES Jones, Bob 1 46T
1672 RACHELJONES Jones, Rachel 1 46T
2007 TANYAADAMS Adams, Tanya 1 46T
In the above output, I'd be trying to add a new Column "TotalHours". For the first line, I'd expect it to run the SQL statement using Bob Jones' EE for "%Employee", his Craft for "%Craft", and his Company for "%Company".
It sounds like what you're trying to achieve is just this:
SELECT [EE#], SortName, FullName, Co, Craft,
(SELECT SUM(Hours)
FROM dbo.PRTH
WHERE PRTH.Employee=e.Employee
AND PRTH.Craft=e.Craft
AND PRTH.Company=e.Company
AND PRTH.EarnCode NOT IN ('5','6','52','60','100','103')) AS TotalHours
FROM TableWithEmployees AS e
WHERE PRTH.Employee=%Employee
AND PRTH.Craft=%Craft
AND PRTH.Company=%Company;
Is that what you mean?

SQL COUNT() Function

I am trying to run a query on a table, for the sake of this question let's call it ABC. I am looking to count how many bank accounts correspond to a particular Company and VendorName. However this returns answers where the BankAcctNumber is the same, ie I have just a duplicate entry. I am looking to only return these entries where the BankAcctNumber for the two or more Company and VendorName entries are different. This code I have entered below runs and returns all Companys and VendorNames with a count on the BankAcctNumber > 1, but it hasn't identified only the ones where BankAcctNumber are different.
SELECT Company, VendorName, COUNT(BankAcctNumber)
FROM ABC
GROUP BY Company, VendorName
HAVING (COUNT(BankAcctNumber) > 1 )
Sorry if what I have said isn't clear, I am more than willing to clear up any fuzziness and lack of detail in my explanation.
Kind Regards
I think you just want count(distinct):
SELECT Company, VendorName, COUNT(DISTINCT BankAcctNumber)
FROM ABC
GROUP BY Company, VendorName;

creating table with correct data

Im having problems finding the correct data. I have a Table which contains customers(customerID). Each customer is connected to a certain phonenumber(PhoneNr). Every number starts with 2-9.
Every customer have a callcenter(CallCenterID) they can call iff needed.
I want to know how many customers call each callcenter, divided from 2-9(PhoneNumber).
So I want to know how many calls a callcenter gets from every customer with 5, as there starting number in phonenumber.
So far so good. My Code in sql:
Select CallCenter, Count(Customers) AS Number
from ******
Where PhoneNumber Like '45%' --Just need the numbers from Danish customers.
Group By Callcenter;
Im new to much of this, but i've tried the whole day to come up with the right result.
Right now Im getting every callcenter, and the number for every call to them.
Can anyone help me?
:)
If I'm understanding correctly, you want the counts for all CallCenter's broken down by the first digit in the PhoneNumber:
SELECT CallCenter, SUBSTR(PhoneNumber, 1, 1) as startsWith, COUNT(*) as number
FROM myTable
GROUP BY CallCenter, SUBSTR(PhoneNumber, 1, 1)
ORDER BY 2, 3
If that's not what you wanted, please explain your question a bit better.

Summarizing records by date in Rails 3 ActiveRecord

I have a large table with many records that share a timestamp. I want to get a result set that has a column summed by timestamp. I see how you can simply use the 'sum' method to get a columns total. I need to, however, group by a date column. This is far less obvious. I know I can use 'find_by_sql' but it will be hideous to code as I have to do this for over 20 columns. I assume AR must have some magic to do this which escapes me?
Date set example:
table/model: games/Game
player_name, points_scored, game_date
john, 20, 08-20-2012
sue, 30, 08-20-2012
john, 12, 08-21-2012
sue, 10, 08-21-2012
What i want to see in my results is:
game_date, total_points
08-20-2012, 50
08-21-2012, 22
Here is a crude example of what the SQL query would look like:
SELECT game_date, SUM(points_scored)
FROM games
GROUP BY game_date
Mind you, I actually have 20 'score' columns to SUM by timestamp.
How can I simply use AR to do this? Thanks in advance.
Ok. It took some digging and playing around but I figured it out. I was hoping to find something better than 'find_by_sql' and I did, but it isn't a whole lot better. Again, knowing that I need to SUM 20+ columns by timestamp, here is the solution in the context of the example above.
results = Game.select( 'game_date, SUM(points_scored) as "points_scored"').group( 'game_date' )
Now, that doesn't look so bad, but I have to type in the 20+ SUM() within that 'select' method. Doesn't save a whole lot of work from 'find_by_sql' but it works.

Sum datediff and group by

I have this simple query, but its not producing the results I want... hopefully you can help:
The result is:
Construction 2
Construction 3
Emergency Funds 4
Housing 5
Seniors Services 9
Seniors Services 185
What I want is:
Construction 5
Emergency Funds 4
Housing 5
Seniors Services 194
SELECT T.NAME, SUM(DATEDIFF (HH,T.DATE_DUE,T.DATE_START))as Donation_Hours FROM TASKS T
GROUP BY t.name, T.DATE_DUE,T.DATE_START
order by name
Try this:
SELECT T.NAME, SUM(DATEDIFF(HH,T.DATE_DUE,T.DATE_START))as Donation_Hours
FROM TASKS T
GROUP BY t.name
ORDER BY name
Stan, some additional detail will go a long way in helping you with this problem. The specific database platform, some sample data, and what you mean when you compare what your result is and what you want it to be, should be included at a minimum.
That being said I think you're alluding to the fact that you have multiple instances of the name rather than a single result per name with a grand total of the hours difference. If this is your problem, you can fix it by removing everything in your group by after the t.name. You're not required to put the constituent elements of an aggregate in the group by clause in the same manner that you would be if you had listed them separately.
I'm assuming that you're using MSSQL through SSMS. This answer was checked against SQL2008 R2.