Question

I have somehow complicated query as follow:

 SELECT w,e2.EntityID
 FROM (SELECT EntityID,SUM(frequency) w
FROM omid.entity_epoch_data where EpochID in 
( select id from epoch where startDateTime>='2013-11-01 00:00:00' and 
startDateTime <= '2013-11-30 00:00:00') 
      GROUP BY EntityID) e1 
    RIGHT JOIN
    entity e2 ON e1.EntityID = e2.EntityID order by w desc

And it works properly but as soon as I add another innerjoin:

SELECT w,e2.EntityID
FROM (SELECT EntityID,SUM(frequency) w
FROM omid.entity_epoch_data inner join omid.entity_dataitem_relation as e3            on(e1.EntityID = e3.EntityID) 
where e3.dataitemtype=3  and EpochID in 
 ( select id from epoch where startDateTime>='2013-11-01 00:00:00' and 
 startDateTime <= '2013-11-30 00:00:00') 
      GROUP BY EntityID) e1 
    RIGHT JOIN
    entity e2 ON e1.EntityID = e2.EntityID order by w desc

I get the following error:

column 'EntityID' in the field set is ambiguous

Does anyone have an idea where my mistake is?

Update :

I have the right version as follow (it gives me exactly what I want)

SELECT ID,e2.EntityID,e2.Name,AVG(intensity),AVG(quality),SUM(frequency) 
FROM omid.entity_epoch_data as e1
right JOIN entity AS e2 ON (e1.EntityID = e2.EntityID)
where EpochID in 
( select id from epoch where startDateTime>='2013-11-01 00:00:00' and 
startDateTime <= '2013-11-30 00:00:00') and e1.EntityID in 
(SELECT entityid FROM omid.entity_dataitem_relation where dataitemtype=3)
 group by e2.EntityID order by sum(frequency) desc;

But it takes time and I need to change the

(SELECT entityid FROM omid.entity_dataitem_relation where dataitemtype=3)
 group by e2.EntityID order by sum(frequency) desc; 

to innerjoin can anyone help me how to do that?

Image:

enter image description here

enter image description here

Was it helpful?

Solution

You have two problems.

First off, the syntax for that first INNER JOIN is not correct. You are conflating the INNER JOIN and ON parts of the statement. (At least, I think this is the case, unless you actually have "." characters in your table names are really want a cartesian result from this JOIN)>

Secondly, the second usage of EntityID (in that new INNER JOIN you added) could refer to the EntityID value from either the left or right side of the INNER JOIN -- that's what the "ambiguous column" in the error means.

Here is some SQL to get you started. It follows the selection logic you give in the comment, below. It names each table with an alias and ensures that each column specifies the table from which it should be drawn. I don't have an instance of MySQL handy to try it, but it should get you started.

 SELECT E.EntityID, SUM(EED.Frequency)
     FROM entity_epoch_data EED INNER JOIN entity_dataitem_relation EDR
     ON EED.EntityID = EDR.EntityID WHERE EDR.entity_dataitemtype = 3
     RIGHT OUTER JOIN entity E ON E.EntityID = EED.EntityID
     GROUP BY E.EntityID

OTHER TIPS

Maybe there are fields named EntityID in both tables - omid.entity_epoch_data and omid.entity_dataitem_relation ?

Your first query had only one take in the subquery to look for entityid, but the second query has two tables in the subquery.

Also, you are referencing table e1 inside a subquery before e1 is defined.

give the tables a specific alias name and that error will go away.. because you have two columns that are not distinguished, so when you try to use one it doesn't know which table to pull it from

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top