SQL Query, get a columns if another columns equal to x - mysql

Lets say I have a table that is like this
x y
10 5
10 8
10 12
11 9
11 14
11 12
14 12
14 5
14 11
I need to return all the x group that has the same value if y = 5
So I would need a query that would return me the x group that has the value 10 or 14.
Query:
select x, y from table ...
Should return me something like this :
x y
10 5
10 8
10 12
14 12
14 5
14 11

select x, y
from your_table
where x in
(
select distinct x
from your_table
where y = 5
)

SELECT *
FROM tableName
WHERE x in
(
SELECT DISTINCT x
FROM tableName
WHERE y = 5
)
SQLFiddle Demo
or a join can also solve it
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT DISTINCT x
FROM tableName
WHERE y = 5
) b ON a.x = b.x
SQLFiddle Demo

Related

how to select all the values in hive with distinct of 2 columns in hive

I have a hive table that looks like this (total 460 columns)
colA colB ....... ce_id filename ......... dt
v j 4 gg 40
v j 5 gg 55
f r 4 gg 40
t y 7 yy 32
a e 5 ee 43
now i need to write a query that selects all the data using distinct of 2 colunms with ce_id and filename
my expected output
colA colB ....... ce_id filename ......... dt
v j 4 gg 40
v j 5 gg 55
t y 7 yy 32
a e 5 ee 43
anyone can guide me how to select all the data with 2 distinct value
I think row_number() does what you want:
select t.*
from (select t.*,
row_number() over (partition by ce_id, filename order by dt) as seqnum
from t
) t
where seqnum = 1;
You don't specify which row you want. The above formulation returns the one with the smallest value of dt. The order by controls the "which".

MySQL select a certain amout of rows for each type in a certain c

I would like to select the first certain number of rows, by groups of a certain column. For example :
Original data:
index type value
0 1 a 0.716430
1 2 a 0.223650
2 3 a 0.375417
3 4 a 0.773874
4 5 a 0.802127
5 6 a 0.956563
6 7 b 0.377718
7 8 b 0.487772
8 9 b 0.672767
9 10 b 0.275895
10 11 b 0.981751
11 12 b 0.914780
12 13 b 0.940582
13 14 c 0.347563
14 15 c 0.101106
15 16 c 0.390205
16 17 c 0.235941
17 18 c 0.593234
18 19 c 0.904659
I would like to select the first 4 rows for each unique value of type, and the order is by index.
So the ideal result would be:
index type value
0 1.0 a 0.716430
1 2.0 a 0.223650
2 3.0 a 0.375417
3 4.0 a 0.773874
4 7.0 b 0.377718
5 8.0 b 0.487772
6 9.0 b 0.672767
7 10.0 b 0.275895
8 14.0 c 0.347563
9 15.0 c 0.101106
10 16.0 c 0.390205
11 17.0 c 0.235941
row_number() is the typical solution to this:
select t.*
from (select t.*,
row_number() over (partition by type order by index) as seqnum
from t
) t
where seqnum <= 4;
In older versions of MySQL, you can do:
select tm.*
from telegram_message tm
where tm.index <= coalesce( (select tm2.index
from telegram_message tm2
where tm2.type = tm.type
order by tm2.index asc
limit 1 offset 3
), tm.index
);
The coalesce() is so all rows are taken if there are not 4 rows for the type.
You can get the result you want by self joining your table on index, where the value of index in the joined table is less than that in the first, and selecting only those rows which have < 4 rows with lower index values:
SELECT t1.id, t1.index, t1.type, t1.value
FROM test t1
LEFT JOIN test t2 ON t2.index < t1.index AND t2.type = t1.type
GROUP BY t1.id, t1.index, t1.type, t1.value
HAVING COUNT(t2.index) < 4
Output:
id index type value
0 1 a 0.71643
1 2 a 0.22365
2 3 a 0.375417
3 4 a 0.773874
6 7 b 0.377718
7 8 b 0.487772
8 9 b 0.672767
9 10 b 0.275895
13 14 c 0.347563
14 15 c 0.101106
15 16 c 0.390205
16 17 c 0.235941
Demo on dbfiddle

MySQL Query keep numbers which do not fall between a range

Could anyone point me in the correct direction on how to do this SQL query? I have two tables coord_table and rm_table. I would like to perform a query where any coord_table.loc which falls between any rm_table.start_loc or rm_table.end_loc is removed from the result of the query.
coord_table
coord_id loc
____________________
1 9
2 19
3 30
4 55
rm_table
rast_id start_loc end_loc
___________________________
1 10 20
2 50 60
query_result
coord_id loc
_____________
1 9
3 30
EDIT: This should work. It uses the BETWEEN syntax:
SELECT a.* FROM coords_table a
LEFT JOIN
(
SELECT
*
FROM
(
SELECT
coords_id, loc, start_loc, end_loc,
(loc BETWEEN start_loc AND end_loc) as is_between
FROM
coords_table, rm_table
) a
WHERE a.is_between = 1
) b
ON a.coords_id = b.coords_id
WHERE b.coords_id IS NULL
Fiddle: http://sqlfiddle.com/#!9/3acdb/2

Put all rows from one table to another

how I can put all the rows from table A to table B witch all table have the same columns names
ex :
table A :
x y z
1 2 3
4 5 6
7 8 9
table B:
x y z
10 11 12
table C should be like this :
x y z
1 2 3
4 5 6
7 8 9
10 11 12
PS : I am using a query , I do'nt want to insert in a real table
If you only want to select data from A and B, the UNION works:
SELECT x, y, z FROM A
UNION SELECT x, y, z FROM B
If you want to insert data from A to B (which contradicts your P.S. but corresponds to your first sentence) then this is your solution:
INSERT into B SELECT * from A

Force group by to keep the original order of the rows

I have a result of a query like this:
24
1
24
11
11
11
13
24
24
11
24
11
24
11
11
13
24
24
11
11
13
21
21
11
11
11
11
11
7
11
11
I need to group this result, but if I simply group by this id mysql also orders it like:
1
2
3
4
What I want is:
24
1
11
13
21
.
.
.
Would it be possible?
The complete query is this one, basically it calculates the closest routes that have a business inside based on given coordinates:
select * from (select dr.* from drivers_route dr inner join businesses_drivers_route bdr on dr.id_drivers_route = bdr.fk_id_drivers_route inner join (select * from (select b.idbusiness,
b.trading_as,
111.1111 *
DEGREES(ACOS(COS(RADIANS(53.33841959999999)) * COS(RADIANS(b.latitude))
* COS(RADIANS(-6.2876401 - b.longitude))
+ SIN(RADIANS(53.33841959999999))
* SIN(RADIANS(b.latitude)))) AS distance_in_km from business b inner join business_category bc on bc.fk_idbusiness = b.idbusiness where b.status_reg = 'A' and bc.status_reg = 'A' and bc.fk_idcategory=44) as b_dist order by b_dist.distance_in_km) as b_ord on b_ord.idbusiness = bdr.fk_id_bus_destination) as r_ord;
You can do this by adding counter in the query like this:
SET #c=0;
SELECT my_field,(#c := #c +1) as c FROM my_table group by my_field order by c;
But as Strawberry said, it is good to have a unique primary key so you can use it instead.