[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

t-sql query sql server 2008

Posted on 2014-03-06
1
Medium Priority
?
310 Views
Last Modified: 2014-03-06
i'm using sql server 2008.

My table is called TestTable2.

The script to create table is here:
CREATE TABLE [dbo].[TestTable2](
	[EmployeeID] [smallint] NULL,
	[Region] [smallint] NULL,
	[WorkYear] [smallint] NULL,
	[ProjectedYear] [smallint] NULL,
	[Salary] [money] NULL,
	[Benefits] [money] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (1, 1, 2014, 0, 41000.2000, 456.2500)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (1, 1, 2014, 1, 43266.4500, 512.2300)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (1, 1, 2014, 2, 45879.5600, 465.2800)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (1, 1, 2014, 3, 46789.2500, 467.2800)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (1, 1, 2014, 4, 47865.3400, 489.2300)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (1, 1, 2014, 5, 49654.2800, 512.2300)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (2, 1, 2014, 0, 56345.2500, 456.2500)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (2, 1, 2014, 1, 58465.3600, 468.2500)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (2, 1, 2014, 2, 59654.4500, 472.2300)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (2, 1, 2014, 3, 60156.6500, 489.2500)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (2, 1, 2014, 4, 62456.7500, 491.3700)
INSERT [dbo].[TestTable2] ([EmployeeID], [Region], [WorkYear], [ProjectedYear], [Salary], [Benefits]) VALUES (2, 1, 2014, 5, 63456.8500, 512.5900)

Open in new window


My table looks like this:
my table
If you look at my table the it shows 5 records for EmployeeID 1 and 5 records for EmployeeID 2.

The table shows projections for 5 years for an employee.
Basically I want to sum up the salary and benefits for the employees by Projected year.

This pic is a more graphical look at my table.

color drawing of my table
So if you look at EmployeeID 1 and ProjectedYear 0, this record has salary 41000.20 and benefits of 456.25.
So I want to add that to EmployeeID 2 and ProjectedYear 0, Which for that employee is salary 56345.25 and benefits 456.25

So the total for Salary for ProjectedYear 0 is 97345.45 and the Total Benefits is 912.50

So my result set should only be these 3 columns ProjectedYear, Salary, Benefits.
This is a drawing I created of my desired query result.

my desired query result
Anyone know the query to achieve this result?
0
Comment
Question by:maqskywalker
1 Comment
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 39911337
This produces the correct results:
SELECT  ProjectedYear,
        SUM(Salary) Salary,
        SUM(Benefits) Benefits
FROM    [TestTable2]
GROUP BY ProjectedYear

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question