Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

t-sql query sql server 2008

Posted on 2014-03-06
1
Medium Priority
?
307 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
[X]
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
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

636 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