Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

t-sql query sql server 2008

Posted on 2014-03-06
1
299 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

860 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