SQL Server: any work around to get NET TOTAL Sales - Refund please












1















If you just create "temp" database the following code can create and add sample data into the tables.



Query is to subtract TOTAL REFUNDS FROM TOTAL SALES which results in a new column named NET TOTAL.



Show: UserID, SiteID, Name, TOTAL Sales, Total Refunds, Net Total



image linked for sample data



Thanks



enter image description here



CREATE TABLE temp.dbo.Users 
(
UserID INT,
SiteID INT,
Name VARCHAR(20)
)

CREATE TABLE temp.dbo.TRANSACTIONS
(
TransactionID int,
UserID int,
SiteID int,
TransactionType varchar(10),
TransactionDate Date,
Amount money
)

INSERT INTO temp.dbo.Users
VALUES (1, 1, 'ARTHUR'), (2, 1, 'AARON'), (3, 2, 'BRETT')

INSERT INTO temp.dbo.TRANSACTIONS
VALUES (4, 1, 1, 'SALE', GETDATE(), 120),
(6, 1, 1, 'REFUND', GETDATE(), 120),
(7, 2, 2, 'SALE', GETDATE(), 90),
(9, 2, 1, 'SALE', GETDATE(), 30),
(10, 2, 1, 'SALE', GETDATE(), 30),
(11, 2, 1, 'SALE', GETDATE(), 30),
(12, 2, 1, 'REFUND', GETDATE(), 30)


Query: This is my current query where I have tried to name it using WITH clause but no luck



SELECT 
U.UserID, U.SiteID, U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name









share|improve this question

























  • Could you clarify your question? I'm not sure what you are looking to get help with. As in, what is your expected output?

    – sniperd
    Jan 3 at 17:01








  • 1





    Looks like he is looking for someone to do his homework for him.No effort, a task as easy as what you would ask in a lesson about sql...

    – TomTom
    Jan 3 at 17:04











  • I had a subtract in the refunds by default however that was an easy job. I was curious to resolve it if that "-" subtract was not there. indeed i got the answer that way but I dont think that was ideal in work environment. needed how to do a postive number from a positive number where the columns are only named in the query. anyways i got the answer, and didnt know this was a way to do it, [NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]

    – Zabi Sidiqkhil
    Jan 3 at 17:22
















1















If you just create "temp" database the following code can create and add sample data into the tables.



Query is to subtract TOTAL REFUNDS FROM TOTAL SALES which results in a new column named NET TOTAL.



Show: UserID, SiteID, Name, TOTAL Sales, Total Refunds, Net Total



image linked for sample data



Thanks



enter image description here



CREATE TABLE temp.dbo.Users 
(
UserID INT,
SiteID INT,
Name VARCHAR(20)
)

CREATE TABLE temp.dbo.TRANSACTIONS
(
TransactionID int,
UserID int,
SiteID int,
TransactionType varchar(10),
TransactionDate Date,
Amount money
)

INSERT INTO temp.dbo.Users
VALUES (1, 1, 'ARTHUR'), (2, 1, 'AARON'), (3, 2, 'BRETT')

INSERT INTO temp.dbo.TRANSACTIONS
VALUES (4, 1, 1, 'SALE', GETDATE(), 120),
(6, 1, 1, 'REFUND', GETDATE(), 120),
(7, 2, 2, 'SALE', GETDATE(), 90),
(9, 2, 1, 'SALE', GETDATE(), 30),
(10, 2, 1, 'SALE', GETDATE(), 30),
(11, 2, 1, 'SALE', GETDATE(), 30),
(12, 2, 1, 'REFUND', GETDATE(), 30)


Query: This is my current query where I have tried to name it using WITH clause but no luck



SELECT 
U.UserID, U.SiteID, U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name









share|improve this question

























  • Could you clarify your question? I'm not sure what you are looking to get help with. As in, what is your expected output?

    – sniperd
    Jan 3 at 17:01








  • 1





    Looks like he is looking for someone to do his homework for him.No effort, a task as easy as what you would ask in a lesson about sql...

    – TomTom
    Jan 3 at 17:04











  • I had a subtract in the refunds by default however that was an easy job. I was curious to resolve it if that "-" subtract was not there. indeed i got the answer that way but I dont think that was ideal in work environment. needed how to do a postive number from a positive number where the columns are only named in the query. anyways i got the answer, and didnt know this was a way to do it, [NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]

    – Zabi Sidiqkhil
    Jan 3 at 17:22














1












1








1








If you just create "temp" database the following code can create and add sample data into the tables.



Query is to subtract TOTAL REFUNDS FROM TOTAL SALES which results in a new column named NET TOTAL.



Show: UserID, SiteID, Name, TOTAL Sales, Total Refunds, Net Total



image linked for sample data



Thanks



enter image description here



CREATE TABLE temp.dbo.Users 
(
UserID INT,
SiteID INT,
Name VARCHAR(20)
)

CREATE TABLE temp.dbo.TRANSACTIONS
(
TransactionID int,
UserID int,
SiteID int,
TransactionType varchar(10),
TransactionDate Date,
Amount money
)

INSERT INTO temp.dbo.Users
VALUES (1, 1, 'ARTHUR'), (2, 1, 'AARON'), (3, 2, 'BRETT')

INSERT INTO temp.dbo.TRANSACTIONS
VALUES (4, 1, 1, 'SALE', GETDATE(), 120),
(6, 1, 1, 'REFUND', GETDATE(), 120),
(7, 2, 2, 'SALE', GETDATE(), 90),
(9, 2, 1, 'SALE', GETDATE(), 30),
(10, 2, 1, 'SALE', GETDATE(), 30),
(11, 2, 1, 'SALE', GETDATE(), 30),
(12, 2, 1, 'REFUND', GETDATE(), 30)


Query: This is my current query where I have tried to name it using WITH clause but no luck



SELECT 
U.UserID, U.SiteID, U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name









share|improve this question
















If you just create "temp" database the following code can create and add sample data into the tables.



Query is to subtract TOTAL REFUNDS FROM TOTAL SALES which results in a new column named NET TOTAL.



Show: UserID, SiteID, Name, TOTAL Sales, Total Refunds, Net Total



image linked for sample data



Thanks



enter image description here



CREATE TABLE temp.dbo.Users 
(
UserID INT,
SiteID INT,
Name VARCHAR(20)
)

CREATE TABLE temp.dbo.TRANSACTIONS
(
TransactionID int,
UserID int,
SiteID int,
TransactionType varchar(10),
TransactionDate Date,
Amount money
)

INSERT INTO temp.dbo.Users
VALUES (1, 1, 'ARTHUR'), (2, 1, 'AARON'), (3, 2, 'BRETT')

INSERT INTO temp.dbo.TRANSACTIONS
VALUES (4, 1, 1, 'SALE', GETDATE(), 120),
(6, 1, 1, 'REFUND', GETDATE(), 120),
(7, 2, 2, 'SALE', GETDATE(), 90),
(9, 2, 1, 'SALE', GETDATE(), 30),
(10, 2, 1, 'SALE', GETDATE(), 30),
(11, 2, 1, 'SALE', GETDATE(), 30),
(12, 2, 1, 'REFUND', GETDATE(), 30)


Query: This is my current query where I have tried to name it using WITH clause but no luck



SELECT 
U.UserID, U.SiteID, U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name






sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 18:25









marc_s

583k13011241270




583k13011241270










asked Jan 3 at 16:54









Zabi SidiqkhilZabi Sidiqkhil

896




896













  • Could you clarify your question? I'm not sure what you are looking to get help with. As in, what is your expected output?

    – sniperd
    Jan 3 at 17:01








  • 1





    Looks like he is looking for someone to do his homework for him.No effort, a task as easy as what you would ask in a lesson about sql...

    – TomTom
    Jan 3 at 17:04











  • I had a subtract in the refunds by default however that was an easy job. I was curious to resolve it if that "-" subtract was not there. indeed i got the answer that way but I dont think that was ideal in work environment. needed how to do a postive number from a positive number where the columns are only named in the query. anyways i got the answer, and didnt know this was a way to do it, [NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]

    – Zabi Sidiqkhil
    Jan 3 at 17:22



















  • Could you clarify your question? I'm not sure what you are looking to get help with. As in, what is your expected output?

    – sniperd
    Jan 3 at 17:01








  • 1





    Looks like he is looking for someone to do his homework for him.No effort, a task as easy as what you would ask in a lesson about sql...

    – TomTom
    Jan 3 at 17:04











  • I had a subtract in the refunds by default however that was an easy job. I was curious to resolve it if that "-" subtract was not there. indeed i got the answer that way but I dont think that was ideal in work environment. needed how to do a postive number from a positive number where the columns are only named in the query. anyways i got the answer, and didnt know this was a way to do it, [NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]

    – Zabi Sidiqkhil
    Jan 3 at 17:22

















Could you clarify your question? I'm not sure what you are looking to get help with. As in, what is your expected output?

– sniperd
Jan 3 at 17:01







Could you clarify your question? I'm not sure what you are looking to get help with. As in, what is your expected output?

– sniperd
Jan 3 at 17:01






1




1





Looks like he is looking for someone to do his homework for him.No effort, a task as easy as what you would ask in a lesson about sql...

– TomTom
Jan 3 at 17:04





Looks like he is looking for someone to do his homework for him.No effort, a task as easy as what you would ask in a lesson about sql...

– TomTom
Jan 3 at 17:04













I had a subtract in the refunds by default however that was an easy job. I was curious to resolve it if that "-" subtract was not there. indeed i got the answer that way but I dont think that was ideal in work environment. needed how to do a postive number from a positive number where the columns are only named in the query. anyways i got the answer, and didnt know this was a way to do it, [NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]

– Zabi Sidiqkhil
Jan 3 at 17:22





I had a subtract in the refunds by default however that was an easy job. I was curious to resolve it if that "-" subtract was not there. indeed i got the answer that way but I dont think that was ideal in work environment. needed how to do a postive number from a positive number where the columns are only named in the query. anyways i got the answer, and didnt know this was a way to do it, [NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]

– Zabi Sidiqkhil
Jan 3 at 17:22












2 Answers
2






active

oldest

votes


















2














Welcome to Stack Overflow! Your question is very good, but it would be better if you provided an expected result set along with your sample data and work in progress.



There are a few approaches that will solve your problem:



SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) - 
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END)
AS [NET TOTAL]


or



SUM(CASE 
WHEN T.TransactionType = 'SALE' THEN T.Amount
WHEN T.TransactionType = 'REFUND' THEN -1 * T.Amount
ELSE 0
END) AS [NET TOTAL]





share|improve this answer



















  • 3





    Why has this been downvoted?

    – Larnu
    Jan 3 at 17:07











  • maybe because someone didn't like your answer.

    – Alvaro Parra
    Jan 3 at 17:12






  • 1





    @AlvaroParra It's not my answer. I just see no reason why it's been downvoted twice, and without an (constructive) reason; as it does answer the question.

    – Larnu
    Jan 3 at 17:15













  • look at me, that happened to me twice here, just take it easy ;)

    – Alvaro Parra
    Jan 3 at 17:17











  • Thanks this answer works work, with this i wont need to use WITH clause and all is sorted within a single query. But since I need Refunds total and sales total i will be doing 2 other sum cases for them and so they are presented in the results.

    – Zabi Sidiqkhil
    Jan 3 at 17:45





















0














This might work



with c as(
SELECT
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS]
-- SUM(T.Amount) AS [TOTALS]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name
)
select *
,[NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]
from c


also you can do this



SELECT 
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS],
-- SUM(T.Amount) AS [TOTALS]
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END)
- SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) as [NET TOTAL]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name





share|improve this answer


























  • Thank you so much it works, i will study your query. cheers

    – Zabi Sidiqkhil
    Jan 3 at 17:07












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026561%2fsql-server-any-work-around-to-get-net-total-sales-refund-please%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Welcome to Stack Overflow! Your question is very good, but it would be better if you provided an expected result set along with your sample data and work in progress.



There are a few approaches that will solve your problem:



SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) - 
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END)
AS [NET TOTAL]


or



SUM(CASE 
WHEN T.TransactionType = 'SALE' THEN T.Amount
WHEN T.TransactionType = 'REFUND' THEN -1 * T.Amount
ELSE 0
END) AS [NET TOTAL]





share|improve this answer



















  • 3





    Why has this been downvoted?

    – Larnu
    Jan 3 at 17:07











  • maybe because someone didn't like your answer.

    – Alvaro Parra
    Jan 3 at 17:12






  • 1





    @AlvaroParra It's not my answer. I just see no reason why it's been downvoted twice, and without an (constructive) reason; as it does answer the question.

    – Larnu
    Jan 3 at 17:15













  • look at me, that happened to me twice here, just take it easy ;)

    – Alvaro Parra
    Jan 3 at 17:17











  • Thanks this answer works work, with this i wont need to use WITH clause and all is sorted within a single query. But since I need Refunds total and sales total i will be doing 2 other sum cases for them and so they are presented in the results.

    – Zabi Sidiqkhil
    Jan 3 at 17:45


















2














Welcome to Stack Overflow! Your question is very good, but it would be better if you provided an expected result set along with your sample data and work in progress.



There are a few approaches that will solve your problem:



SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) - 
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END)
AS [NET TOTAL]


or



SUM(CASE 
WHEN T.TransactionType = 'SALE' THEN T.Amount
WHEN T.TransactionType = 'REFUND' THEN -1 * T.Amount
ELSE 0
END) AS [NET TOTAL]





share|improve this answer



















  • 3





    Why has this been downvoted?

    – Larnu
    Jan 3 at 17:07











  • maybe because someone didn't like your answer.

    – Alvaro Parra
    Jan 3 at 17:12






  • 1





    @AlvaroParra It's not my answer. I just see no reason why it's been downvoted twice, and without an (constructive) reason; as it does answer the question.

    – Larnu
    Jan 3 at 17:15













  • look at me, that happened to me twice here, just take it easy ;)

    – Alvaro Parra
    Jan 3 at 17:17











  • Thanks this answer works work, with this i wont need to use WITH clause and all is sorted within a single query. But since I need Refunds total and sales total i will be doing 2 other sum cases for them and so they are presented in the results.

    – Zabi Sidiqkhil
    Jan 3 at 17:45
















2












2








2







Welcome to Stack Overflow! Your question is very good, but it would be better if you provided an expected result set along with your sample data and work in progress.



There are a few approaches that will solve your problem:



SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) - 
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END)
AS [NET TOTAL]


or



SUM(CASE 
WHEN T.TransactionType = 'SALE' THEN T.Amount
WHEN T.TransactionType = 'REFUND' THEN -1 * T.Amount
ELSE 0
END) AS [NET TOTAL]





share|improve this answer













Welcome to Stack Overflow! Your question is very good, but it would be better if you provided an expected result set along with your sample data and work in progress.



There are a few approaches that will solve your problem:



SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) - 
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END)
AS [NET TOTAL]


or



SUM(CASE 
WHEN T.TransactionType = 'SALE' THEN T.Amount
WHEN T.TransactionType = 'REFUND' THEN -1 * T.Amount
ELSE 0
END) AS [NET TOTAL]






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 17:04









Jeff BreadnerJeff Breadner

1,086617




1,086617








  • 3





    Why has this been downvoted?

    – Larnu
    Jan 3 at 17:07











  • maybe because someone didn't like your answer.

    – Alvaro Parra
    Jan 3 at 17:12






  • 1





    @AlvaroParra It's not my answer. I just see no reason why it's been downvoted twice, and without an (constructive) reason; as it does answer the question.

    – Larnu
    Jan 3 at 17:15













  • look at me, that happened to me twice here, just take it easy ;)

    – Alvaro Parra
    Jan 3 at 17:17











  • Thanks this answer works work, with this i wont need to use WITH clause and all is sorted within a single query. But since I need Refunds total and sales total i will be doing 2 other sum cases for them and so they are presented in the results.

    – Zabi Sidiqkhil
    Jan 3 at 17:45
















  • 3





    Why has this been downvoted?

    – Larnu
    Jan 3 at 17:07











  • maybe because someone didn't like your answer.

    – Alvaro Parra
    Jan 3 at 17:12






  • 1





    @AlvaroParra It's not my answer. I just see no reason why it's been downvoted twice, and without an (constructive) reason; as it does answer the question.

    – Larnu
    Jan 3 at 17:15













  • look at me, that happened to me twice here, just take it easy ;)

    – Alvaro Parra
    Jan 3 at 17:17











  • Thanks this answer works work, with this i wont need to use WITH clause and all is sorted within a single query. But since I need Refunds total and sales total i will be doing 2 other sum cases for them and so they are presented in the results.

    – Zabi Sidiqkhil
    Jan 3 at 17:45










3




3





Why has this been downvoted?

– Larnu
Jan 3 at 17:07





Why has this been downvoted?

– Larnu
Jan 3 at 17:07













maybe because someone didn't like your answer.

– Alvaro Parra
Jan 3 at 17:12





maybe because someone didn't like your answer.

– Alvaro Parra
Jan 3 at 17:12




1




1





@AlvaroParra It's not my answer. I just see no reason why it's been downvoted twice, and without an (constructive) reason; as it does answer the question.

– Larnu
Jan 3 at 17:15







@AlvaroParra It's not my answer. I just see no reason why it's been downvoted twice, and without an (constructive) reason; as it does answer the question.

– Larnu
Jan 3 at 17:15















look at me, that happened to me twice here, just take it easy ;)

– Alvaro Parra
Jan 3 at 17:17





look at me, that happened to me twice here, just take it easy ;)

– Alvaro Parra
Jan 3 at 17:17













Thanks this answer works work, with this i wont need to use WITH clause and all is sorted within a single query. But since I need Refunds total and sales total i will be doing 2 other sum cases for them and so they are presented in the results.

– Zabi Sidiqkhil
Jan 3 at 17:45







Thanks this answer works work, with this i wont need to use WITH clause and all is sorted within a single query. But since I need Refunds total and sales total i will be doing 2 other sum cases for them and so they are presented in the results.

– Zabi Sidiqkhil
Jan 3 at 17:45















0














This might work



with c as(
SELECT
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS]
-- SUM(T.Amount) AS [TOTALS]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name
)
select *
,[NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]
from c


also you can do this



SELECT 
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS],
-- SUM(T.Amount) AS [TOTALS]
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END)
- SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) as [NET TOTAL]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name





share|improve this answer


























  • Thank you so much it works, i will study your query. cheers

    – Zabi Sidiqkhil
    Jan 3 at 17:07
















0














This might work



with c as(
SELECT
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS]
-- SUM(T.Amount) AS [TOTALS]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name
)
select *
,[NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]
from c


also you can do this



SELECT 
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS],
-- SUM(T.Amount) AS [TOTALS]
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END)
- SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) as [NET TOTAL]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name





share|improve this answer


























  • Thank you so much it works, i will study your query. cheers

    – Zabi Sidiqkhil
    Jan 3 at 17:07














0












0








0







This might work



with c as(
SELECT
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS]
-- SUM(T.Amount) AS [TOTALS]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name
)
select *
,[NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]
from c


also you can do this



SELECT 
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS],
-- SUM(T.Amount) AS [TOTALS]
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END)
- SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) as [NET TOTAL]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name





share|improve this answer















This might work



with c as(
SELECT
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS]
-- SUM(T.Amount) AS [TOTALS]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name
)
select *
,[NET TOTAL] = [TOTAL SALES] - [TOTAL REFUNDS]
from c


also you can do this



SELECT 
U.UserID,
COUNT(U.UserID) OVER (PARTITION BY U.UserID) AS cnts,
U.SiteID,
U.Name,
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END) AS [TOTAL SALES],
SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) AS [TOTAL REFUNDS],
-- SUM(T.Amount) AS [TOTALS]
SUM(CASE WHEN T.TransactionType = 'SALE' THEN T.Amount ELSE 0 END)
- SUM(CASE WHEN T.TransactionType = 'REFUND' THEN T.Amount ELSE 0 END) as [NET TOTAL]
FROM
temp.dbo.Users AS U
INNER JOIN
temp.dbo.Transactions AS T ON U.UserID = T.UserID AND U.SiteID = T.SiteID
GROUP BY
U.UserID, U.SiteID, U.Name






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 17:09

























answered Jan 3 at 17:02









Alvaro ParraAlvaro Parra

6091722




6091722













  • Thank you so much it works, i will study your query. cheers

    – Zabi Sidiqkhil
    Jan 3 at 17:07



















  • Thank you so much it works, i will study your query. cheers

    – Zabi Sidiqkhil
    Jan 3 at 17:07

















Thank you so much it works, i will study your query. cheers

– Zabi Sidiqkhil
Jan 3 at 17:07





Thank you so much it works, i will study your query. cheers

– Zabi Sidiqkhil
Jan 3 at 17:07


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026561%2fsql-server-any-work-around-to-get-net-total-sales-refund-please%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas