[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
Medium Priority
169 Views
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
Question by:Murray Brown

LVL 12

Accepted Solution

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_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([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([Teamwork]) As Sum_Teamwork
From Diary2 S1
Where CompanyID = 'ABC'
And Survey = 2
Group By UserID, Name) S2
Where S1.UserId = S2.UserId
0

Author Closing Comment

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

Featured Post

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
Course of the Month19 days, 5 hours left to enroll