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
The set of three pairs { (x1,y1),(x2,y2),(x3,y3)} are said to be transitive if y1= x2 and x3 =x1 and y3 = y2.
Write a query to output all such transitive sets.
I see this as a series of joins. You can use row_number() to generate primary keys. Your requirement translates as:
with data as (select t.*, row_number() over(order by null) rn from mytable t)
select
from data d1
inner join data d2
on d2.x = d1.y
and d2.rn <> d1.rn
inner join data d2
on d3.x = d1.x
and d3.y = d2.y
and d3.rn <> d2.rn
and d3.rn <> d1.rn
Related
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)
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
Is this SQL an atomic operation for MySQL ?
UPDATE
api_report a
JOIN
(
SELECT
id
FROM
api_report
WHERE
api_report.success_nums = 7878
) b
ON a.id = b.id
SET
a.success_nums = 4646;
Assume that I have 2 threads to execute the upper SQL, the data will be updated by 1 of the 2 thread? or by the both 2 thread concurrently?
I wrote a demo, and it seems the data is updated by only 1 of the 2 thread.
Use correct multiple-table syntax:
UPDATE api_report a
JOIN api_report b ON a.id = b.id
SET a.success_nums = 4646
WHERE b.success_nums = 7878
Attention! this query will update ALL matched rows including one which has success_nums = 7878. fiddle. If you does not need this then add more conditions to WHERE clause, for example, AND a.success_nums != 7878.
This SQL seems to be an atomic operation for MySQL.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a query where i did a join for getting two table information, All i need to do is unique the tt_code.
My SQL
SELECT cd.`tt_code`, ri.`complain_code`, ri.`repair_time`
FROM `complain_details`cd
JOIN `repair_info` ri ON `cd`.`complain_code` =`ri`.`complain_code`
WHERE `cd`.`stat` = 'n' AND `ri`.`stat` = 'n'
AND (`cd`.`ass_to_per` = 'murad.hasan' OR `ri`.`ass_to_per` = 'murad.hasan')
My current Output
Desire output is unique tt_code rows, using SQL only. I know about DISTINCT but i am unable to achieve it.
You can use GROUP BY.
SELECT cd.`tt_code`, ri.`complain_code`, ri.`repair_time`
FROM `complain_details`cd
JOIN `repair_info` ri ON `cd`.`complain_code` =`ri`.`complain_code`
WHERE `cd`.`stat` = 'n' AND `ri`.`stat` = 'n'
AND (`cd`.`ass_to_per` = 'murad.hasan' OR `ri`.`ass_to_per` = 'murad.hasan')
GROUP BY cd.`tt_code`;
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 8 years ago.
Improve this question
I have two tables. Table A and Table B. How can I compare a column in A (data type: date)
with a column in B (data type: date) with condition: column in B >= column in A?
There are two things being asked here.
1) Show me those that are greater
2) Tell me the comparison
So, to do both, try this. Part (1) is in the where clause, and part (2) is in the select statement.
select *, datediff(day, b.col, a.col) as ColumnDifference
from TableA a, TableB b
where b.col >= a.col
Documentation for datediff: http://msdn.microsoft.com/en-us/library/ms189794(v=sql.100).aspx