How to add/combine SUM from 2 tables Oracle, Basic - sql

I am very new to SQL,
I have 2 code like this
Select ID, SUM(Column1) AS Sum1
from table1
Select ID, SUM(Column1) AS Sum2
from table2
AS result of table1
ID------ Sum1
001 20
003 10
004 5
AS result of table2
ID------ Sum2
001 20
003 10
005 10
I would like show the result after joining this 2 tables like this, how can I wirte the code to show like below?
ID------ Sum
001 40
003 20
004 5
005 10
Thank you so much

SELECT ID, SUM(COLUMN1)
FROM (
SELECT ID, COLUMN1 FROM T1
UNION ALL
SELECT ID, COLUMN1 FROM T2
)
GROUP BY ID;
note here that UNION ALL is used instead of UNION so you can show duplicated results

You can FULL JOIN the two subqueries together (after adding a GROUP BY to each), and use COALESCE() to return the proper value:
SELECT COALESCE(a.ID,b.ID) AS ID
, COALESCE(a.Sum1,0)+COALESCE(b.Sum2,0) AS Sum
FROM (Select ID, SUM(Column1) AS Sum1
from table1
GROUP BY ID
) a
FULL JOIN (Select ID, SUM(Column1) AS Sum2
from table2
GROUP BY ID
)b
ON a.ID = b.ID
FULL JOIN or FULL OUTER JOIN will return records from each side of the join whether they join or not. COALESCE() returns the first non-NULL value from the list of parameters you supply it.
Alternatively, you could UNION the two sets prior to aggregating:
SELECT ID, SUM(Column1) AS Sum
FROM (Select ID, Column1
FROM table1
UNION ALL
Select ID, Column1
FROM table2
) sub
GROUP BY ID

Related

Query to get oldest date into a column?

I have some tables that look like this:
Table1
Column1
A
B
C
Table2
column1 Id1 Id2
A 2 100
A 3 101
B 2 100
B 3 101
C 2 100
Table3
Id2 Date Item Status
100 10/20/17 A1 A
101 10/21/17 A1 A
100 11/22/17 A2 I
101 11/23/17 A2 A
My query looks like this:
Select
Date, *
FROM TABLE1 T1
LEFT JOIN Table2 T2 ON
T1.Column1 = T2.Column2
LEFT JOIN Table3 T3 ON
T2.Id2 = T3.Id2
I would like to return the oldest date on table 3 where the status is A into the date column in my query.
I was able to return the oldest date with this query but can't integrate this into my query with joins.
SELECT
MIN(DATE)
FROM Table3
WHERE Id2 IN (100,101)
AND STATUS = 'A'
group by Id2, ITEM
How can I get the oldest date into the query that uses the join clauses?
Couple of ways to approach what I believe that you're trying to do.
I'd say the easiest would be to simplify your search parameters with a subquery of Table 3.
select * from table2 t2
LEFT JOIN
(Select Id2, Item, max(Date)from Table 3
WHERE Status = 'A'
GROUP BY Id2, Item)a ON t2.Id2 = a.Id2;
The subquery does the filtering and then a quick join to table 2 would get all your data.

UNION ALL Trick ORACLE

This may be so easy, but I can not figure out how I can do.
so my code is like this
FOR example
SELECT id,total,total2
FROM (select id, 0 AS total ,t1.total AS total2
FROM table1 t1
union ALL
select id, t2.total AS total, 0 AS total2
FRoM table2 t2)
it shows
id------total------totals
001 0 20
001 15 0
I would like to show like this
id------total------totals
001 15 20
How can I do that?
Thank you so much
Do an aggregation afterwards:
SELECT id, sum(total) as total, sum(total2) as total2
FROM (select id, 0 AS total ,t1.total AS total2
FROM table1 t1
union ALL
select id, t2.total AS total, 0 AS total2
FRoM table2 t2
) i
GROUP BY id;
Instead of dumping 0's into some columns as filler, you can use a WITH and subquery's to retrieve a sum on all records in the matching Id's from a distinct list of Id's.
;WITH GetIds AS (
SELECT DISTINCT id
FROM table1
UNION
SELECT DISTINCT id
FROM table2
)
SELECT id,
(SELECT SUM(total) FROM table1 t1 WHERE t1.id = ids.id) AS total1,
(SELECT SUM(total) FROM table2 t2 WHERE t2.id = ids.id) AS total2
FROM GetIds ids

How to count distinct rows and get data of the row and count of it as a second column

Let's say I have a data
ID
AAA
ABB
ABC
BDS
BRD
CXD
DCU
ETS
I would like to count distinct to a first letter rows and get the number of their appearance to the right. Sorry I know I am not a very good user of a technical language, but I am new to SQL and English is not my first language.
So by script I would like to return
ID Total
A 3
B 2
C 1
D 1
E 1
I have tried
select left(id,1), count(left(id,1) as Total
from Places
group by Id
order by Total desc;
, but it didn't work. Your help will be greatly appreciated.
select left(id,1), count(*) as Total
from Places
group by left(id,1)
order by Total desc;
Is this you need?
declare #t table(val varchar(10))
insert into #t
select 'AAA' union all
select 'ABB' union all
select 'ABC' union all
select 'BDS' union all
select 'BRD' union all
select 'CXD' union all
select 'DCU' union all
select 'ETS'
select left(t1.val,1) as id ,count(t1.val) as total from #t as t1 left join
(
select distinct right(val,1) as val from #t
) as t2 on t1.val =t2.val
group by left(t1.val,1)
Result is
id total
---- -----------
A 3
B 2
C 1
D 1
E 1

Select rows not in another table by comparing two table

I have following two tables TableA and TableB
TableA
Id Month_Id Customer_Id Total_Amount
1 1 1 50
2 2 1 150
3 3 1 200
4 1 2 75
5 2 2 100
6 1 3 400
7 2 3 200
TableB
Id Month_Id Customer_Id Total_Amount
1 1 1 50
2 2 1 150
3 1 2 75
I want to compare Month_Id Customer_Id Total_Amount in both tables and select Id from TableA. The output should be as follow.
Output
Id
3
5
6
7
My concept is:
SELECT TableA.Id FROM TableA
WHERE TableA.Month_Id <> TableB.MonthId AND
TableA.Customer_Id <> TableB.Customer_Id AND
TableA.Total_Amount <> TableB.Total_Amount
SELECT TableA.Id
FROM TableA
WHERE NOT EXISTS (
SELECT 1
FROM TableB
WHERE TableB.Month_Id = TableA.Month_Id
AND TableB.Customer_Id = TableA.Customer_Id
AND TableB.Total_Amount = TableA.Total_Amount
)
select Id
from (
select Id, Month_Id, Customer_Id, Total_Amount from TableA
except
select Id, Month_Id, Customer_Id, Total_Amount from TableB
) q
SELECT id FROM
(SELECT id, month_id, customer_id, total_ammount FROM TableA
EXCEPT
SELECT id, month_id, customer_id, total_ammount FROM TableB);
You can use the EXCEPT set operator:
SELECT id
FROM (SELECT * FROM table_a
EXCEPT
SELECT * FROM table_b) t
You can use Merge with WHEN NOT MATCHED
place your condition in ON <merge_search_condition>
SELECT Id FROM TableA A LEFT JOIN tableB B
ON A.Id=B.Id AND A.Month_Id =B.Month_Id
AND A.Customer_Id =B.Customer_Id
AND A.Total_Amount=b.Total_Amount
WHERE B.Id is NULL
In oracle sql it would be:
SELECT ID FROM
(SELECT ID, Month_Id, Customer_Id, Total_Amount FROM TABLE_A
MINUS
SELECT ID, Month_Id, Customer_Id, Total_Amount FROM TABLE_B);
Is this what you want?
(Not sure of MINUS operator in sql-server though)

How to get max value from 2 tables

Using Sql Server
I want to get max value from two table
Table1
ID Total
101 100
102 600
.....
Table2
ID Total
101 300
102 400
....
I want to get a max value from 2 table according to the id
Expected Output
ID Total
101 300 (max value in table2)
102 600 (max value in table1)
....
...
How to make a Query
Need Query Help
SELECT
ID, MAX(Total)
FROM
(
SELECT ID, Total FROM Table1
UNION ALL
SELECT ID, Total FROM Table2
) foo
GROUP BY
ID
; with
q_00 as (
select id, Total from Tbl_1
union all
select id, Total from Tbl_2
)
select id, max(Total)
from q_00
group by id
order by id ;
One other option worth considering might be
WITH T(ID, Total)
AS (SELECT ID,
MAX(Total)
FROM Table1
GROUP BY ID
UNION ALL
SELECT ID,
MAX(Total)
FROM Table2
GROUP BY ID)
SELECT ID,
MAX(Total) AS Total
FROM T
GROUP BY ID
If ID,Total is indexed in the two tables possibly this might give a better plan (untested)