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

x
?
Solved

VB.net SQL Find the difference in two totals

Posted on 2014-08-02
2
Medium Priority
?
169 Views
Last Modified: 2014-08-02
Hi
I am using the following SQL query to calculate total scores in a SQL table for  Survey 1
Once the second survey is complete I have to calculate the difference between the scores.
I could calculate total survey 2 scores by changing the following to have Survey = 2
How do I calculate the difference, Do I use data tables?

            "Select [UserID],[Name],Sum([Gratitude]) as Sum_Gratitude, Sum([Reframing]) as Sum_Reframing, Sum([Empathy]) as Sum_Empathy, Sum([Adaptable]) as Sum_Adaptable, Sum([Teamwork]) As Sum_Teamwork From Diary2 Where CompanyID = 'ABC' And Survey = 1 Group By UserID, Name"
0
Comment
Question by:Murray Brown
2 Comments
 
LVL 12

Accepted Solution

by:
Harish Varghese earned 2000 total points
ID: 40236015
SELECT S1.UserId, S1.Name, 
	S1.Sum_Gratitude - S2.Sum_Gratitude as Sum_Gratitude_Diff,
	S1.Sum_Reframing - S2.Sum_Reframing as Sum_Reframing_Diff,
	S1.Sum_Empathy - S2.Sum_Empathy as Sum_Empathy_Diff,
	S1.Sum_Adaptable - S2.Sum_Adaptable as Sum_Adaptable_Diff,
	S1.Sum_Teamwork - S2.Sum_Teamwork as Sum_Teamwork_Diff
From 
	(Select [UserID], 
		[Name],
		Sum([Gratitude]) as Sum_Gratitude, 
		Sum([Reframing]) as Sum_Reframing, 
		Sum([Empathy]) as Sum_Empathy, 
		Sum([Adaptable]) as Sum_Adaptable, 
		Sum([Teamwork]) As Sum_Teamwork
	From Diary2 S1
	Where CompanyID = 'ABC' 
	And Survey = 1 
	Group By UserID, Name) S1,

	(Select [UserID], 
		[Name],
		Sum([Gratitude]) as Sum_Gratitude, 
		Sum([Reframing]) as Sum_Reframing, 
		Sum([Empathy]) as Sum_Empathy, 
		Sum([Adaptable]) as Sum_Adaptable, 
		Sum([Teamwork]) As Sum_Teamwork
	From Diary2 S1
	Where CompanyID = 'ABC' 
	And Survey = 2 
	Group By UserID, Name) S2
Where S1.UserId = S2.UserId

Open in new window

0
 

Author Closing Comment

by:Murray Brown
ID: 40236034
Thanks very much for taking the time out to answer this. Much appreciated
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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 month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

834 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