Sql row separation2019 Community Moderator ElectionSql concatenate of row data into columnHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Parameterize an SQL IN clauseInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL table
Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?
My adviser wants to be the first author
Min function accepting varying number of arguments in C++17
Unexpected result from ArcLength
Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?
How do I hide Chekhov's Gun?
Professor being mistaken for a grad student
Happy pi day, everyone!
Charles Hockett - 'F' article?
What are substitutions for coconut in curry?
Define, (actually define) the "stability" and "energy" of a compound
Most cost effective thermostat setting: consistent temperature vs. lowest temperature possible
Time travel from stationary position?
How Could an Airship Be Repaired Mid-Flight
(Calculus) Derivative Thinking Question
Why do Australian milk farmers need to protest supermarkets' milk price?
Use void Apex method in Lightning Web Component
Interplanetary conflict, some disease destroys the ability to understand or appreciate music
Life insurance that covers only simultaneous/dual deaths
A limit with limit zero everywhere must be zero somewhere
Can a druid choose the size of its wild shape beast?
Why did it take so long to abandon sail after steamships were demonstrated?
It's a yearly task, alright
Hacking a Safe Lock after 3 tries
Sql row separation
2019 Community Moderator ElectionSql concatenate of row data into columnHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Parameterize an SQL IN clauseInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL table
Table Schema:
Create Table
(
Transaction CHAR(18),
Serial INT,
Project CHAR(3),
Amount MONEY
CONSTRAINT [PK_TRANSACTION_SERIAL] PRIMARY KEY CLUSTERED
(
Transaction ASC,
Serial ASC
)
)
Data set:
+-----------------+-------+---------+---------+
| Transaction | Serial| Project | Amount |
+-----------------+-------+---------+---------+
| A00000000000001 | 1 | 100 | 500 |
| A00000000000001 | 2 | 200 | -200 |
| A00000000000001 | 3 | 200 | -100 |
| A00000000000001 | 4 | 101 | -200 |
| A00000000000002 | 1 | 100 | 100 |
| A00000000000002 | 2 | 101 | -100 |
| A00000000000003 | 1 | 100 | 300 |
| A00000000000003 | 2 | 200 | -300 |
| A00000000000004 | 1 | 200 | -200 |
| A00000000000004 | 2 | 100 | 100 |
| A00000000000004 | 3 | 101 | 100 |
| A00000000000005 | 1 | 200 | 200 |
| A00000000000005 | 2 | 100 | -300 |
| A00000000000005 | 3 | 101 | 100 |
+-----------------+-------+---------+---------+
For any transaction there will be either 1 positive balance against multiple negative balance or 1 negative against multiple positive.
There will be no transaction as many positive amount against many negative amount.
Transactions with 1 positive and 1 negative amount is the best case scenario.
My target is to make all transactions 1 to 1 as following.
In first row at output amount for project 200 is merged as 2nd and 2rd row of dataset has same transaction ans same project.
Here the max ABS(amount) row for each transaction is being splitted into multiple rows according to the value of amount of other rows of that transaction.
Output:
+-----------------+---------+---------+
| Transaction | Project | Amount |
+-----------------+---------+---------+
| A00000000000001 | 100 | 300 |
| A00000000000001 | 200 | -300 |
---------------------------------------
| A00000000000001 | 100 | 200 |
| A00000000000001 | 101 | -200 |
---------------------------------------
| A00000000000002 | 100 | 100 |
| A00000000000002 | 101 | -100 |
---------------------------------------
| A00000000000003 | 100 | 300 |
| A00000000000003 | 200 | -300 |
---------------------------------------
| A00000000000004 | 200 | -100 |
| A00000000000004 | 100 | 100 |
---------------------------------------
| A00000000000004 | 200 | -100 |
| A00000000000004 | 101 | 100 |
---------------------------------------
| A00000000000005 | 200 | 200 |
| A00000000000005 | 100 | -200 |
---------------------------------------
| A00000000000005 | 101 | 100 |
| A00000000000005 | 100 | -100 |
+-----------------+---------+---------+
I am using SQL SERVER 2012 or higher.
sql
add a comment |
Table Schema:
Create Table
(
Transaction CHAR(18),
Serial INT,
Project CHAR(3),
Amount MONEY
CONSTRAINT [PK_TRANSACTION_SERIAL] PRIMARY KEY CLUSTERED
(
Transaction ASC,
Serial ASC
)
)
Data set:
+-----------------+-------+---------+---------+
| Transaction | Serial| Project | Amount |
+-----------------+-------+---------+---------+
| A00000000000001 | 1 | 100 | 500 |
| A00000000000001 | 2 | 200 | -200 |
| A00000000000001 | 3 | 200 | -100 |
| A00000000000001 | 4 | 101 | -200 |
| A00000000000002 | 1 | 100 | 100 |
| A00000000000002 | 2 | 101 | -100 |
| A00000000000003 | 1 | 100 | 300 |
| A00000000000003 | 2 | 200 | -300 |
| A00000000000004 | 1 | 200 | -200 |
| A00000000000004 | 2 | 100 | 100 |
| A00000000000004 | 3 | 101 | 100 |
| A00000000000005 | 1 | 200 | 200 |
| A00000000000005 | 2 | 100 | -300 |
| A00000000000005 | 3 | 101 | 100 |
+-----------------+-------+---------+---------+
For any transaction there will be either 1 positive balance against multiple negative balance or 1 negative against multiple positive.
There will be no transaction as many positive amount against many negative amount.
Transactions with 1 positive and 1 negative amount is the best case scenario.
My target is to make all transactions 1 to 1 as following.
In first row at output amount for project 200 is merged as 2nd and 2rd row of dataset has same transaction ans same project.
Here the max ABS(amount) row for each transaction is being splitted into multiple rows according to the value of amount of other rows of that transaction.
Output:
+-----------------+---------+---------+
| Transaction | Project | Amount |
+-----------------+---------+---------+
| A00000000000001 | 100 | 300 |
| A00000000000001 | 200 | -300 |
---------------------------------------
| A00000000000001 | 100 | 200 |
| A00000000000001 | 101 | -200 |
---------------------------------------
| A00000000000002 | 100 | 100 |
| A00000000000002 | 101 | -100 |
---------------------------------------
| A00000000000003 | 100 | 300 |
| A00000000000003 | 200 | -300 |
---------------------------------------
| A00000000000004 | 200 | -100 |
| A00000000000004 | 100 | 100 |
---------------------------------------
| A00000000000004 | 200 | -100 |
| A00000000000004 | 101 | 100 |
---------------------------------------
| A00000000000005 | 200 | 200 |
| A00000000000005 | 100 | -200 |
---------------------------------------
| A00000000000005 | 101 | 100 |
| A00000000000005 | 100 | -100 |
+-----------------+---------+---------+
I am using SQL SERVER 2012 or higher.
sql
It makes no sense how split value 500 for A...1 and Project 100 into 2 amounts? The rest really is a select sum(Amount) group by transaction, project from table.
– nolaspeaker
Dec 30 '15 at 3:56
For Transaction A...1 Project 100 has amount 500 and project 200 has amount -300(after summation of same project in same transaction) and project 101 has amount -200. So I want to relate Project 100 to 200 with amount 300 & -300 and Project 100 to 101 with amount 200 & -200.
– Esty
Dec 30 '15 at 4:00
is there ANY other column available such as ID (unique to each row)? How do you intend to ensure a row isn't included more than once?
– Used_By_Already
Dec 30 '15 at 4:10
See my updated dataset.
– Esty
Dec 30 '15 at 4:13
add a comment |
Table Schema:
Create Table
(
Transaction CHAR(18),
Serial INT,
Project CHAR(3),
Amount MONEY
CONSTRAINT [PK_TRANSACTION_SERIAL] PRIMARY KEY CLUSTERED
(
Transaction ASC,
Serial ASC
)
)
Data set:
+-----------------+-------+---------+---------+
| Transaction | Serial| Project | Amount |
+-----------------+-------+---------+---------+
| A00000000000001 | 1 | 100 | 500 |
| A00000000000001 | 2 | 200 | -200 |
| A00000000000001 | 3 | 200 | -100 |
| A00000000000001 | 4 | 101 | -200 |
| A00000000000002 | 1 | 100 | 100 |
| A00000000000002 | 2 | 101 | -100 |
| A00000000000003 | 1 | 100 | 300 |
| A00000000000003 | 2 | 200 | -300 |
| A00000000000004 | 1 | 200 | -200 |
| A00000000000004 | 2 | 100 | 100 |
| A00000000000004 | 3 | 101 | 100 |
| A00000000000005 | 1 | 200 | 200 |
| A00000000000005 | 2 | 100 | -300 |
| A00000000000005 | 3 | 101 | 100 |
+-----------------+-------+---------+---------+
For any transaction there will be either 1 positive balance against multiple negative balance or 1 negative against multiple positive.
There will be no transaction as many positive amount against many negative amount.
Transactions with 1 positive and 1 negative amount is the best case scenario.
My target is to make all transactions 1 to 1 as following.
In first row at output amount for project 200 is merged as 2nd and 2rd row of dataset has same transaction ans same project.
Here the max ABS(amount) row for each transaction is being splitted into multiple rows according to the value of amount of other rows of that transaction.
Output:
+-----------------+---------+---------+
| Transaction | Project | Amount |
+-----------------+---------+---------+
| A00000000000001 | 100 | 300 |
| A00000000000001 | 200 | -300 |
---------------------------------------
| A00000000000001 | 100 | 200 |
| A00000000000001 | 101 | -200 |
---------------------------------------
| A00000000000002 | 100 | 100 |
| A00000000000002 | 101 | -100 |
---------------------------------------
| A00000000000003 | 100 | 300 |
| A00000000000003 | 200 | -300 |
---------------------------------------
| A00000000000004 | 200 | -100 |
| A00000000000004 | 100 | 100 |
---------------------------------------
| A00000000000004 | 200 | -100 |
| A00000000000004 | 101 | 100 |
---------------------------------------
| A00000000000005 | 200 | 200 |
| A00000000000005 | 100 | -200 |
---------------------------------------
| A00000000000005 | 101 | 100 |
| A00000000000005 | 100 | -100 |
+-----------------+---------+---------+
I am using SQL SERVER 2012 or higher.
sql
Table Schema:
Create Table
(
Transaction CHAR(18),
Serial INT,
Project CHAR(3),
Amount MONEY
CONSTRAINT [PK_TRANSACTION_SERIAL] PRIMARY KEY CLUSTERED
(
Transaction ASC,
Serial ASC
)
)
Data set:
+-----------------+-------+---------+---------+
| Transaction | Serial| Project | Amount |
+-----------------+-------+---------+---------+
| A00000000000001 | 1 | 100 | 500 |
| A00000000000001 | 2 | 200 | -200 |
| A00000000000001 | 3 | 200 | -100 |
| A00000000000001 | 4 | 101 | -200 |
| A00000000000002 | 1 | 100 | 100 |
| A00000000000002 | 2 | 101 | -100 |
| A00000000000003 | 1 | 100 | 300 |
| A00000000000003 | 2 | 200 | -300 |
| A00000000000004 | 1 | 200 | -200 |
| A00000000000004 | 2 | 100 | 100 |
| A00000000000004 | 3 | 101 | 100 |
| A00000000000005 | 1 | 200 | 200 |
| A00000000000005 | 2 | 100 | -300 |
| A00000000000005 | 3 | 101 | 100 |
+-----------------+-------+---------+---------+
For any transaction there will be either 1 positive balance against multiple negative balance or 1 negative against multiple positive.
There will be no transaction as many positive amount against many negative amount.
Transactions with 1 positive and 1 negative amount is the best case scenario.
My target is to make all transactions 1 to 1 as following.
In first row at output amount for project 200 is merged as 2nd and 2rd row of dataset has same transaction ans same project.
Here the max ABS(amount) row for each transaction is being splitted into multiple rows according to the value of amount of other rows of that transaction.
Output:
+-----------------+---------+---------+
| Transaction | Project | Amount |
+-----------------+---------+---------+
| A00000000000001 | 100 | 300 |
| A00000000000001 | 200 | -300 |
---------------------------------------
| A00000000000001 | 100 | 200 |
| A00000000000001 | 101 | -200 |
---------------------------------------
| A00000000000002 | 100 | 100 |
| A00000000000002 | 101 | -100 |
---------------------------------------
| A00000000000003 | 100 | 300 |
| A00000000000003 | 200 | -300 |
---------------------------------------
| A00000000000004 | 200 | -100 |
| A00000000000004 | 100 | 100 |
---------------------------------------
| A00000000000004 | 200 | -100 |
| A00000000000004 | 101 | 100 |
---------------------------------------
| A00000000000005 | 200 | 200 |
| A00000000000005 | 100 | -200 |
---------------------------------------
| A00000000000005 | 101 | 100 |
| A00000000000005 | 100 | -100 |
+-----------------+---------+---------+
I am using SQL SERVER 2012 or higher.
sql
sql
edited Mar 7 at 14:04
Cœur
18.9k9110153
18.9k9110153
asked Dec 30 '15 at 3:21
EstyEsty
1,3531927
1,3531927
It makes no sense how split value 500 for A...1 and Project 100 into 2 amounts? The rest really is a select sum(Amount) group by transaction, project from table.
– nolaspeaker
Dec 30 '15 at 3:56
For Transaction A...1 Project 100 has amount 500 and project 200 has amount -300(after summation of same project in same transaction) and project 101 has amount -200. So I want to relate Project 100 to 200 with amount 300 & -300 and Project 100 to 101 with amount 200 & -200.
– Esty
Dec 30 '15 at 4:00
is there ANY other column available such as ID (unique to each row)? How do you intend to ensure a row isn't included more than once?
– Used_By_Already
Dec 30 '15 at 4:10
See my updated dataset.
– Esty
Dec 30 '15 at 4:13
add a comment |
It makes no sense how split value 500 for A...1 and Project 100 into 2 amounts? The rest really is a select sum(Amount) group by transaction, project from table.
– nolaspeaker
Dec 30 '15 at 3:56
For Transaction A...1 Project 100 has amount 500 and project 200 has amount -300(after summation of same project in same transaction) and project 101 has amount -200. So I want to relate Project 100 to 200 with amount 300 & -300 and Project 100 to 101 with amount 200 & -200.
– Esty
Dec 30 '15 at 4:00
is there ANY other column available such as ID (unique to each row)? How do you intend to ensure a row isn't included more than once?
– Used_By_Already
Dec 30 '15 at 4:10
See my updated dataset.
– Esty
Dec 30 '15 at 4:13
It makes no sense how split value 500 for A...1 and Project 100 into 2 amounts? The rest really is a select sum(Amount) group by transaction, project from table.
– nolaspeaker
Dec 30 '15 at 3:56
It makes no sense how split value 500 for A...1 and Project 100 into 2 amounts? The rest really is a select sum(Amount) group by transaction, project from table.
– nolaspeaker
Dec 30 '15 at 3:56
For Transaction A...1 Project 100 has amount 500 and project 200 has amount -300(after summation of same project in same transaction) and project 101 has amount -200. So I want to relate Project 100 to 200 with amount 300 & -300 and Project 100 to 101 with amount 200 & -200.
– Esty
Dec 30 '15 at 4:00
For Transaction A...1 Project 100 has amount 500 and project 200 has amount -300(after summation of same project in same transaction) and project 101 has amount -200. So I want to relate Project 100 to 200 with amount 300 & -300 and Project 100 to 101 with amount 200 & -200.
– Esty
Dec 30 '15 at 4:00
is there ANY other column available such as ID (unique to each row)? How do you intend to ensure a row isn't included more than once?
– Used_By_Already
Dec 30 '15 at 4:10
is there ANY other column available such as ID (unique to each row)? How do you intend to ensure a row isn't included more than once?
– Used_By_Already
Dec 30 '15 at 4:10
See my updated dataset.
– Esty
Dec 30 '15 at 4:13
See my updated dataset.
– Esty
Dec 30 '15 at 4:13
add a comment |
1 Answer
1
active
oldest
votes
I am not sure about Row separation approach but for getting this 1-1 (negative and positive) value you can order your output.
you can order based on transaction and then absolute value of your amount like -
select *
from <your_table_name>
order by Transaction, abs(Amount)
With your approach order by transaction and ABS(Amount) and taking the max(Amount) for each transaction I may find which row I need to split in multiple. But How I am going to split it?
– Esty
Dec 30 '15 at 3:56
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34522483%2fsql-row-separation%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
I am not sure about Row separation approach but for getting this 1-1 (negative and positive) value you can order your output.
you can order based on transaction and then absolute value of your amount like -
select *
from <your_table_name>
order by Transaction, abs(Amount)
With your approach order by transaction and ABS(Amount) and taking the max(Amount) for each transaction I may find which row I need to split in multiple. But How I am going to split it?
– Esty
Dec 30 '15 at 3:56
add a comment |
I am not sure about Row separation approach but for getting this 1-1 (negative and positive) value you can order your output.
you can order based on transaction and then absolute value of your amount like -
select *
from <your_table_name>
order by Transaction, abs(Amount)
With your approach order by transaction and ABS(Amount) and taking the max(Amount) for each transaction I may find which row I need to split in multiple. But How I am going to split it?
– Esty
Dec 30 '15 at 3:56
add a comment |
I am not sure about Row separation approach but for getting this 1-1 (negative and positive) value you can order your output.
you can order based on transaction and then absolute value of your amount like -
select *
from <your_table_name>
order by Transaction, abs(Amount)
I am not sure about Row separation approach but for getting this 1-1 (negative and positive) value you can order your output.
you can order based on transaction and then absolute value of your amount like -
select *
from <your_table_name>
order by Transaction, abs(Amount)
answered Dec 30 '15 at 3:50
pratik gargpratik garg
2,79211221
2,79211221
With your approach order by transaction and ABS(Amount) and taking the max(Amount) for each transaction I may find which row I need to split in multiple. But How I am going to split it?
– Esty
Dec 30 '15 at 3:56
add a comment |
With your approach order by transaction and ABS(Amount) and taking the max(Amount) for each transaction I may find which row I need to split in multiple. But How I am going to split it?
– Esty
Dec 30 '15 at 3:56
With your approach order by transaction and ABS(Amount) and taking the max(Amount) for each transaction I may find which row I need to split in multiple. But How I am going to split it?
– Esty
Dec 30 '15 at 3:56
With your approach order by transaction and ABS(Amount) and taking the max(Amount) for each transaction I may find which row I need to split in multiple. But How I am going to split it?
– Esty
Dec 30 '15 at 3:56
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34522483%2fsql-row-separation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
It makes no sense how split value 500 for A...1 and Project 100 into 2 amounts? The rest really is a select sum(Amount) group by transaction, project from table.
– nolaspeaker
Dec 30 '15 at 3:56
For Transaction A...1 Project 100 has amount 500 and project 200 has amount -300(after summation of same project in same transaction) and project 101 has amount -200. So I want to relate Project 100 to 200 with amount 300 & -300 and Project 100 to 101 with amount 200 & -200.
– Esty
Dec 30 '15 at 4:00
is there ANY other column available such as ID (unique to each row)? How do you intend to ensure a row isn't included more than once?
– Used_By_Already
Dec 30 '15 at 4:10
See my updated dataset.
– Esty
Dec 30 '15 at 4:13