find the unique record on two columns in oracle - unique

i have a table like
from to distance
Bangalore Hyderabad 1000
Hyderabad Bangalore 1000
here Hyderabad to Bangalore and Bangalore to Hyderabad both are having same distance.so i want
first record if the data comes like this...

You could just write a simple where clause which defines your need.
Eg:
select distance from TABLE_NAME where ( ("from" = 'Bangalore' and "to" = 'Hyderabad') or ("from" = 'Hyderabad' and "to" ='Bangalore') ) and rownum <= 1
http://sqlfiddle.com/#!4/0fc987/4

Related

Fetch fixed set of duplicate records from table

I want to fetch records from a table that contains duplicate records. I want the output to be like only two duplicate records from each set of duplicate records in overall record output set.
example-
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Monesy
Russia
Bhumi
India
Peter
England
Bruice
England
Radhe
India
Output should have only two duplicate set of records from all duplicate of similar type as we can see in output below the country is repeating only two times and it took only first two counters of duplicate records in final record set -
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Peter
England
You can number the lines by the window and select only the first N.
Sorting should be chosen according to the business logic of the query.
For example:
;WITH numbered_name AS
(
SELECT *
, ROW_NUMBER() OVER (PARTITION BY t.Country ORDER BY t.Name) rn
FROM table t
)
SELECT Name
, Country
FROM numbered_name
WHERE rn <= 2

how to retrieve latest data from mysql table out of duplicate records

I am trying to retrieve latest data from my sql table for each record. There will be duplicate data for each record with some data changes. I need to retrieve the latest timestamped data. Can someone suggest which is the optimum solution in terms of performance. Have seen some solutions with inner joins and sub queries.
Sample data given below
Technology Students Amount Area Date
python 500 1000 Bangalore 2021-08-06 12:03:26
Ruby 100 1000 Bangalore 2021-08-06 05:18:50
Java 300 1000 Bangalore 2021-08-06 18:23:40
python 900 1000 Bangalore 2021-08-06 16:23:30
Java 100 1000 Bangalore 2021-08-06 12:23:50
Ruby 500 1000 Bangalore 2021-08-06 15:13:40
my o/p should contain latest data for each tech
Technology Students Amount Area Date
Java 300 1000 Bangalore 2021-08-06 18:23:40
python 900 1000 Bangalore 2021-08-06 16:23:30
Ruby 500 1000 Bangalore 2021-08-06 15:13:40
One way to do this:-
*Replace Table with real table Name.
select table.*
from table
join
(
select Technology, max(Date) as max_dt
from table
group by Technology
) t
on table.Technology= t.Technology and table.Date = t.max_dt
The most performant solution where you don't need to self-join is to use a window function, optionally with a cte although a sub-query is fine also.
Unfortunately row_number() is supported only from version 8.0, however including here for completeness and to show why you should upgrade!
with latest as (
select * , Row_Number() over(partition by technology order by date desc) rn
from t
)
select Technology, Students, Amount, Area, Date
from latest
where rn=1

Get row position within a series of records in mysql

I have the following rows within a table.
MatchIDAuto CompetitionIDAuto TeamHome TeamAway MatchDate
4770 65 New Zealand South Africa 2017-02-19
4771 65 New Zealand South Africa 2017-02-21
4772 65 New Zealand South Africa 2017-02-25
4773 65 New Zealand South Africa 2017-03-01
4774 65 New Zealand South Africa 2017-03-04
What I need to be able to do is when I do the following:
select * from Match2 where MatchIDAuto=4772
Is know that it is the 3rd Match in the series. How could I dynamically calculate that with the query?
Without an ORDER BY clause there is no guarantee that row with id 4772 is the third row in the serie. That is because the database without an order by clause MAY generate different orders accordingly the Execution Plan.
So the first thing to do is to add an order by clause to your query. By the data it could be MatchIDAuto or MatchDate that I will leave by your choice. The query in MySql to get you the order number of a row will be:
select *
from (
select m.*,
#ord:=#ord+1 roworder
from Match2 m,
(select #ord:=0) t
ORDER BY MatchDate
) sub
where MatchIDAuto=4772;
This will return every column plus roworder with the order 3. On this query I choose the MatchDate field since, to me makes more sense on your data to be the column of your order by.
This technique will create a variable and sums it up to every row. You just wrap it in a subquery and query it.

Need help in mysql join query - codeigniter

i have a table in mysql with the following data
[CODE]
Table name Test
Assettype Serial_No Status location
Mouse 123456 In Stock chennai
Mouse 98765 Allocated chennai
Keyboard 23498 In Stock bangalore
Keyboard 45646 Allocated bangalore
Mouse 234234 Decommisioned hyderabad
i am looking for a mysql query which will give the below mentioned output
Assettype In Stock Allocated Decommisioned Location
Mouse 1 1 0 chennai
Keyboard 1 1 0 bangalore
Mouse 0 0 1 hyderabad
Kindly help
You can do a group by on assettype and location and find the counts like this:
select
assettype,
sum(status = 'In Stock') In_stock,
sum(status = 'Allocated') Allocated,
sum(status = 'Decommisioned') Decommisioned,
location
from test
group by assettype, location;

how to write nested query for this scenario?

cars_aircraft table
id, aircraft_name, home_base_address,alternate_base_airport
1 , MIG 20 , 77 , 80
2 , MIG 30 , 80 ,50
cars_airport table
id ,airport_city , airport_name
1 , mumbai , CST
2 , pune , PIA
i need aircraft_name,airport_name as a result for airport_city="mumbai".
Condition is that in search not only searched city's aircraft will come as
well as neighour city's aircraft should also come.
i.e neighour city of mumbai is pune(based on alternative_base_airport value of mumbai which is home_base_address of pune , so for mumbai search mumbai and pune both should come)
please help
The second table doesn't look like it's got any association with the first... but this is something you can do:
Select * from table where airport="Mumbai"
union
select * from table where home_base in (select alternative_base from table where homebase = "Mumbai")