Question : SQL Query syntax

Hi - I have a main select statement that is fairly straightforward - Get all reports that have been submitted - I need to include another select statement into the query to filter the results further and I am unsure how to get it working.  The query pulls all the reports, there is another table that stores all the reports that have been amended and the new id for them - I have the sub query (the answer to which comes from the related question to this question) I am including the query that i have attempted - with the sub-query in it - but it is written incorrectly and I am not sure how to alter it...maybe this is as clear as mud - please feel free to ask questions for anything I have not explained well enough...  The where clause where I specify the report_id should reflect all records and then ONLY the newest reportID that may exist within that list....
Select pc.PROV_STATE_NAME_FR,rd.Operating_entity_name,rd.YEAR,
 rd.report_type_name_fr,id.inventory_state_name_FR,id.phase_type_name_fr,id.report_id,
CASE id.in_equipment
WHEN 'false' THEN 'Liquide Dans Un Equipement'
WHEN 'true' THEN  'Liquide Pas Dans Un Equipement'
END AS InEquipment ,
SUM(id.quantity) As quantity,id.unit_fr,
SUM(id.Concentration_value) AS Concentration,id.concentration_range
FROM vwInventory_Denormalized AS id JOIN vwReportLegalEntity_Denormalized AS rd ON id.report_id = rd.report_id
JOIN Company_location cl ON cl.Legal_entity_id = rd.legal_entity_id JOIN Location_address la ON la.location_id = cl.location_id JOIN ProvState_code pc ON pc.Prov_state_code = la.Prov_state_code
JOIN EC_Region r ON r.EC_Region_id = pc.Region_id
WHERE id.quantity > 0
AND rd.REPORT_TYPE_ID = 2
AND rd.Year = 2009
AND id.report_id EXISTS IN( select * from report_amendment a
where a.report_new_id = (select max(report_new_id)
from report_amendment
where report_root_id = a.report_root_id))
GROUP BY pc.PROV_STATE_NAME_FR,
rd.Operating_entity_name,
rd.YEAR,
rd.report_type_name_fr,
id.inventory_state_name_fr,
id.phase_type_name_fr,
id.reportid,
id.unit_fr,
id.concentration_range,
id.in_equipment,
rd.report_type_name_fr
Related Solutions: SQL Query

Answer : SQL Query syntax

sorry for got to name the "table vwInventory_Denormalized " as X
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
Select pc.PROV_STATE_NAME_FR,rd.Operating_entity_name,rd.YEAR,
 rd.report_type_name_fr,id.inventory_state_name_FR,id.phase_type_name_fr,id.report_id,
id.InEquipment ,
SUM(id.quantity) As quantity,id.unit_fr,
SUM(id.Concentration_value) AS Concentration,id.concentration_range

FROM (select x.*
            ,'Liquide '+case in_equipment when 'true' then 'Pas ' else '' end
            +'Dans un Equipment' as InEquipment  
        from vwInventory_Denormalized as X
      )AS id 
Inner JOIN vwReportLegalEntity_Denormalized AS rd 
   ON id.report_id = rd.report_id
Inner JOIN Company_location cl 
   ON cl.Legal_entity_id = rd.legal_entity_id 
Inner JOIN Location_address la 
   ON la.location_id = cl.location_id 
Inner JOIN ProvState_code pc 
   ON pc.Prov_state_code = la.Prov_state_code
Inner JOIN EC_Region r 
   ON r.EC_Region_id = pc.Region_id

 Left outer join report_amendment as a1
   on a1.report_old_id=id.report_id

 WHERE id.quantity > 0
  AND rd.REPORT_TYPE_ID = 2
  AND rd.Year = 2009
  AND a1.report_old_id is null
      

GROUP BY pc.PROV_STATE_NAME_FR,
rd.Operating_entity_name,
rd.YEAR,
rd.report_type_name_fr,
id.inventory_state_name_fr,
id.phase_type_name_fr,
id.reportid,
id.unit_fr,
id.concentration_range,
id.inequipment,
rd.report_type_name_fr
Random Solutions  
 
programming4us programming4us