I have the following sql query:
select count(tu.authorID) as c,t.tweettext,tu.name
from tweet t
inner join tweet_user tu on t.authorid=tu.authorid
where count(tu.authorID)>=5;
but it seems that inner join does not work with where in this case and I get the following error:
Error Code: 1111. Invalid use of group function 0.062 sec
Does anyone know what is my problem or how I can do that?
Use HAVING instead of WHERE for aggregate function like COUNT:
SELECT t.tweettext,tu.name, count(tu.authorID) as c
FROM tweet t inner join tweet_user tu on t.authorid=tu.authorid
HAVING count(tu.authorID)>=5
Although mySQL could do away with GROUP BY you could probably include GROUP BY also but I think you don't need the tweettext?
SELECT tu.name, count(tu.authorID) as c
FROM tweet t inner join tweet_user tu on t.authorid=tu.authorid
GROUP BY tu.name
HAVING count(tu.authorID)>=5
For group clauses you need to use
GROUP BY x
HAVING COUNT(x) > 0
http://www.w3schools.com/sql/sql_having.asp
Related
I am trying to get a field which is calculated by a subquery. I found about 400 posts at SO that state that you can use the outer ID in a select subquery clause unless you are trying to use it in a join (what I don't).
Here's my query:
SELECT (SELECT group_concat(ct.NAME)
FROM core_tagobject cto
JOIN core_tag ct ON ct.id=cto.tag_id
AND cto.context="tags_working_on"
AND cto.object_id=u.id) AS "tag_list"
FROM auth_user u
I always get back SQL error (1054) - unknown column 'u.id' in 'on clause'.
What am I doing wrong?
Thx!
I guess you want below sql
SELECT group_concat(ct.NAME) AS `tag_list`
FROM core_tagobject cto JOIN core_tag ct ON ct.id=cto.tag_id
JOIN auth_user u ON cto.object_id=u.id
WHERE cto.context="tags_working_on"
With some data examples would be better to produce a proper solution, but could you try:
SELECT tag_list.*
FROM auth_user u
INNER JOIN (
SELECT group_concat(ct.NAME)
FROM core_tagobject cto
JOIN core_tag ct ON ct.id=cto.tag_id
AND cto.context="tags_working_on"
) AS tag_list on tag_list.object_id=u.id ;
I am getting a syntax error with the code below and I can't pinpoint what's wrong.
SELECT contrat.nomPrenom
FROM contrat
WHERE Type_emploi LIKE 'Acteur'
INNER JOIN film
ON contrat.ID_film = film.ID_film
AND film.Note IN (
SELECT Note
FROM film
ORDER BY Note
DESC
LIMIT 1
)
WHERE Type_emploi LIKE 'Acteur' INNER JOIN film
Error is here: you have to use INNER JOIN before using WHERE clause.
Try inner join before the where clause
I am quite new to Redshift SQL and have been trying to figure out what's wrong with my syntax.
My task is to join 2 tables: question and user via left join as I want to retain all values from table question.
At the moment it throws the following error message: [Amazon](500310) Invalid operation: This type of correlated subquery pattern is not supported yet; when I use left join. On the other hand, when I change the code to join it works just fine. I suspect this is because I have an aggregated function and logical expression within my subquery that makes my left join an inner join.
But as I mentioned above, I need to retain all values from table question.
Below is my code
select
qa.user_id as user_email,
i.timestamp as session_login_time,
qa.timestamp as question_ask_time,
qa.question_id,
qa.question
from
schema1.question as qa
left join
schema1.user as i
on
qa.user_id = i.email
and
i.timestamp =
(select
max(timestamp)
from schema1.user
where
timestamp <= qa.timestamp)
where user_email <> 'tester' and user_email not like '%tester.com'
group by qa.user_id, i.timestamp, qa.timestamp, qa.question_id, qa.question
The purpose of the subquery is to get the closest session_login_time to each of the question_ask_time. So, multiple rows of question can have the same session_login_time value.
Could anybody please help me identify what I am missing from my code above? How do I make my left join works?
Thank you so much!
I guess that should get you the same results without involving a sub query
select
qa.user_id as user_email,
max(i.timestamp) as session_login_time,
qa.timestamp as question_ask_time,
qa.question_id,
qa.question
from schema1.question as qa
left join schema1.user as i
on qa.user_id = i.email
and i.timestamp <= qa.timestamp
where qa.user_id <> 'tester' and qa.user_id not like '%tester.com'
group by qa.user_id, qa.timestamp, qa.question_id, qa.question
I have a select statement that uses inner joins on multiple tables, and I want to get COUNT() from one particular table, however my current statement is throwing an error:
Syntax error: unexpected 'COUNT' (count)
Helpful. I know. Gotta love MySQL's detailed and in-depth error messages.
Here is my select statement:
SELECT SE.SEId, SE.ParentME, SE.ParentSE, SE.Name, SE.Status, SE.Description,
UDC.UDCId, UDC.Code, UDC.Description,
TRM.COUNT(*)
FROM SubEquipment SE
INNER JOIN UserDefinedCode UDC ON UDC.ETId = SE.EquipmentType
INNER JOIN Terminal TRM ON TRM.SEId = SE.SEId
GROUP BY TRM.SEId
WHERE ParentME = #MEId;
What am I doing wrong? Is this possible?
You want to do the following:
SELECT SE.SEId, SE.ParentME, SE.ParentSE, SE.Name, SE.Status, SE.Description,
UDC.UDCId, UDC.Code, UDC.Description,
COUNT(DISTINCT TRM.SEID)
FROM SubEquipment SE
INNER JOIN UserDefinedCode UDC ON UDC.ETId = SE.EquipmentType
INNER JOIN Terminal TRM ON TRM.SEId = SE.SEId
WHERE ParentME = #MEId
GROUP BY 1,2,3,4,5,6,7,8,9
Because Count is an aggregate your single measures must be grouped. Plus the error you're seeing is because COUNT isn't a column in TRM. That's what it thinks you're asking for.
Try COUNT(DISTINCT [the TRM primary key field(s)]); it should count the distinct terminal "id" values, so even if the intermediate JOIN multiples the rows, you'll still get the number of terminals.
In addition to FirebladeDan's answer, (as he suggested) a subquery also cleaned this issue up:
SELECT DISTINCT SE.SEId, SE.ParentME, SE.ParentSE, SE.Name, SE.Status, SE.Description,
UDC.UDCId, UDC.Code, UDC.Description,
--Subquery to get the count
(SELECT COUNT(*) FROM Terminal WHERE TRM.SEId = SE.SEId) AS TerminalCount
FROM SubEquipment SE
INNER JOIN UserDefinedCode UDC ON UDC.ETId = SE.EquipmentType
LEFT JOIN Terminal TRM ON TRM.SEId = SE.SEId
WHERE ParentME = #MEId;
This got rid of the need for grouping the columns.
Subnote: I changed the INNER JOIN on the Terminal table to a LEFT JOIN, because if a SEId did not have any associated terminals, it would not return any information, which also called for a DISTINCT query.
I have this query
Select nametwo
from cities
inner join usuarios
where cities.nametwo=usuarios.jug1
or cities.nametwo=usuarios.jug2
or cities.nametwo=usuarios.jug3
and the data is
jug1 = 2 jug2 = 1 jug3 = 4
then, the query order the data by ASC, and I want that order it by the order of the select.
is there a way for do this? thanks
Seems that you are wanting to order by the usuarios table unless I am misunderstanding.
Select nametwo
from cities
inner join usuarios
where cities.nametwo=usuarios.jug1
or cities.nametwo=usuarios.jug2
or cities.nametwo=usuarios.jug3
order by usuarios
First, if you use inner join, use an on clause. This is required in every database except MySQL and it just looks really awkward. Here is an equivalent query:
Select c.nametwo
from cities c inner join
usuarios u
on c.nametwo in (u.jug1, u.jug2, u.jug3);
This formulation actually directly suggests the answer. Use the field() function:
order by field(c.nametwo, u.jug1, u.jug2, u.jug3)