SQL join tables with conditions to only show Max - mysql

I need to join tableA and tableB on UniqueID however I only want to join on the latest Date in tableA with certain Status restrictions (Not in S). I know I need to use the Max functions however I can't get it to work. How do I get the result table below?
I was thinking of something like:
Select
tableA.UniqueID,
MAX(tableA.Date),
tableA.Status,
tableB.Col1
From tableA
Inner join tableB on (tableB.UniqueID = tableA.UniqueID and tableA.Status = 'A')`
tableA:
| UniqueID | Date | Status |
| -------- | ------------------- |--------|
| 123 | 2015-07-05 00:00:00 | S |
| 123 | 2015-07-06 00:00:00 | S |
| 123 | 2015-07-07 00:00:00 | A |
and tableB:
| UniqueID | Col1 |
| -------- | - |
| 123 | X |
| 125 | Y |
| 126 | Z |
Result table:
| UniqueID | Date | Status | Col1|
| -------- | ------------------- |--------|---- |
| 123 | 2015-07-07 00:00:00 | A | X |

This is one of the ways -
select
t_a.*, t_b.*
from
table_b t_b
join
(
select
uniqueid, date, status,
rank() over (partition by uniqueid order by date desc ) rank_
from
table_a
) t_a on t_b.uniqueid = t_a.uniqueid
where
t_a.rank_ = 1
;
fiddle url : (https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=06660177b6c116a811218863ce428985)

Related

How Affect Group By to Other Second Join Table

I have some table like this
table request_buys
| id | invoice | user_id |
| -- | ----------------- | ------- |
| 3 | 20220405/01104298 | 1 |
table traces
| id | request_buy_id | status_id | created_at |
| -- | -------------- | --------- | ------------------- |
| 37 | 3 | 1 | 2022-03-27 14:12:25 |
| 38 | 3 | 2 | 2022-03-28 14:12:25 |
| 39 | 3 | 3 | 2022-03-29 14:12:25 |
| 40 | 3 | 4 | 2022-03-30 14:12:25 |
| 41 | 3 | 5 | 2022-03-31 14:12:25 |
| 42 | 3 | 6 | 2022-04-01 14:12:25 |
table statuses
| id | nama |
| -- | ----------------- |
| 1 | Order Placed |
| 2 | Order Paid |
| 3 | Accepted |
| 4 | Picked by Courier |
| 5 | In Transit |
| 6 | Delivered |
| 7 | Rated |
| 8 | Rejected |
| 9 | Canceled |
and then i try to design query like below
select
request_buys.invoice,
MAX(traces.id) as traces_id,
MAX(statuses.nama) as statuses_nama
from
`request_buys`
inner join `traces` on `request_buys`.`id` = `traces`.`request_buy_id`
inner join `statuses` on `traces`.`status_id` = `statuses`.`id`
where
`user_id` = 1
group by
request_buys.id
and produces output like the following
output
| invoice | traces_id | statuses_nama |
| ----------------- | --------- | ----------------- |
| 20220405/01104298 | 42 | Picked by Courier |
and the output i expect should be like in the table below
expect
| invoice | traces_id | statuses_nama |
| ----------------- | --------- | ----------------- |
| 20220405/01104298 | 42 | Delivered |
I understand my error is in MAX(statuses.nama) which I should change like removing MAX() in statuses.nama
But i just get error like this "SELECT list is not in GROUP BY clause and contains nonaggregated ... this is incompatible with sql_mode=only_full_group_by"
then I tried some to clear the value "ONLY_FULL_GROUP_BY" with a query like the following
SET sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''))
and the result is like this
output
| invoice | traces_id | statuses_nama |
| ----------------- | --------- | ----------------- |
| 20220405/01104298 | 42 | Order Placed |
and I'm really stuck at this
and how to make trace_id.status_id from the "GROUP BY" result based on request_buys.id still have a relationship with statuses.id
Your problem lies with your misuse of the MAX(statuses.nama) expression. Based on your expected output,you intend to get the statuses.nama which matches the MAX(traces.id), NOT the MAX(statuses.nama) value which returns the highest value in terms of alphabetic order. In this case, the initial letter 'P' > 'D' . I have tweaked your code a bit and tried it on workbench,supposing there are more than one invoice for a particular user.(e.g insert into request_buys values (4,'20230405/01104298',1); insert into traces values (43,4,7,'2022-04-01 14:12:25');) It works as intended.
select invoice, t.id as traces_id, s.nama as statuses_name from request_buys r
join traces t on r.id=t.request_buy_id
join statuses s on t.status_id=s.id
join
(select traces.request_buy_id, MAX(traces.id) as traces_id
from `request_buys`
inner join `traces` on `request_buys`.`id` = `traces`.`request_buy_id`
where
`user_id` = 1
group by
traces.request_buy_id ) join_t
on t.request_buy_id=join_t.request_buy_id and t.id=join_t.traces_id
;
If I'm understanding correctly, you're trying to retrieve the most recent status for each invoice. Using MAX(nama) won't return that result, because it just picks the maximum status name alphabetically.
Assuming you're using MySQL 8.x, use ROW_NUMBER() to sort and rank the statuses for each invoice, by the most recent date first. Then grab the latest one using where rowNum = 1
WITH cte AS (
SELECT rb.id AS request_buy_id
, rb.invoice
, t.id AS traces_id
, s.nama AS statuses_nama
, ROW_NUMBER() OVER(PARTITION BY rb.id ORDER BY t.created_at DESC) AS RowNum
FROM request_buys rb
INNER JOIN traces t ON rb.id = t.request_buy_id
INNER JOIN statuses s ON t.status_id = s.id
WHERE user_id = 1
)
SELECT *
FROM cte
WHERE RowNum = 1
;
Result:
request_buy_id
invoice
traces_id
statuses_nama
RowNum
3
20220405/01104298
42
Delivered
1
db<>fiddle here

Select with innerjoin and dynamic update

I created a query in my magento database to find product the have a "New From" dat but no "New To".
SELECT
catalog_product_entity.entity_id,
catalog_product_entity.sku,
a.value as New_From,
b.value as New_To
FROM `catalog_product_entity`
INNER JOIN catalog_product_entity_datetime as a on a.entity_id = catalog_product_entity.entity_id and a.attribute_id = '93'
INNER JOIN catalog_product_entity_datetime as b on b.entity_id = catalog_product_entity.entity_id and b.attribute_id = '94'
WHERE a.value IS NOT NULL AND b.value IS NULL
Now what I would like to accomplish since it concerns 500+ items to run a UPDATE statement on this query closing "New To" 1 month after having been opened.
Any ideas how to run that?
So to clarify:
Table 1: (catalog_product_entity)
| entity_id | sku |
| --------- | --- |
| 1 | ABC |
| 2 | DEF |
Table 2: (catalog_product_entity_datetime)
| id | entity_id | attribute_id | value |
| --- | --------- | ------------ | ----- |
| 1 | 1 | 93 | 2013-06-12 00:00:00 |
| 2 | 1 | 94 | 2013-07-12 00:00:00 |
| 3 | 1 | 98 | Some other attribute |
| 4 | 2 | 93 | 2014-08-20 00:00:00 |
| 5 | 2 | 94 | NULL | <- This I want updating
| 6 | 2 | 98 | Some other attribute |
I want to update the NULL values in table 2 with the date from that same table + 1 month. My search returns:
| entity -id | sku | New_From | New_To |
| ----------- | --- | ------------------- | ------------------- |
| 2 | DEF | 2014-08-20 00:00:00 | NULL |
So I would like to write the query that updates that field to the New_From + 1 month.
| entity -id | sku | New_From | New_To |
| ----------- | --- | ------------------- | ------------------- |
| 2 | DEF | 2014-08-20 00:00:00 | 2014-09-20 00:00:00 |
In my mind I will end up with something like this: (but this one does not work)
UPDATE catalog_product_entity_datetime
SET value = TIMESTAMP(a.value+300000000)
INNER JOIN catalog_product_entity_datetime as a on a.entity_id = catalog_product_entity_datetime.entity_id and a.attribute_id = '93'
WHERE attribute_id=94
Try this:
UPDATE catalog_product_entity_datetime t1
JOIN catalog_product_entity t2
ON t1.entity_id = t2.entity_id
AND t1.attribute_id = 94
JOIN catalog_product_entity_datetime t3
ON t2.entity_id = t3.entity_id
AND t3.attribute_id = 93
SET t1.value = CASE WHEN t1.value IS NULL THEN date_add(t3.value, interval 1 month)
ELSE t1.value END
;
Demo Here

Mysql count column values and merge columns

I was having problems in creating counting rows by grouping based on a given field value.
For example: I have a Table A structure like this:
+------+------------+
| id | Person |
+------+------------+
| 1 | "Sandy" |
| 2 | "Piper" |
| 3 | "Candy" |
| 4 | "Pendy" |
+------------+------+
Also I have a Table B structure like this:
+------+------------+---------+
| id | Person | Point |
+------+------------+---------+
| 1 | "Sandy" | 10 |
| 2 | "Piper" | 20 |
| 3 | "Candy" | 30 |
| 4 | "Sandy" | 10 |
| 5 | "Piper" | 20 |
| 6 | "Zafar" | 30 |
+------------+------+---------+
And needed a result like:
+------+------------+---------+
| id | Person | Point |
+------+------------+---------+
| 1 | "Piper" | 40 |
| 2 | "Candy" | 30 |
| 3 | "Zafar" | 30 |
| 4 | "Sandy" | 20 |
| 5 | "Pendy" | 0 |
+------------+------+---------+
I hope the table examples are itself self-explanatory.
SELECT person
, SUM(point) total
FROM
( SELECT person,point FROM table_b
UNION
ALL
SELECT person,0 FROM table_a
) x
GROUP
BY person
ORDER
BY total DESC;
It is a simple left join with a group by
select tableA.person, sum(tableB.points) from tableA left join tableB on tableA.person = tableB.person group by tableA.person
union
select tableB.person, sum(tableB.points) from tableB left join tableA on tableA.person = tableB.person where tableA.id is null group by tableA.person
I think below sql useful to you.
select a.id, a.Person,b.total_point from (
select id, Person from tablea) as a join
(select Person, sum(Point) as total_point from tableb group by person) as b on a.person =b.person
Thank you

How to fetch data from two tables using mysql query with some conditions applied to the second table?

I want to generate a result from a MySql query with below requirement.
Table 1 :
---------------
| nid | type |
---------------
| 1 | forum |
| 2 | forum |
| 3 | forum |
| 4 | forum |
---------------
Table 2
-----------------------
| nid | cid | created |
-----------------------
| 1 | 32 | 123456 |
| 2 | 65 | 123457 |
| 4 | 67 | 123458 |
| 1 | 61 | 123491 |
| 1 | 78 | 123497 |
| 2 | 23 | 123498 |
| 1 | 12 | 123698 |
| 4 | 54 | 132365 |
| 4 | 81 | 135698 |
| 1 | 30 | 168965 |
-----------------------
Now i require result like below. (Condition : I need the nid from first table, smallest cid for the corresponding nid in second table WHERE type = 'forum')
--------------
| nid | cid |
--------------
| 1 | 12 |
| 2 | 23 |
| 4 | 67 |
--------------
You can try this
SELECT tbl1.nid,
min(tbl2.cid) as cid
FROM table1 tbl1
INNER JOIN table2 tbl2 ON tbl1.nid=tbl2.nid
GROUP BY tbl2.nid;
SQL Fiddle
Try this
SELECT t1.nid,
min(t2.cid) as cid
FROM table1 t1
INNER JOIN table2 t2 ON t1.nid=t2.nid
GROUP BY t2.nid;
This could also work
select nid, min(cid) cid
from table2
group by nid
The above queries are have issues in group by clause. Kindly check this query.
SELECT t1.NID, MIN(t2.CID) AS cis from
TAB1 t1 inner join TAB2 t2 on t1.nid = t2.nid
group by t1.nid

MySQL: Using a subset of rows to check against where clause

I have a table something like this:
----------------------------------------
|uID|responseDate|field_01|field_02|etc|
+---+------------+--------+--------+---+
| 1 | 2011-12-02 | yes | no | |
| 2 | 2011-11-25 | no | yes | |
| 1 | 2012-01-02 | no | yes | |
| 2 | 2012-12-01 | no | no | |
| 3 | 2010-01-02 | yes | no | |
+---+------------+--------+--------+---+
I would like to get uIDs and responseDates for whom the latest response to field_01 is 'yes' - so my query should return:
------------------
|uID|responseDate|
+---+------------+
| 3 | 2010-01-02 |
+---+------------+
I'm using an inner join, but incorrectly. Here is my query:
SELECT f.uID, f.responseDate
FROM form_05 as f
INNER JOIN (
SELECT uID, max(responseDate) AS latest_date
FROM form_05
WHERE field_01 = 'yes'
GROUP BY uID ) dmax
ON dmax.uID = f.uID and dmax.latest_date = f.responseDate
ORDER BY f.uID ASC;
What this returns, however, is the latest entries for each uID where field_01 is yes, i.e.:
------------------
|uID|responseDate|
+---+------------+
| 1 | 2011-12-02 |
| 3 | 2010-01-02 |
+---+------------+
But I don't want that. I'd like to make it so that only the latest entry for each uID is eligible for the test. How can I restructure the query? Any pointers are greatly appreciated.
Give this a try and let me know if it doesn't work:
select t2.uid, t2.responseDate from t2
left join (
select t1.uid, max(t1.responseDate) as MaxDate from t1
group by t1.uid
) as SubQuery on t2.uid = SubQuery.uid and t2.responseDate = SubQuery.MaxDate
where MaxDate is not null and field_01 = "yes"