how to create a table like this by mysql - mysql

I have a table like this:
ID Payment year
A 10 1
A 15 2
A 12 3
B 11 2
B 15 4
C 25 1
C 17 3
I'm looking for a query that returns row for each ID for the its last year. The year column is ordered increasing for each ID.
I need a result like this:
ID Payment year
A 12 3
B 15 4
C 17 3
I have this query so far:
select ID, Payment, Year from payment_table
where year = (select max(year) from ?????????);
I don't know what shall I write instead of ????????
It would be appreciated If anybody gives me some idea.

Use subquery :
select t.*
from table t
where year = (select max(t1.year) from table t1 where t1.id = t.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

sql select max for each grouped column and rest of row

I have a table in mysql
name year data
a 1 1
b 1 2
c 2 3
a 2 4
c 3 5
For each year I need the max(data), year, and name associated with that max.
ive tried
select max(data), name , year from table group by year; however I do not have access to name.
Thank you in advance.
I think you can try something like below
select name, data, year from table A
join (select max(data) data, year from table a
group by year) B on A.data = B.data and A.year = B.year

SQL query joining and grouping table

Select table1.id, (table1.total_item-table2.requested) as items_left
from table1, table2
where table2.year = 2015
group by table1.id
i am using SQL Server 2008..
Whats wrong with my code? it cannot be group in id number,
i want to sum all the individual table1.items in table1, and sum all the individual table2.requested in table 2 to
subtract the remaining items as I name it items_left, and group it by table1.id
something like this...
0. id items_left year
1. 1 3 2015
2. 5 10 2015
3. 3 4 2015
this is the output of that code above...
there is a duplication of id and I cant group it to
0. id items_left
1. 1 1
2. 1 1
3. 1 2
4. 5 5
5. 3 2
6. 5 5
7. 3 2
i want an output like this.. please sir/ma'am help me..
something like this...
0. id items_left year
1. 1 3 2015
2. 5 10 2015
3. 3 4 2015
You are missing a "sum()" for the second field; also there is missing joining condition:
SELECT tab1.id, sum(tab1.total_item - ifnull(tab2.requested,0)) AS items_left
FROM tab1
LEFT JOIN tab2 ON tab1.id = tab2.??
WHERE tab2.year = 2015
GROUP BY tab1.id

Joining Summary Tables in MYSQL

I have two tables that I would like to join in MYSQL and I'm looking for the most optimized way to do this. Here's the problem:
I want to count the number of records based on a field (call it customer) in each table then join the results together (using customer) to produce a summary table. Note that all customers must be returned even if one table does not include a customer
TABLE A
Customer
--------
1
1
4
4
5
6
and
TABLE B
Customer
--------
4
5
5
5
6
6
7
7
7
into a summary table
SUMMARY
Customer CountA CountB
-----------------------
1 2 0
4 2 1
5 1 3
6 1 2
7 0 3
Any ideas on how to do something like this?
SELECT customer,SUM(source = 'a') cnta, SUM(source = 'b') cntb FROM
(
SELECT 'a' source,customer FROM customer_a
UNION ALL
SELECT 'b',customer FROM customer_b
) n
GROUP
BY customer;

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.