0

I have the following table:

enter image description here

My task is: for each event selected item , report the event id along with the total unit count for all items for that event.

I tried the following:

 SELECT S.ITEMNO,S.EVENTID, S.ROOMID, T.TOTAL                          
 FROM SELECTEDITEM S JOIN                                              
                    (SELECT EVENTID, SUM(UNIT_COUNT) AS TOTAL          
                    FROM SELECTEDITEM                                  
                    GROUP BY EVENTID) AS T                             
                    ON S.ITEMNO=T.ITEMNO;                              
---------+---------+---------+---------+---------+---------+---------+---------
DSNT408I SQLCODE = -206, ERROR:  T.ITEMNO IS NOT VALID IN THE CONTEXT WHERE IT 
         IS USED                                                               

Why is it not working? Can I join an existing table with one that has been just generated?

4

2 回答 2

1
SELECT S.ITEMNO,S.EVENTID, SUM(UNIT_COUNT) as total
FROM SELECTEDITEM S
GROUP BY S.ITEMNO,S.EVENTID
于 2013-06-05T07:41:54.313 回答
1

Are you sure you didn't want

SELECT S.ITEMNO,S.EVENTID, S.ROOMID, T.TOTAL                          
  FROM SELECTEDITEM S 
  JOIN (SELECT EVENTID, SUM(UNIT_COUNT) AS TOTAL          
          FROM SELECTEDITEM                                  
         GROUP BY EVENTID) AS T                             
    ON S.EVENTID=T.EVENTID;

The problem is that your subselect does not have an t.ITEMNO column, so the ON clause wouldn't work.

于 2013-06-05T15:47:24.643 回答