Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to retrieve some data in a table with certain criteria.
With the following criteria:
JurPengampu = 17
NRP codes must be different from one another, if the same, then the IdJurus = 17 is taken
The explanation is in the picture.
Thank you for help,,
enter image description here
Use analytical function as follows:
select * from
(select t.*,
row_number() over (partition by kodenrp order by idjurusan) as rn
from your_table t) t
where rn = 1
Or use NOT EXISTS as follows:
select t.*
from your_table t
where not exists
(select 1 from your_Table tt
where t.kodenrp = tt.kodenrp and tt.idjurusan < t.idjhurusan)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
For example, I have n rows, I need to update from the 4th row to the nth row. here n means the last row of the query. I know multi-row updates can crash applications but in my case, I am sure that I will have a max of 40 rows for 1 user.
Please check this query for user wise update where update records start from 4 and onwards. Enable WHERE clause for specific userid otherwise disable it.
-- MySQL (v5.8)
UPDATE test tt
INNER JOIN (SELECT id, userid
, ROW_NUMBER() OVER (PARTITION BY userid order by id) row_num
FROM test
-- WHERE userid = 1
) t
ON tt.id = t.id
AND t.row_num >= 4
SET tt.amount = tt.amount + 10;
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=e0cda11163a44f7a2b82e31c6d13ed8d
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
A table has fields
code
batch
I want to know what code(s) are in batch 1 but not in batch 2.
What is the best SQL statement for this?
There are multiple options as follows:
Using NOT EXISTS:
SELECT DISTINCT T.CODE FROM YOUR_TABLE T
WHERE T.BATCH = 'BATCH1'
AND NOT EXISTS (SELECT 1 FROM YOUR_tABLE TT
WHERE TT.CODE = T.CODE
AND TT.BATCH = 'BATCH2');
Using LEFT JOIN:
SELECT DISTINCT B1.CODE
FROM YOUR_tABLE B1 LEFT JOIN YOUR_tABLE B2
ON B1.CODE = B2.CODE AND B2.BATCH = 'BATCH2'
WHERE B1.BATCH = 'BATCH1' AND B2.CODE IS NULL;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
As the title suggests, trying to take the count or possibly distinct count of a column, we call it year, to count the number of a years for an individual or populated ID and place it another column in the same table. Here is an idea of what I have so far and we want to update the table. Thank you.
Join group table to updated table directly:
UPDATE outputtable O JOIN
(SELECT personID, COUNT(DISTINCT year) AS countYear
FROM outputtable GROUP BY personID) temp ON O.personID=temp.personID
SET O.N=temp.countYear
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to do this task in sql. but I am not able to derive a logic that helps getting me this output.
It looks like you want to bring the name of the player whose code is "x" for each id. If so, one option uses window functions:
select id, playercode, playername,
max(case when playercode = 'x' then playername end) over(partition by id) requiredoutput
from mytable
If your database does not support window functions, one option uses a correlated subquery. Assuming no duplicates on (id, playercode):
select id, playercode, playername,
(
select t1.playername
from mytable t1
where t1.id = t.id and t1.playercode = 'x'
) requiredoutput
from mytable t
If you're looking for an update statement maybe something like this would work
with upd_cte as (
select distinct id, player_name
from tTable
where Playercode='x')
update t
set requiredoutput=u.player_name
from tTable t
join upd_cte u on t.id=u.id;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to display result having data categorized in the group with their latest message according to date.
I tried grouping and having along with nested queries but no luck.
SELECT groupName,date FROM chat where groupName like '%he%' group by groupName,date having min(date);
I want the two rows to be printed along having the latest message with them.Here it would be row 4 and row 5. this is just an arbitrary data though which I need to impplement on bulk.
You don't need group by, just filtering:
select c.*
from chat c
where c.date = (select max(c2.date) from chat c2 where c2.groupname = c.groupname);
You can do it with NOT EXISTS:
SELECT c.*
FROM chat c
WHERE
groupName LIKE '%he%'
AND
NOT EXISTS (
SELECT 1 FROM chat
WHERE groupname = c.groupname AND date > c.date
)