SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Calculate Difference between 2 queries Columns in union ALL


How to Calculate Difference between 2 queries Columns in union ALL

Author
Message
asita
asita
Say Hey Kid
Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)

Group: General Forum Members
Points: 662 Visits: 337
Hello Good Afternoon,

Can you please help me with below query how to get the another column in the output as (First_Bal - S_Balance) ?


SELECT
COL1, Col2, Col3, First_Bal, First_Qty, S_Balance
--- New Column which is First_bal - S_Balance here how???
FROM (
SELECT COL1, Col2, Col3, SUM(Balance) First_Bal,
Sum(Quantity) First_Qty, 0 as S_Balance
from Table2
Group by COL1, Col2, Col3

UNION ALL,

SELECT COL1, Col2, Col3, 0 First_Bal, 0 First_Qty,
SUM(MyBalance) as S_Balance
from Table1
Group by COL1, Col2, Col3
)



Thank you in advance
ASita
asita
asita
Say Hey Kid
Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)

Group: General Forum Members
Points: 662 Visits: 337
Hello there can somebody please give suggestions.

Thank you
DesNorton
DesNorton
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5094 Visits: 3963
asita - Sunday, July 16, 2017 2:40 PM
Hello Good Afternoon,

Can you please help me with below query how to get the another column in the output as (First_Bal - S_Balance) ?


SELECT
COL1, Col2, Col3, First_Bal, First_Qty, S_Balance
--- New Column which is First_bal - S_Balance here how???
FROM (
SELECT COL1, Col2, Col3, SUM(Balance) First_Bal,
Sum(Quantity) First_Qty, 0 as S_Balance
from Table2
Group by COL1, Col2, Col3

UNION ALL,

SELECT COL1, Col2, Col3, 0 First_Bal, 0 First_Qty,
SUM(MyBalance) as S_Balance
from Table1
Group by COL1, Col2, Col3
)



Thank you in advance
ASita


SELECT
COL1, Col2, Col3, First_Bal, First_Qty, S_Balance
, First_bal - S_Balance AS New_Balance
FROM (
SELECT COL1, Col2, Col3, SUM(Balance) First_Bal,
Sum(Quantity) First_Qty, 0 as S_Balance
from Table2
Group by COL1, Col2, Col3

UNION ALL,

SELECT COL1, Col2, Col3, 0 First_Bal, 0 First_Qty,
SUM(MyBalance) as S_Balance
from Table1
Group by COL1, Col2, Col3
)




How to post data/code on a forum to get the best help.Make sure that you include code in the appropriate IFCode tags.
Anjan@Sql
Anjan@Sql
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1250 Visits: 1498
adding ALIAS NAME for a derived table.

SELECT
COL1, Col2, Col3, First_Bal, First_Qty, S_Balance
, First_bal - S_Balance AS New_Balance
FROM (
SELECT COL1, Col2, Col3, SUM(Balance) First_Bal,
Sum(Quantity) First_Qty, 0 as S_Balance
from Table2
Group by COL1, Col2, Col3

UNION ALL,

SELECT COL1, Col2, Col3, 0 First_Bal, 0 First_Qty,
SUM(MyBalance) as S_Balance
from Table1
Group by COL1, Col2, Col3
) tem
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41941 Visits: 20008
Anjan@Sql - Monday, July 17, 2017 1:07 AM
adding ALIAS NAME for a derived table.

SELECT
COL1, Col2, Col3, First_Bal, First_Qty, S_Balance
, First_bal - S_Balance AS New_Balance
FROM (
SELECT COL1, Col2, Col3, SUM(Balance) First_Bal,
Sum(Quantity) First_Qty, 0 as S_Balance
from Table2
Group by COL1, Col2, Col3

UNION ALL,

SELECT COL1, Col2, Col3, 0 First_Bal, 0 First_Qty,
SUM(MyBalance) as S_Balance
from Table1
Group by COL1, Col2, Col3
) tem


You might want to remove the comma after UNION ALL too.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Anjan@Sql
Anjan@Sql
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1250 Visits: 1498
Yes that's correct Chris..

SELECT
COL1, Col2, Col3, First_Bal, First_Qty, S_Balance
, First_bal - S_Balance AS New_Balance
FROM (
SELECT COL1, Col2, Col3, SUM(Balance) First_Bal,
Sum(Quantity) First_Qty, 0 as S_Balance
from Table2
Group by COL1, Col2, Col3

UNION ALL
SELECT COL1, Col2, Col3, 0 First_Bal, 0 First_Qty,
SUM(MyBalance) as S_Balance
from Table1
Group by COL1, Col2, Col3
) tem
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search