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
Related
I have two tables with the data of
Id Amt
1 10
2 10
Tbl2
Id Amt
1 10
I want a query that will result to this
Id Amt
1 20
2 10
I tried different joins but no luck.
Thanks!
The safest way to do this is with UNION ALL:
SELECT ID, SUM(AMT) AS AMT FROM (
SELECT ID, AMT FROM TABLE1
UNION ALL
SELECT ID, AMT FROM TABLE2
) GROUP BY ID;
By the way you can do also with a FULL OUTER JOIN being careful to group by ID:
SELECT
NVL(T1.ID, T2.ID) AS ID,
NVL(T1.AMT, 0) + NVL(T2.AMT, 0) AS AMT
FROM
(SELECT ID, SUM(AMT) AS AMT FROM TABLE1 GROUP BY ID) T1
FULL OUTER JOIN
(SELECT ID, SUM(AMT) AS AMT FROM TABLE2 GROUP BY ID) T2
ON (T1.ID = T2.ID);
Use left join:
select tbl1.id, tbl1.amt + coalesce(tbl2.amt, 0)
from tbl1 left join
tbl2
on tbl1.id = tbl2.id
I have two tables called warehouse table and order table.
Warehouse table (table1)
Customer orderno
AA 111
BB 222
Order table (table2)
Customer orderno status
AA 111 1
BB 222 2
CC 333 3
DD 444 4
My requirement is to show if any order is not available in table1 then it should select from table2 with status of (1,2,4)
I am trying with union but the results are not showing as expected.
select customer,orderNo from table1
union
select customer,orderno from table2
where status IN (1,2,4) ```
Can you please help me on this.
If you want orders with status of (1,2,4) then use below query
select t2.*
from table2 t2
where not exists (select 1 from table1 t1 where t1.orderno = t2.orderno)
and t2.status IN (1,2,4)
order by status;
you can union the tables, prioritize the results and then get the one with the higher priority for each order.
select Customer, orderno
from (
select Customer, orderno, ROW_NUMBER() over(partition by Customer, orderno order by Priority asc) as rn
from (
select Customer, orderno, 1 as Priority from #table1
union all
select Customer, orderno, 2 as Priority from #table2 where status IN (1,2,4)) o
) n
where n.rn = 1
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
I'd appreciate if someone could help.
I have two tables that have no relationship:
Table_1
ID NAME VALUE
1 abc 10
2 def 20
3 def 20
Table_2
ID2 NAME2 VALUE2
5 ghi 30
6 gkl 40
I want to have a select statement that would show the data from both tables like this:
ID NAME VALUE ID2 NAME2 VALUE2
1 abc 10 5 ghi 30
2 def 20 6 gkl 40
3 def 20
The point is to show data of each record in one row, the table can look like:
ID NAME VALUE ID2 NAME2 VALUE2
5 ghi 30
6 gkl 40
If Table_1 has no records. Same is true for Table_2.
I tried to use cross join, but then the data will repeat.
Thanks a lot
You need to add a join condition. In this case, by using row_number() to add a sequential number on each side. Then full outer join to get all the records:
select t1.id, t1.name, t1.value, t2.id as id2, t2.name as name2, t2.value as value2
from (select t1.*, row_number() over (order by id) as seqnum
from table_1 t1
) t1 full outer join
(select t2.*, row_number() over (order by id) as seqnum
from table_2 t2
) t2
on t1.seqnum = t2.seqnum;
Try this:
with Table_1(ID, NAME, VALUE) as (
select 1, 'abc', 10 union all
select 2, 'def', 20 union all
select 3, 'def', 20
), Table_2(ID2, NAME2, VALUE2) as (
select 5, 'ghi', 30 union all
select 6, 'gkl', 40
), prep_table_1 (ID, NAME, VALUE, rn) as (
select id, name, value, row_number() over(order by id)
from table_1
), prep_table_2 (ID2, NAME2, VALUE2, rn) as (
select id2, name2, value2, row_number() over(order by id2)
from table_2
)
select t1.ID, t1.NAME, t1.VALUE, t2.ID2, t2.NAME2, t2.VALUE2
from prep_table_1 t1
full outer join prep_table_2 t2 on t1.rn = t2.rn
SQLFiddle
This also works
select * from Table_1,Table_2
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)