I have this statement:
SELECT id, name
FROM players
WHERE this = 1
AND name NOT IN (SELECT name
FROM players_online
WHERE this = 'that')
ORDER BY RAND()
LIMIT 3, 6
All columns are correctly named. No SQL errors occur, it just returns no results.
From what I've read in documentation it should work, but doesn't.
Any idea?
Don't use NOT IN with subqueries. It has the wrong semantics if any value in the subquery is NULL. In that case, the NOT IN never evaluates to TRUE, so no rows are returned at all.
Instead, use NOT EXISTS:
SELECT p.id, p.name
FROM players p
WHERE p.this = 1 AND
NOT EXISTS (SELECT 1
FROM players_online po
WHERE po.name = p.name AND po.this = 'that'
)
ORDER BY RAND()
LIMIT 3, 6;
Related
I am trying to filter results based on the name assigned on count() and get this:
Unknown column 'total_submissions' in 'where clause'
SELECT SQL_CALC_FOUND_ROWS patient.*,count(patient_data.id) as total_submissions
FROM patient
LEFT JOIN patient_data ON (patient_data.patient_id = patient.id AND patient_data.date_finished IS NOT NULL)
WHERE patient.doc_id = 2 AND total_submissions = 5
GROUP BY patient.id
ORDER BY patient.id DESC
LIMIT 0,40
After more research I did find out about not being able to use a column alias in the WHERE but I am unsure how to execute this query then. I assume it's possible but how would I be able to filter the results based on the count() calculation of the query?
total_submissions is a column alias and the result of an aggregation function. You need to do that check in a havingclause:
SELECT SQL_CALC_FOUND_ROWS p.*, count(pd.id) as total_submissions
FROM patient p LEFT JOIN
patient_data pd
ON pd.patient_id = p.id AND pd.date_finished IS NOT NULL
WHERE p.doc_id = 2
GROUP BY p.id
HAVING total_submissions = 5
ORDER BY p.id DESC
LIMIT 0, 40;
Notes:
Table aliases make the query easier to write and to read.
The condition on doc_id should still be in the WHERE clause.
You can't use column alias in where clause because the precedence in sql evaluation don't let the db engine know the alias name when evaluate the where clause
First is evaluated the FROM clase then the WHERE clause and after the SELECT cluase ..
In your case you have an aggregation function related to yu alias and this can be evaluated only after the group by is performed, pratically at the end of query process
for this reason there is a proper filter based on HAVING that work on the result of the aggreated query
SELECT SQL_CALC_FOUND_ROWS patient.*, count(patient_data.id) as total_submissions
FROM patient
LEFT JOIN patient_data ON (patient_data.patient_id = patient.id AND patient_data.date_finished IS NOT NULL)
WHERE patient.doc_id = 2
GROUP BY patient.id
HAVING total_submissions = 0
ORDER BY patient.id DESC
LIMIT 0,40
select d.order_type from migu_td_aaa_order_log_d d where exists(select 1
from migu_user r where r.user_id = '156210106' and r.user_num =
d.serv_number) and d.product_id in ('2028594290','2028596512','2028597138' )
order by d.opr_time desc limit 1
why the above sql failed ,indicates :
FAILED: SemanticException [Error 10002]: Line 4:11 Invalid column reference 'opr_time'
but the below one works :
select temp.order_type from (
select d.* from migu_td_aaa_order_log_d d where exists(select 1 from
migu_user r where r.user_id = '156210106' and r.user_num = d.serv_number)
and d.product_id in ('2028594290','2028596512','2028597138' ) order by
d.opr_time desc limit 1) temp;
this one works fine ,too ,and much more efficient than the second one:
select d.* from migu_td_aaa_order_log_d d where exists(select 1 from
migu_user r where r.user_id = '156210106' and r.user_num = d.serv_number)
and d.product_id in ('2028594290','2028596512','2028597138' )
order by d.opr_time desc limit 1
I only need to get order_type field,so even though the second one works,but it cost much more time.
Can anyone help me?
Thanks a lot!
Your first query does not work because, in the first select statement, you are just getting one column (d.order_type), but you are trying to order by another column (d.opr_time), which you have not included in your select statement
select d.order_type from ...
...
order by d.opr_time desc limit 1
Note that if you added the column d.opr_time to your first query, it would work:
select d.order_type, d.opr_time from ...
...
order by d.opr_time desc limit 1
Your second query works because, in the subquery, you have selected all the columns of d (d.*), so when you order by opr_time, that column is present. (Same for the third query).
select temp.order_type from (
select d.* ... order by d.opr_time ...
EDITED:
According to the Hive documentation:
When using group by clause, the select statement can only include
columns included in the group by clause. Of course, you can have as
many aggregation functions (e.g. count) in the select statement as
well.
So, this query:
select d.order_type, d.opr_time from ...
...
order by d.opr_time desc limit 1
Shouldn't work either, because the select clause has an additional column (d.order_type) that is not included in the group by clause.
I hope this helps.
P.S. This answer about SQL execution order might be useful.
1.
Hive currently have an order by limitation.
The current status of this issue is PATCH AVAILABLE.
see -
"Can't order by an unselected column"
https://issues.apache.org/jira/browse/HIVE-15160
2.
You might want to get familiar with LEFT SEMI JOIN which is a cleaner syntax for EXISTS
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Joins#LanguageManualJoins-JoinSyntax
3.
using min / max over a struct / named_struct can be used instead of order by ... asc / desc and limit 1
Here is an alternative solution:
select max(named_struct('opr_time',d.opr_time,'order_type',d.order_type)).order_type
from migu_td_aaa_order_log_d d
left semi join migu_user r
on r.user_num =
d.serv_number
and r.user_id = '156210106'
where d.product_id in ('2028594290','2028596512','2028597138')
;
P.s.
You seriously want to consider to treat IDs (user_id, product_id) as numeric and not as strings.
I receive an error when i execute this query:
SELECT
(SELECT count(cp_projeto_view.id) FROM cp_projeto_view WHERE cp_projeto_view.id_projeto = cp_projeto.id AND cp_projeto_view.id_pessoa = 467 LIMIT 1) AS qtde_visualizacoes
FROM cp_projeto
WHERE qtde_visualizacoes = 0
The error is: #1054 - Unknown column 'qtde_visualizacoes' in 'where clause'
Why qtde_visualizacoes does not exists?
Thank you very much!
You cannot use a column alias in a where clause (unless you use a subquery). MySQL has an extension where you can use having instead:
SELECT (SELECT count(cp_projeto_view.id)
FROM cp_projeto_view
WHERE cp_projeto_view.id_projeto = cp_projeto.id AND cp_projeto_view.id_pessoa = 467
LIMIT 1
) AS qtde_visualizacoes
FROM cp_projeto
HAVING qtde_visualizacoes = 0
EDIT:
The query that you probably want is more like:
select p.*
from cp_projecto p
where not exists (select 1 from cp_projeto_view pv where pv.id_projeto = p.id and pv.id_pessoa = 467)
This will return all cp_projectos that have no matching rows in cp_projeto_view. Your original query would only return rows with a single column of 0s, which doesn't make much sense. If you want the count, then do:
select count(*) as cnt
from cp_projecto p
where not exists (select 1 from cp_projeto_view pv where pv.id_projeto = p.id and pv.id_pessoa = 467)
And, for performance, create an index on cp_projeto_view(projeto, id_pessoa).
Hello I write this sql query to update a table. The query is:
Update Product Set MakeId = (Select Distinct v.Id from VehicleCompany v, Car_Window c, product p Where p.ProductSKU = c.mf_part_no
AND c.make = v.Name
Group By v.Id)
But whenever I run this query it gives the following error:
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1
value. This is not permitted when the subquery follows =, !=, <, <= ,
, >= or when the subquery is used as an expression.
May I know Where I'm making the mistake?
Thanks in Advance
Your Select Distinct can return multiple values. To assign a value to MakeId your subquery must return only 1 value.
I am not sure whether that's what you want. You could write (Select Top 1 ...)
Run
Select Distinct v.Id
from VehicleCompany v, Car_Window c, product p
Where p.ProductSKU = c.mf_part_no
AND c.make = v.Name
You should see it is bring back more than 1 row and you will need to fix this part of the query
If you are confident the ID will always be the same changing to this will fix it for you:
Select top 1 Distinct v.Id from VehicleCompany v, Car_Window c, product p
Where p.ProductSKU = c.mf_part_no
AND c.make = v.Name
select count(distinct(vw_SIPMIP.product_id)) from vw_SIPMIP , sp_mip_rule
where
vw_SIPMIP.product_id not in (select a.product_id from vw_non_SIPMIP a)
and sp_mip_rule.id = vw_SIPMIP.id
and sp_mip_rule.createdby != '_IMPORT' limit 1
I am keep getting this error
your syntax is incorrect on the distinct... By using the (parens), it is thinking Distinct is a function and expecting the inner value as a parameter to pass and get a value back... what you want is... Additionally, since you have no other columns you are returning, you don't need a limit one... COUNT(*) or COUNT(DISTINCT SomeColumn) will ALWAYS return a single row all by itself... no group by needed.
select count(distinct vw_SIPMIP.product_id) YourDistinctCount
from vw_SIPMIP,
sp_mip_rule
where vw_SIPMIP.product_id not in (select a.product_id from vw_non_SIPMIP a)
and sp_mip_rule.id = vw_SIPMIP.id
and sp_mip_rule.createdby != '_IMPORT'