Solved

t-sql query sql server 2008

Posted on 2014-03-06
1
294 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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now