I'm new to Hive and I would to select only column that terminated with 'id', for example, movieid, userid etc...
I've tried: SELECT '*+(id)' FROM ratings WHERE movieid = 1; but it doesn't work.
How can I do a query like this?
Here is you can use regex to match column names you are looking, below is answer,
set hive.support.quoted.identifiers=none;
select `.*id` from ratings WHERE movieid = 1;
Related
simple search query
select * from table where id = id_1;
id_1,......................................id_2000
is there a way to search all these ids with one query or pl/sql code?
You could try:
SELECT * FROM table WHERE id IN (id_1, id_2, ... id_2000);
or if the IDs are strings:
SELECT * FROM table WHERE id IN ('id_'1, 'id_2', ... 'id_2000');
I am writing a HIVE query to pull about 2,000 unique keys from a table.
I keep getting this error - java.lang.StackOverflowError
My query is basic but looks like this:
SELECT * FROM table WHERE (Id = 1 or Id = 2 or Id = 3 Id = 4)
my WHERE clause goes all the way up to 2000 unique id's and I receive the error above. Does anyone know of a more efficient way to do this or get this query to work?
Thanks!
You may use the SPLIT and EXPLODE to convert the comma separated string to rows and then use IN or EXISTS.
using IN
SELECT * FROM yourtable t WHERE
t.ID IN
(
SELECT
explode(split('1,2,3,4,5,6,1998,1999,2000',',')) as id
) ;
Using EXISTS
SELECT * FROM yourtable t WHERE
EXISTS
(
SELECT 1 FROM (
SELECT
explode(split('1,2,3,4,5,6,1998,1999,2000',',')) as id
) s
WHERE s.id = t.id
);
Make use of the Between clause instead of specifying all unique ids:
SELECT ID FROM table WHERE ID BETWEEN 1 AND 2000 GROUP BY ID;
i you can create a table for these IDs and after use the condition of exist in the new table to get only your specific IDs
I have user tables which contains column city_id and inside this column city ids are stored as comma separated values as following:
User
user_id user_name city_id
1 ahmar_arshad 1,2,3
2 abdul_muiz 15,2,9
3 abdul_momin 1,2,13
Now I want to search rows which contains city id = 1 from city_id column.
How it can be done?
Here is a general solution, which should work on Postgres:
SELECT *
FROM yourTable
WHERE ',' || city_id || ',' LIKE '%,1,%';
Demo
The trick here is to compare the CSV list of city IDs in the form, e.g. ,1,2,3, against ,ID,, where ID can be any individual city ID.
Note that it would be best to normalize your table and store those city IDs across separate records instead of in CSV strings. This would make querying your data easier, and would probably also increase performance.
If you convert that comma separated list to an array you can use Postgres' powerful array operators:
select *
from cities
where '1' = any (string_to_array(city_id, ','));
If you need to find rows with on of several ID's
select *
from cities
where string_to_array(city_id, ',') && array['1', '2']
The && is the "overlaps" operator for arrays. If you need to find rows that contain all of a list of IDs: you can use the contains operator:
select *
from cities
where string_to_array(city_id, ',') #> array['1', '2']
Check This.
you should use string_to_array to spilt column and then use city_id='1' conditon in where clause.
select * from (
select id,user_name,unnest(string_to_array(city_id, ',')) city_id
from RT
)a where city_id='1'
Check Demo Here.
OutPut
Try this query !
SELECT *
FROM [Table Name]
WHERE city_id LIKE '1,%,%'
OR city_id LIKE '%,1,%'
OR city_id LIKE '%,%,1';
Try this One..
--**********************************************************************************
--Creating Table structure for the reference
create table #test (user_id int,user_name varchar(100),city_id varchar(25))
insert into #test values (1,'ahmar_arshad','1,2,3')
insert into #test values (1,'abdul_muiz','15,2,9')
insert into #test values (1,'abdul_momin','1,2,13')
--select * from #test
--**********************************************************************************
-- Query
select user_id,user_name,city_id from
(
select user_id,user_name,city_id,replace(city_id,',','') as City_ID2
from #test
)A
where City_ID2 like '%1%'
-- Replace comma with star(*) and then search.
--**********************************************************************************
SELECT *
FROM user
WHERE user_id LIKE '%1%';
you can search like keyword on w3school
I want to know how to do the following in SQL :
SELECT *
FROM table_A
WHERE id IN(:myValues)
AND other_colum has the same value
For example, if i've a conversation table(iduser,idconversation), I want SQL query that returns some of Ids that have the same conversation id. It should return
35;105
37;105
35;106
37;106
With 35,37 the idUsers and 105,106 the conversations they have in common.
To go further, i work with Doctrine and PostegreSQL, and the table that I want to query is generated (many to many relation) but i've difficulty to integrate sub-query.
**public function getAllCommonConversationByUserId($ids)
{
return $this->createQueryBuilder('c')
->select('c.id')
->innerJoin('c.idUser', 'recievedConversation')
->where('recievedConversation IN (:ids)')
->andWhere('$qb->expr()->eq("SELECT id FROM table GROUP BY(id) HAVING COUNT(*) >1")')
->setParameter(':ids', $ids)
->getQuery()
->getResult();
}**
Just:
SELECT *
FROM table_A
WHERE idconversation in ('105','106') and iduser in ('35','37')
UPDATE:
Are you saying if the idconversation is duplicate? (showing multiple times?)
If so:
Select *
From table
where idconversation in
(
Select idconversation
From table
group by (idconversation)
Having count(*) >1
)
--where iduser in ('35','37')
Try to do this:
select id, conversation
from [your table name]
where
conversation in (
select conversation
from [your table name]
where id in (35)
)
It return all of the participants of the conversation with user id = 35
If you have duplicates in your table, please add distinct to select statement.
You can get the conversations using group by and having:
SELECT conversationid
FROM table_A
WHERE userid in (35, 37)
GROUP BY userid
HAVING count(distinct userid) = 2;
If you want the original rows, you can join back to the original table.
For example, the table has columns MYINDEX and NAME.
MYINDEX | NAME
=================
1 | BOB
2 | BOB
3 | CHARLES
Ho do I find row with highest MYINDEX for specific NAME? E.g. I want to find ROW-2 for name "BOB".
SELECT Max(MYINDEX) FROM table WHERE NAME = [insertNameHere]
EDIT: to get the whole row:
Select * //never do this really
From Table
Where MYINDEX = (Select Max(MYINDEX) From Table Where Name = [InsertNameHere]
There are several ways to tackle this one. I'm assuming that there may be other columns that you want from the row, otherwise as others have said, simply name, MAX(my_index) ... GROUP BY name will work. Here are a couple of examples:
SELECT
MT.name,
MT.my_index
FROM
(
SELECT
name,
MAX(my_index) AS max_my_index
FROM
My_Table
GROUP BY
name
) SQ
INNER JOIN My_Table MT ON
MT.name = SQ.name AND
MT.my_index = SQ.max_my_index
Another possible solution:
SELECT
MT1.name,
MT1.my_index
FROM
My_Table MT1
WHERE
NOT EXISTS
(
SELECT *
FROM
My_Table MT2
WHERE
MT2.name = MT1.name AND
MT2.my_index > MT1.my_index
)
SELECT MAX(MYINDEX) FROM table
WHERE NAME = 'BOB'
For the whole row, do:
SELECT * FROM table
WHERE NAME = 'BOB'
AND MyIndex = (SELECT Max(MYINDEX) from table WHERE NAME = 'BOB')
If you wanted to see the highest index for name = 'Bob', use:
SELECT MAX(MYINDEX) AS [MaxIndex]
FROM myTable
WHERE Name = 'Bob'
If you want to skip the inner join, you could do:
SELECT * FROM table WHERE NAME = 'BOB' ORDER BY MYINDEX DESC LIMIT 1;
Use
FROM TABLE SELECT MAX(MYINDEX), NAME GROUP BY NAME