How to get max value from 2 tables - sql

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)

Related

SQL Server Sum Distinct Group by

For example this table, I need to sum this grouped by id and date with distinct
id amt date
1 100 2018/06/01
1 120 2018/06/02
1 100 2018/06/03
1 100 2018/06/03
1 100 2018/06/03
2 100 2018/06/01
2 100 2018/06/01
2 100 2018/06/01
2 130 2018/06/02
2 130 2018/06/02
2 130 2018/06/02
2 130 2018/06/02
2 100 2018/06/03
First i tried
SELECT SUM(DISTINCT amt) GROUP BY id
But the result are wrong, it's removing duplicate for example on id 1, instead of 320 it only results 220 because it remove the duplicated amt 100.
So I tried
SELECT SUM(DISTINCT amt) GROUP BY id, date
But I can't sum it.
Edit: Sorry i forgot to say the result should be
id amt
1 320
2 330
With a long version, but easy to understand query. The below query using CTE should help you
with records as
(select distinct id, date, amt from table_name)
select sum (amt), id
from records
group by
id;
tried with below.
SELECT id,SUM(DISTINCT amt) as amt,date #tmp from [yourTableName] GROUP BY id, date
select id,amt from #tmp
Try this:
select id, SUM(amt) from (
select id, SUM(distinct amt) amt, [date] from #tbl
group by id, [date]
) a group by id
First you need to group distinct amt grouped by id and date. Next, you have to to group the result by id, summing partially summed amt column (in first step we summed only distinct values from particular days).
Try this...
SELECT id,
Sum(amt) AS amt
FROM (SELECT DISTINCT *
FROM mytable) tbl
GROUP BY id
Output
+----+-----+
| id | amt |
+----+-----+
| 1 | 320 |
| 2 | 330 |
+----+-----+
SQL FIDDLE: http://sqlfiddle.com/#!18/2d356/14/0
The below query using CTE and Row_number should help you
with cte
as
(
select 1 id, 100 amt,cast('2018/06/01' as date) date
union all
select 1 id, 120 amt,cast('2018/06/02' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 100 amt,cast('2018/06/03' as date) date
)
select id,sum(amt) as sum1 from
(
select *,ROW_NUMBER() over(partition by id,date order by id,date) s1 from cte
) b
where s1=1
group by id

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

Add values from count that have come from a UNION

I am attempting to add together the values_occurrence for the id's that match. For example ID number 1 has 3 and another record shows ID number 1 having 4. I want to ADD 3 + 4 based on the ID matching.
I am trying to see which ID has the most entries in each table and then add them together.
ID value_occurrence
--------------------
1 3
1 4
so far this is what I have.
SELECT ID, COUNT(ID) AS value_occurrence
FROM TABLE1
GROUP BY ID
UNION
SELECT ID, COUNT(ID) AS value_occurrence
FROM TABLE2
GROUP BY ID
ORDER BY ID ASC;
Any help would be appreciated.
Do a union all and then aggregate:
select id, count(*) as total_cnt, sum(t1) as t1_cnt, sum(t2) as t2_cnt
from ((select id, 1 as t1, 0 as t2 from table1) union all
(select id, 0, 1 from table2)
) t
group by id
order by id;

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

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

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