INSERT INTO statement with SELECT... IN error - insert

Well, i need to add a row in user_badges for each person who had correctly respond to a poll. The
"select user_id from room_poll_results........" is working fine alone, but as soon as i try to use it in my INSERT INTO statement, it gives back an error:
"[Err] 1054 - Unknown column 'user_id' in 'IN/ALL/ANY subquery'"
I don't know where it's coming from...
INSERT INTO user_badges (user_id,PPO) SELECT user_id IN
(SELECT user_id FROM room_poll_results
WHERE user_id in (select user_id from room_poll_results
where answer_text='3' AND question_id='3') AND user_id in
(select user_id from room_poll_results where answer_text='2' AND question_id='4'));

It's telling you that there's no column called user_id in room_poll_results. Change that column name (in the subselects) to whatever is the appropriate field in the table. (You'd want to post the full schema for a more specific response.)

Whenever you get errors as "[Err] 1054 - Unknown column 'user_id' in 'IN/ALL/ANY subquery'" just read it to mean that a COLUMN with the give name in parenthesis does not exist in your query.
To debug this, since you are have sub-queries, run each query independently then see their results. That will help you know what table has the missing column. Once you've got it then you can create the column and then unite your sub-queries and that's it!

I assume user_badges table has a column user_id. Your first IN should be replaced by FROM. So, I've slightly modified your query as below:
INSERT INTO user_badges (user_id, PPO)
SELECT user_id FROM
(
SELECT user_id FROM room_poll_results
WHERE user_id in
(
select user_id from room_poll_results
where answer_text='3' AND question_id='3'
) AS U
AND user_id in
(
select user_id from room_poll_results
where answer_text='2' AND question_id='4'
) AS U
) AS U;

Related

PL\SQL - Fetch values from table and Item - Oracle Apex

I need to insert data of one table to another table. All the values are from the table except one SO_ID. It is coming from the Item on the page. How do I do it?
insert into T_SORDER_ITEM_INWARD
(
select sd.ID, SO_ID
into :P25_SO_ID, sd.STOCK_ID,sd.ITEM_ID,sd.UOM_ID,
sd.ITEM_CONDITION_ID,sd.ORIGINAL,sd.ACTUAL,sd.WIDTH,sd.LENGTH,sd.STOCKQTY,
sd.KANTA,sd.RATE,sd.PACKET, sd.LABEL_METER, sd.EXCESS_SHORT,sd.LOCATION_ID,
sd.CLIENT_INITIAL, sd.FIN_YEAR, sd.SERIAL_NO
from T_STOCK_DETAIL sd join t_stock_master sm
on sd.stock_id = sm.stock_id
where sm.customer_id = p25_customer
)
A simplified example:
insert into another_table (id, name, location)
select :P25_SO_ID,
t.name,
t.location
from this_table t
Always name all columns you're inserting into (first line in my example).
Your query is impossible to understand. Not just because syntax is wrong, but because we have no idea which column is supposed to get which value.

How to fix hive code for counting a column and group by another column?

There are 3 columns in my hive data (user, gender, rating). now, I want to count number of user_id, gender wise. I have written hive code as
select user_id, gender, count(*) from u_user group by user_id;
but the error that I have got is
SemanticException [Error 10025]: Line 1:16 Expression not in GROUP BY
key 'gender'
How to fix this?
Well, the keys you group by should be the same with the keys in the select. As below:
select user_id,gender,count(1) from u_user group by user_id,gender;
And if you want to count user_id of each gender type , you can write like this:
select gender,count(distinct user_id) from u_user group by gender;

how to delete columns with less than 20 repetitions on a hive table

I am trying to learn about deleting user_id's repeated in less than 20 times in ratings table (id's with less than 20 votes mess up the prediction)
delete * FROM rating
WHERE COUNT(user_id) <20;
Below is the error I have gotten: org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: SemanticException [Error 10128]: Line 3:6 Not yet supported place for UDAF 'COUNT'"
There are two big problems
Your query is wrong. to work properly you need to use aggregation function count with groupby on user_id columns.
You can not delete records using delete statement unless you table is transactional table.
To delete the record from non-transnational table you need to use insert overwrite statement to overwrite the table with the records you want.
Syntax:
Insert overwrite table select * from <table_name> where <condition>
you code should look like this
INSERT overwrite TABLE rating
SELECT *
FROM rating
WHERE
user_id IN
(
SELECT user_id
FROM rating
GROUP BY(user_id)
HAVING count(user_id) > 20
);
If you are having transactional table then you can delete user_id having count less than 20 with the following statement.
hive> delete from rating where user_id in
(select user_id from rating group by user_id having count(user_id) < 20);

ORA-00907: missing right parenthesis when creating a table

I have the below query that works fine and produces the correct result
select id, sum(item_stock)
from seller
group by id
order by id ASC;
When I try to create a table with the query above like this
CREATE TABLE total_stock
AS (
select id, sum(item_stock)
from seller
group by id
order by id ASC );
I get the following error
SQL Error: ORA-00907: missing right parenthesis
Any help on why this isn't working would be greatly appreciated
Your problem is caused by the clause ORDER BY.
You have to:
Add an alias to your "sum" field
Create another subquery in order to "remove" the ORDER BY clause
CREATE TABLE total_stock
AS (
select id, item_stock
from (
select id, sum(item_stock) as item_stock
from seller
group by id
order by id ASC
)
)

sql statement to select and insert one value from one table rest from outside the tables

I have two db2 tables, table one contains rows of constant data with id as one of the columns. second table contains id column which is foreign key to id column in first table and has 3 more varchar columns.
I am trying to insert rows into second table, where entry into id col is based on a where clause and the remaining columns get values from outside.
table1 has columns id, t1c1, t1c2, t1c3
table2 has columns id, t2c1, t2c2, t2c3
to insert into table2, I am trying this query:
insert into table2 values
(select id from table1 where t1c2 like 'xxx', 'abc1','abc2');
I know it is something basic I am missing here.
Please help correcting this query.

Resources