Return rows that have a negative equivalent










-2














I have a non-normalized table with several columns. I would like to return all columns that have a positive number along with a negative number of the same value.



Example:



ID | Value
-------------
1 | 10
1 | -10
3 | 15
3 | 15
4 | -1
5 | 4


Current Output:



ID | Values
-------------
1 | 10
1 | -10
3 | 15
3 | 15


Desired Output:



ID | Value
-------------
1 | 10
1 | -10


I have made a windows function as seen below that will select absolute values that are the same, but this includes pairs where there are a positive number.



select Count(*) Over (Partition By DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([Maintenance Expense])) As cnt , * 
From WorkTemp.dbo.Customer2700Combine
Where [Maintenance Expense] Is Not Null
Order By 1 Desc,DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([NonRental Total])









share|improve this question



















  • 2




    Hi and welcome to SO. Here is a great place to start.
    – Sean Lange
    Nov 12 '18 at 22:37










  • In your example above, do you care about these values being part of the same ID? or any value regardless of the ID?
    – Saul Cruz
    Nov 12 '18 at 23:19















-2














I have a non-normalized table with several columns. I would like to return all columns that have a positive number along with a negative number of the same value.



Example:



ID | Value
-------------
1 | 10
1 | -10
3 | 15
3 | 15
4 | -1
5 | 4


Current Output:



ID | Values
-------------
1 | 10
1 | -10
3 | 15
3 | 15


Desired Output:



ID | Value
-------------
1 | 10
1 | -10


I have made a windows function as seen below that will select absolute values that are the same, but this includes pairs where there are a positive number.



select Count(*) Over (Partition By DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([Maintenance Expense])) As cnt , * 
From WorkTemp.dbo.Customer2700Combine
Where [Maintenance Expense] Is Not Null
Order By 1 Desc,DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([NonRental Total])









share|improve this question



















  • 2




    Hi and welcome to SO. Here is a great place to start.
    – Sean Lange
    Nov 12 '18 at 22:37










  • In your example above, do you care about these values being part of the same ID? or any value regardless of the ID?
    – Saul Cruz
    Nov 12 '18 at 23:19













-2












-2








-2


1





I have a non-normalized table with several columns. I would like to return all columns that have a positive number along with a negative number of the same value.



Example:



ID | Value
-------------
1 | 10
1 | -10
3 | 15
3 | 15
4 | -1
5 | 4


Current Output:



ID | Values
-------------
1 | 10
1 | -10
3 | 15
3 | 15


Desired Output:



ID | Value
-------------
1 | 10
1 | -10


I have made a windows function as seen below that will select absolute values that are the same, but this includes pairs where there are a positive number.



select Count(*) Over (Partition By DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([Maintenance Expense])) As cnt , * 
From WorkTemp.dbo.Customer2700Combine
Where [Maintenance Expense] Is Not Null
Order By 1 Desc,DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([NonRental Total])









share|improve this question















I have a non-normalized table with several columns. I would like to return all columns that have a positive number along with a negative number of the same value.



Example:



ID | Value
-------------
1 | 10
1 | -10
3 | 15
3 | 15
4 | -1
5 | 4


Current Output:



ID | Values
-------------
1 | 10
1 | -10
3 | 15
3 | 15


Desired Output:



ID | Value
-------------
1 | 10
1 | -10


I have made a windows function as seen below that will select absolute values that are the same, but this includes pairs where there are a positive number.



select Count(*) Over (Partition By DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([Maintenance Expense])) As cnt , * 
From WorkTemp.dbo.Customer2700Combine
Where [Maintenance Expense] Is Not Null
Order By 1 Desc,DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([NonRental Total])






sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 0:40









Alejandro

4,24322136




4,24322136










asked Nov 12 '18 at 22:32









Jowy

31




31







  • 2




    Hi and welcome to SO. Here is a great place to start.
    – Sean Lange
    Nov 12 '18 at 22:37










  • In your example above, do you care about these values being part of the same ID? or any value regardless of the ID?
    – Saul Cruz
    Nov 12 '18 at 23:19












  • 2




    Hi and welcome to SO. Here is a great place to start.
    – Sean Lange
    Nov 12 '18 at 22:37










  • In your example above, do you care about these values being part of the same ID? or any value regardless of the ID?
    – Saul Cruz
    Nov 12 '18 at 23:19







2




2




Hi and welcome to SO. Here is a great place to start.
– Sean Lange
Nov 12 '18 at 22:37




Hi and welcome to SO. Here is a great place to start.
– Sean Lange
Nov 12 '18 at 22:37












In your example above, do you care about these values being part of the same ID? or any value regardless of the ID?
– Saul Cruz
Nov 12 '18 at 23:19




In your example above, do you care about these values being part of the same ID? or any value regardless of the ID?
– Saul Cruz
Nov 12 '18 at 23:19












1 Answer
1






active

oldest

votes


















0














Not sure if your requirement is by [ID], looking at your example, description and desired output, this is how I would do it:



DROP TABLE IF EXISTS #sopg;

SELECT [ID],
[VALUE]
INTO #sopg
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x;

-- Assuming that one ID can only have maximum 2 rows (like your example above) and want this by ID

SELECT s.[ID],
s.[VALUE]
FROM #sopg s
INNER JOIN
(
SELECT ID,
SUM(VALUE) SumZero
FROM #sopg
GROUP BY ID
HAVING SUM(VALUE) = 0
) SumZero ON SumZero.ID = s.ID

-- Another way, assuming that ID can have more than 2 rows and different values

DROP TABLE IF EXISTS #sopg2;

SELECT [ID],
[VALUE]
INTO #sopg2
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 1 AS ID,
-9 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x

SELECT a.[ID],
a.[VALUE]
FROM #sopg2 a
INNER JOIN #sopg b ON b.ID = a.ID AND a.VALUE = -b.VALUE





share|improve this answer


















  • 1




    Thank you so much! The second solution works perfectly, matching on a reversed value is very clever, I had no idea you could do that!
    – Jowy
    Nov 13 '18 at 20:38










  • Yeah, you can basically do every conversion/transformation on the '=' part, by the way, if you copy and paste the test data that I put in my answer as part of your input test data in your question, people might upvote your answer as it will be easier to understand and reproduce. Cheers
    – Saul Cruz
    Nov 13 '18 at 21:11










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%2f53271058%2freturn-rows-that-have-a-negative-equivalent%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Not sure if your requirement is by [ID], looking at your example, description and desired output, this is how I would do it:



DROP TABLE IF EXISTS #sopg;

SELECT [ID],
[VALUE]
INTO #sopg
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x;

-- Assuming that one ID can only have maximum 2 rows (like your example above) and want this by ID

SELECT s.[ID],
s.[VALUE]
FROM #sopg s
INNER JOIN
(
SELECT ID,
SUM(VALUE) SumZero
FROM #sopg
GROUP BY ID
HAVING SUM(VALUE) = 0
) SumZero ON SumZero.ID = s.ID

-- Another way, assuming that ID can have more than 2 rows and different values

DROP TABLE IF EXISTS #sopg2;

SELECT [ID],
[VALUE]
INTO #sopg2
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 1 AS ID,
-9 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x

SELECT a.[ID],
a.[VALUE]
FROM #sopg2 a
INNER JOIN #sopg b ON b.ID = a.ID AND a.VALUE = -b.VALUE





share|improve this answer


















  • 1




    Thank you so much! The second solution works perfectly, matching on a reversed value is very clever, I had no idea you could do that!
    – Jowy
    Nov 13 '18 at 20:38










  • Yeah, you can basically do every conversion/transformation on the '=' part, by the way, if you copy and paste the test data that I put in my answer as part of your input test data in your question, people might upvote your answer as it will be easier to understand and reproduce. Cheers
    – Saul Cruz
    Nov 13 '18 at 21:11















0














Not sure if your requirement is by [ID], looking at your example, description and desired output, this is how I would do it:



DROP TABLE IF EXISTS #sopg;

SELECT [ID],
[VALUE]
INTO #sopg
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x;

-- Assuming that one ID can only have maximum 2 rows (like your example above) and want this by ID

SELECT s.[ID],
s.[VALUE]
FROM #sopg s
INNER JOIN
(
SELECT ID,
SUM(VALUE) SumZero
FROM #sopg
GROUP BY ID
HAVING SUM(VALUE) = 0
) SumZero ON SumZero.ID = s.ID

-- Another way, assuming that ID can have more than 2 rows and different values

DROP TABLE IF EXISTS #sopg2;

SELECT [ID],
[VALUE]
INTO #sopg2
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 1 AS ID,
-9 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x

SELECT a.[ID],
a.[VALUE]
FROM #sopg2 a
INNER JOIN #sopg b ON b.ID = a.ID AND a.VALUE = -b.VALUE





share|improve this answer


















  • 1




    Thank you so much! The second solution works perfectly, matching on a reversed value is very clever, I had no idea you could do that!
    – Jowy
    Nov 13 '18 at 20:38










  • Yeah, you can basically do every conversion/transformation on the '=' part, by the way, if you copy and paste the test data that I put in my answer as part of your input test data in your question, people might upvote your answer as it will be easier to understand and reproduce. Cheers
    – Saul Cruz
    Nov 13 '18 at 21:11













0












0








0






Not sure if your requirement is by [ID], looking at your example, description and desired output, this is how I would do it:



DROP TABLE IF EXISTS #sopg;

SELECT [ID],
[VALUE]
INTO #sopg
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x;

-- Assuming that one ID can only have maximum 2 rows (like your example above) and want this by ID

SELECT s.[ID],
s.[VALUE]
FROM #sopg s
INNER JOIN
(
SELECT ID,
SUM(VALUE) SumZero
FROM #sopg
GROUP BY ID
HAVING SUM(VALUE) = 0
) SumZero ON SumZero.ID = s.ID

-- Another way, assuming that ID can have more than 2 rows and different values

DROP TABLE IF EXISTS #sopg2;

SELECT [ID],
[VALUE]
INTO #sopg2
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 1 AS ID,
-9 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x

SELECT a.[ID],
a.[VALUE]
FROM #sopg2 a
INNER JOIN #sopg b ON b.ID = a.ID AND a.VALUE = -b.VALUE





share|improve this answer














Not sure if your requirement is by [ID], looking at your example, description and desired output, this is how I would do it:



DROP TABLE IF EXISTS #sopg;

SELECT [ID],
[VALUE]
INTO #sopg
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x;

-- Assuming that one ID can only have maximum 2 rows (like your example above) and want this by ID

SELECT s.[ID],
s.[VALUE]
FROM #sopg s
INNER JOIN
(
SELECT ID,
SUM(VALUE) SumZero
FROM #sopg
GROUP BY ID
HAVING SUM(VALUE) = 0
) SumZero ON SumZero.ID = s.ID

-- Another way, assuming that ID can have more than 2 rows and different values

DROP TABLE IF EXISTS #sopg2;

SELECT [ID],
[VALUE]
INTO #sopg2
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 1 AS ID,
-9 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x

SELECT a.[ID],
a.[VALUE]
FROM #sopg2 a
INNER JOIN #sopg b ON b.ID = a.ID AND a.VALUE = -b.VALUE






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 0:19

























answered Nov 12 '18 at 23:26









Saul Cruz

33229




33229







  • 1




    Thank you so much! The second solution works perfectly, matching on a reversed value is very clever, I had no idea you could do that!
    – Jowy
    Nov 13 '18 at 20:38










  • Yeah, you can basically do every conversion/transformation on the '=' part, by the way, if you copy and paste the test data that I put in my answer as part of your input test data in your question, people might upvote your answer as it will be easier to understand and reproduce. Cheers
    – Saul Cruz
    Nov 13 '18 at 21:11












  • 1




    Thank you so much! The second solution works perfectly, matching on a reversed value is very clever, I had no idea you could do that!
    – Jowy
    Nov 13 '18 at 20:38










  • Yeah, you can basically do every conversion/transformation on the '=' part, by the way, if you copy and paste the test data that I put in my answer as part of your input test data in your question, people might upvote your answer as it will be easier to understand and reproduce. Cheers
    – Saul Cruz
    Nov 13 '18 at 21:11







1




1




Thank you so much! The second solution works perfectly, matching on a reversed value is very clever, I had no idea you could do that!
– Jowy
Nov 13 '18 at 20:38




Thank you so much! The second solution works perfectly, matching on a reversed value is very clever, I had no idea you could do that!
– Jowy
Nov 13 '18 at 20:38












Yeah, you can basically do every conversion/transformation on the '=' part, by the way, if you copy and paste the test data that I put in my answer as part of your input test data in your question, people might upvote your answer as it will be easier to understand and reproduce. Cheers
– Saul Cruz
Nov 13 '18 at 21:11




Yeah, you can basically do every conversion/transformation on the '=' part, by the way, if you copy and paste the test data that I put in my answer as part of your input test data in your question, people might upvote your answer as it will be easier to understand and reproduce. Cheers
– Saul Cruz
Nov 13 '18 at 21:11

















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53271058%2freturn-rows-that-have-a-negative-equivalent%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党