Subtracting Values from Different Tables - mysql

I have two tables:
Table A
ID Number Profile
1 100 Baker
2 75 Fields
3 100 Wayward
Table B
ID Number Tag Sender
1 50 on chris
2 50 off james
What I am try to do subtract the values from Table A and Table B who has the same ID numbers
Table C
ID Number
1 50
2 25

SELECT A.ID,A.NUMBER-B.NUMBER DIFF
FROM TABLE_A AS A
JOIN TABLE_B AS B ON A.ID=B.ID

Related

Select rows of table 1 when sum of colA grouped by ID is bigger than sum of colB in table 2 grouped by ID

We have a table of our sold items, it looks like this : ( Table A )
id
sell_id
item
amount
11
5
A
3000
12
5
B
2000
13
6
A
5120
14
7
C
5000
and a table where shipped items are placed that looks like this : ( Table B )
id
sub_id
item
amount
1
11
A
2850
2
11
A
150
3
12
B
2100
( Table B is matched to Table A by referencing TableA.id in Table B as sub_id ).
I want to find rows that sum of amount per TableA.id is not equivalent of sum of TableB.amount per TableB.sub_id.
In other words I want to know which sold items are not shipped exactly as the amount which is sold.
I've tried left joining tableA to tableB but i cannot get it to work.
Any help would be appreciated. Thanks!
For example:
SELECT a.*
FROM a
WHERE NOT EXISTS ( SELECT NULL
FROM b
WHERE a.id = b.sub_id
GROUP BY b.sub_id
HAVING a.amount = SUM(b.amount) )
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1b13c67b6e622a5da72f63074d53d423

Remove duplicates in Join statement in mysql and group

I have two tables
table1
Name marks
John 50
Smith 70
Adam 60
Roy 70
table2
Score Grade other
50 C 1.5
60 B 0.7
70 A 0.8
70 A 1.0
I want to get how many people have got A, B, C passes
I want to get an output as
Grade Count
C 1
B 1
A 2
Query I tried was
SELECT table2.Grade,
COUNT(DISTINCT table2.Grade) as count
FROM table1
LEFT JOIN table2
ON table1.Mark = table2.Score
GROUP BY table2.Grade;
But it Gives
Grade Count
C 1
B 1
A 4
So How to remove the duplicates ?
Please help.
At your second table, you got duplicate rows:
Score Grade other
50 C 1.5
60 B 0.7
70 A 0.8
70 A 1.0
Here, there are to A's, and when you joing with first table according to the field "Grade--Score", the whole join is:
Jhon 50 C
Smith 70 A
Smith 70 A --> Second A from second table
Adam 60 B
Roy 70 A
Roy 70 A --> Second A from second table
So group by and count will result 4 for the field grade here:
A 4 --> 2 Smith and 2 Roy
B 1
C 1
So, to get how many single person per grade:
select tb2.Grade GradeMark, count(*) TotalPersons
from table1 as tb1
left join (select tbi2.Score, distinct(tbi2.Grade), tbi2.other, from table2 tbi2) as tb2 on tb2.Grade = tb1.marks
group by tb2.Grade
This query will select distinct values from table2, join with table one and count the results per grade so you should get:
A 2
B 1
C 1
You don't need a JOIN for this. You can try like below using a simple group by and get the count()
select Grade, count(*) as `Count`
from table2
group by Grade;

Sum rows after comparing one row with multiple rows

I have two tables that will be a and b. I want to compare number in a to number in b where they share an ID. a has a single row, while b has multiple. If the sum of b is less than a, then that row of a should be added to the total sum, otherwise it should be skipped.
Table a
ID Number
4 50
5 60
6 70
Table b
ID Number SharedID
1 30 4
2 25 4
3 50 5
4 5 5
5 30 6
6 10 6
Using that example: b 1 and 2 are greater than a 4, so it wouldn't be counted. b 3 and 4 are less than a 5 so it would count. b 5 and 6 are less than a 6, so it would count. The total should be 130.
I'm having trouble with doing a comparison of one row to multiple and then only summing some of the numbers.
This should do the job:
select sum(Number)
from a
where Number > (select sum(Number) from b where b.SharedID = a.ID)
Try this query:
SELECT SUM(a.Number)
FROM a INNER JOIN
(
SELECT b.SharedID, SUM(b.Number) AS theSum
FROM b
GROUP BY b.SharedID
) t
ON a.ID = t.SharedID
WHERE t.theSum < a.Number
The conceptually easiest option is to create a temporary table containing the sums of Table b, and then to JOIN that back to Table a. The WHERE clause restricts your total to only a Number values which are greater than the b sum.
select SUM(number) from(
select case when(aa.number>(select SUM(bb.number)
from bb where bb.sharedid=aa.id))then aa.number else 0 end as number from aa )abc
This should do the trick

How to join two tables with one similar column into one table

I have 3 tables, A and B and C.
sample data for Table A
nid tid
101 3
101 4
101 7
103 3
103 5
104 2
104 4
104 7
sample data for Table B
tid name
2 ram
3 shyam
4 krishna
5 shiv
7 narad
What I want is, in a Third Table C
id nid labels
1 101 shyam, krishna, narad
2 103 shyam, shiv
3 104 ram, krishna, narad
I know how to do this with PHP, but is there any way to do this mysql alone?
Both tables (A and B) have thousands of records and don't have any unique column at the moment.
I tried GROUP_CONCAT but I could not construct desired output.
Edit 1 - I forgot to mention that Table C already has id and nid column inserted, while labels column is empty. So I need help in constructing some query which can update all records of Table C with labels mentioned as above.
Thanks. Regards,
This will insert the records on TableC. Since ID is an autogenerated column, you can omit this in the INSERT clause.
INSERT INTO TableC(Nid, Labels)
SELECT a.nid, GROUP_CONCAT(b.Name) Labels
FROM TableA a
INNER JOIN TableB b
ON a.tid = b.tid
GROUP BY a.nid

Access Totals Query Not Necessarily Returning First Record

I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.