Raz Biswas Raz Biswas - 3 days ago 5
SQL Question

DISTINCT based on ID between Two table and SUM of the other exiting column in SQL

SELECT
SUM(amount), (DISTINCT table1.id)
FROM
table1
INNER JOIN
table2 ON table1.id = table2.id IS NULL;

Table1 id amount Table2 id product
1 40 5 10
2 364.25 2 20
3 704.5 8 30
4 404.5 3 40
5 580.5 2 20


The id is not unique or primary ------------------first i need to ignore all double entry ID from table2 then match id from table2 to table1 after that those ids will be match i need total of amount figure amount is a column name i will not calculate single the data will be more than 20000. please help me if you can

First compare match table2 id with table1 id if found any id match then those id amount need to be SUM i mean total. here match id is 2 and 3 according to table2 and then we will add this 2 and 3 id amount so result will be 364.25+704.5=1068.75 i am looking the result how can i do it using mysql.

I am trying to
DISTINCT
based on
ID
between two tables and
SUM
of the other existing column we have in table1. Can somebody help me how to do it?

Answer

Based on your latest comment try this

SELECT 
    SUM(amount)
FROM 
    table1
INNER JOIN 
    (Select Distinct ID From table2) T2 ON table1.id = T2.id
Comments