Solved

t-sql query sql server 2008

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

912 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now