DB2 how to sum two column from two different table - sql

Select sum(amt) as totalA from tableA where id>10;
Select sum(amount) as totalB from tableB where people = 'JOSH';
What is the best way if the objective is to have sum(totalA + totalB)?

select sum(total) from
(
select sum(amt) as total from tableA where id>10
union all
select sum(amount) from tableB where people = 'JOSH'
) as q

Related

I want to get difference of sum of a column from two tables

I have 2 tables: transactions and transactions_archive. Each of them has fields accountno,drcr(which has values either as C or D) and field amount. I want to get difference of sum of all 'C' in both transactions and transactions_archive and sum of all 'D' in both transactions and transactions_archive.
What query can I use to get this answer.
I tried this unsuccessfully:
select (
select accountno,drcr,sum(amount)as total from
(
select accountno,drcr,amount
from ebank.tbtransactions
where drcr='C'
union all
select accountno,drcr,amount
from ebank.tbtransactions_archive
where drcr='C'
)
)
-
(select accountno,drcr,sum(amount)as total
from (
select accountno,drcr,amount
from ebank.tbtransactions
where drcr='D'
union all
select accountno,drcr,amount
from ebank.tbtransactions_archive
where drcr='D'
)
)
group by accountno,drcr;
If I understand correctly, you want to subtract all the "D"s from the "C"s. Combine the tables using UNION ALL and use conditional aggregation:
select accountno,
sum(case when drcr = 'C' then amount else - amount end)as total
from ((select accountno, drcr, amount
from ebank.tbtransactions
) union all
(select accountno, drcr, amount
from ebank.tbtransactions_archive
)
) t
where drcr in ('D', 'C')
group by accountno;
SELECT top 1 (amount - nextamount) as diff from(
SELECT
amount,LEAD(amount, 1,0) OVER (ORDER BY YEAR(drcr)) AS nextamount FROM(
SELECT drcr, sum(amount) as amount from transactions JOIN transactions_archive on transactions.drcr and transactions_archive.drcr GROUP BY drcr))

Sum the count(*) of between three tables

I want to sum the number of counts between 3 tables. I have added three input fields to give a specific date each time but I am struggling on how to SUM the COUNTS(*)
select count(*)
from db.table1
where call_date = ${var:call_date};
select count(*)
from db.table2
where call_date = ${var:call_date};
select count(*)
from db.table3
where call_date= ${var:call_date};
thanks in advance
You can simply use them as sub-query as follows:
select (select count(*) from db.table1 where call_date = ${var:call_date})
+ (select count(*) from db.table2 where call_date = ${var:call_date})
+ (select count(*) from db.table3 where call_date= ${var:call_date})
as rslt;
UNION ALL the selects. SUM() the result.
select sum(cnt) from
(
select count(*) cnt
from db.table1
where call_date = ${var:call_date}
UNION ALL
select count(*)
from db.table2
where call_date = ${var:call_date}
UNION ALL
select count(*)
from db.table3
where call_date= ${var:call_date}
) dt

Difference from sum of column separately from different tables having same columns in sql

I have 2 tables with same columns TABLE A and TABLE B with columns QUANTITY and AMOUNT.
For single column difference i used this -
SELECT (
(SELECT SUM(QUANTITY) FROM TABLE A ) - (SELECT SUM(QUANTITY) FROM TABLE B )
)
AS DIFF FROM DUAL;
I want the difference of QUANTITY and AMOUNT columns.
SELECT SUM(QUANTITY) AS SUM1, SUM(AMOUNT) SUM2 FROM TABLE A;
SELECT SUM(QUANTITY) AS SUM3, SUM(AMOUNT) SUM4 FROM TABLE B;
I want to achieve-
SELECT (SUM1 - SUM3) AS DIFF1, (SUM2 - SUM4) AS DIFF2;
Can i do it in single select query or should i go for function or something else??
Thanks for any help in advance.
It can be simply extended to
SELECT
(SELECT SUM(QUANTITY) FROM A ) - (SELECT SUM(QUANTITY) FROM B )
AS DIFF_SUM,
(SELECT SUM(AMOUNT) FROM A ) - (SELECT SUM(AMOUNT) FROM B )
AS DIFF_QUANT FROM DUAL;
Another, perhaps more readable way to do this:
WITH a1 AS
(SELECT SUM(QUANTITY) AS SUM1, SUM(AMOUNT) SUM2 FROM A),
b1 AS
(SELECT SUM(QUANTITY) AS SUM3, SUM(AMOUNT) SUM4 FROM B)
SELECT (SUM1 - SUM3) AS DIFF1, (SUM2 - SUM4) AS DIFF2 FROM a1, b1;
If you want to really combine them, here's a third way:
SELECT SUM(Quantity), SUM(Amount)
FROM
(SELECT Quantity, Amount FROM A
UNION ALL
SELECT -Quantity, -Amount FROM B)
OR:
SELECT
SUM(A.AMOUNT) AS SUMAMOUNTA
,SUM(A.QUANTITY) AS SUMQTYA
,SUM(B.AMOUNT) AS SUMAMOUNTB
,SUM(B.QUANTITY) AS SUMQTYB
,SUM(A.QUANTITY) - SUM(B.QUANTITY) AS DIFFQTY
,SUM(A.AMOUNT) - SUM(B.AMOUNT) DIFFSUM
FROM
TABLEA A, TABLEB B;

Insert two select queries in a single row

I have two select statements, both having some common fields.
This is my table structure :
Id, year, valueA, ValueB
Here is my two select statements
SELECT
id, [year],
SUM(Total_Volume) / (count(distinct(month)) * 7)
FROM
TableA
GROUP BY
id, [year]
and
SELECT
id, [year],
SUM(Total_volume) / (count(distinct(month)) * 5)
FROM
TableA
WHERE
weekday NOT IN ('Friday','Saturday')
GROUP BY
station_id, [year]
I have two different conditions for two statements. Id and year is common for both the statements.
Result from first select statement should be stored in column valueA and result from second select statement should be stored in ValueB .
Any possible way to combine these queries and insert them into table as a single statement?
SELECT a.id ,a.[year],valA,valB
from(
SELECT id ,[year],SUM(Total_Volume)/ (count(distinct(month))*7) valA from TableA
group by id,[year]) a INNER JOIN
(select id,[year],SUM(Total_volume)/(count(distinct(month))*5) valB
from TableA
WHERE weekday NOT IN ('Friday','Saturday')
group by station_id,[year]) b
on a.id=b.id and a.[year]=b.[year]
You could do both the calculation in a single query
SELECT id,
[year],
Sum(total_volume) / ( Count(DISTINCT( Month)) * 7 ),
Sum(CASE
WHEN weekday NOT IN ('Friday', 'Saturday' ) THEN
total_volume
ELSE 0
END) / (Count(DISTINCT( Month)) * 5 )
FROM tableA
GROUP BY id,
[year];

Getting the sum of several columns from two tables

I want to get the sum of several columns from 2 different tables (these tables share the same structure).
If I only consider one table, I would write this kind of query:
SELECT MONTH_REF, SUM(amount1), SUM(amount2)
FROM T_FOO
WHERE seller = XXX
GROUP BY MONTH_REF;
However, I would like to also work with the data from the table T_BAR, and then have a select query that return the following columns:
MONTH_REF
SUM(T_FOO.amount1) + SUM(T_BAR.amount1)
SUM(T_FOO.amount2) + SUM(T_BAR.amount2)
everything grouped by the value of MONTH_REF.
Note that a record for a given MONTH_REF can be found in one table but not in the other table.
In this case, I would like to get the sum of T_FOO.amount1 + 0 (or 0 + T_BAR.amount1).
How can I write my SQL query to get this information?
For information, my database is Oracle 10g.
You can union your tables before the group by (this is on Oracle, by the way):
SELECT t.month_ref, SUM(t.amount1), SUM(t.amount2)
FROM (SELECT month_ref, amount1, amount2
FROM T_FOO
WHERE seller = XXX
UNION ALL
SELECT month_ref, amount1, amount2
FROM T_BAR
WHERE seller = XXX
) t
GROUP BY t.month_ref
You may also union the tables with the seller field and filter by it later (in case you need more advanced logic):
SELECT t.month_ref, SUM(t.amount1), SUM(t.amount2)
FROM (SELECT month_ref, amount1, amount2, seller
FROM T_FOO
UNION ALL
SELECT month_ref, amount1, amount2, seller
FROM T_BAR) t
where t.seller = XXX
GROUP BY t.month_ref
Have you tried using a union?
SELECT MONTH_REF, SUM(amount1), SUM(amount2)
FROM (
SELECT MONTH_REF, SUM(amount1) AS amount1, SUM(amount2) as amount2
FROM T_FOO
WHERE seller = XXX
GROUP BY MONTH_REF
UNION ALL SELECT MONTH_REF, SUM(amount1), SUM(amount2)
FROM T_BAR
WHERE seller = XXX
GROUP BY MONTH_REF
) tmp
GROUP BY MONTH_REF
Alternatively, an outer join should also work:
SELECT month_ref,
SUM(t_foo.amount1) + SUM(t_bar.amount1),
SUM(t_foo.amount2)+SUM(t_bar.amount2)
FROM t_foo FULL OUTER JOIN t_bar
ON t_foo.month_ref = t_bar.month_ref
GROUP BY month_ref
I finally get this working using the Lieven's answer.
Here is the correct code (amount1 = ... is not working on my environment, and there are too many ; in the query):
SELECT MONTH_REF, SUM(sumAmount1), SUM(sumAmount2)
FROM (
SELECT MONTH_REF, SUM(amount1) as sumAmount1, SUM(amount2) as sumAmount1
FROM T_FOO
WHERE seller = XXX
GROUP BY MONTH_REF
UNION ALL SELECT MONTH_REF, SUM(amount1), SUM(amount2)
FROM T_BAR
WHERE seller = XXX
GROUP BY MONTH_REF
) tmp
GROUP BY MONTH_REF
SELECT (SELECT SUM(amount) from debit) as debitamounts, (SELECT SUM(amount) from credit) as creditamounts