Can anyone help me with this Query:
I’m sort of new to writing sql queries so I’m sure that’s the reason for my oversight.

For some reason when I go from the first query pasted below (which I did not originate) to the second query pasted below (I’ll point out the added lines). I get the following message

Msg 156, Level 15, State 1, Line 76
Incorrect syntax near the keyword 'where'.
Msg 156, Level 15, State 1, Line 156
Incorrect syntax near the keyword 'where'.

In the second query I will also highlight the line numbers mentioned below

Any HELP is Greatly Appreciated



First Query:
select
c3.[_ResourceGuid]
,'Added' as [Added or Removed]
,c3.[LatestSnapshotDate] as InventoryDate
,c3.[DisplayName]
,c3.[DisplayVersion]

from
(
select
c2.[_Resourceguid]
,c2.[LatestSnapshotDate]
,c2.[NextToLastSnapshotDate]
,ih4.[Snapshotid]
,ih4.[DisplayName]
,ih4.[DisplayVersion]
from
(
select
ih3.[_ResourceGuid]
,ih3.[LatestSnapshotDate]
,c1.[NextToLastSnapshotDate]
from
(
select
[_ResourceGuid]
,max([InventoryDate]) as LatestSnapshotDate
from
InvHist_AddRemoveProgram
group by
[_ResourceGuid]
) ih3
join
(
select
ih2.[_ResourceGuid]
,max([InventoryDate]) as [NextToLastSnapshotDate]
from
InvHist_AddRemoveProgram ih1
Left Join
(
select
[_ResourceGuid]
,max([InventoryDate]) as LatestSnapshotDate
from
InvHist_AddRemoveProgram
group by
[_ResourceGuid]
) ih2
on
ih1.[_ResourceGuid]=ih2.[_ResourceGuid]
where
ih1.[InventoryDate]<>ih2.[LatestSnapshotDate]
group by
ih2.[_ResourceGuid]
) c1
on
ih3.[_ResourceGuid]=c1.[_ResourceGuid]
) c2
join
InvHist_AddRemoveProgram ih4
on
c2.[_ResourceGuid] = ih4.[_ResourceGuid] and c2.[LatestSnapshotDate]=ih4.[InventoryDate]
) c3
left join
InvHist_AddRemoveProgram ih5
on
c3.[_ResourceGuid] = ih5.[_ResourceGuid]
and c3.[NextToLastSnapshotdate]=ih5.[InventoryDate]
and c3.[DisplayName]=ih5.[DisplayName]
and c3.[DisplayVersion]=ih5.[DisplayVersion]
where
ih5.[Snapshotid] is null

UNION

select
c3.[_ResourceGuid]
,'Removed' as [Added or Removed]
,c3.[NextToLastSnapshotDate] as InventoryDate
,c3.[DisplayName]
,c3.[DisplayVersion]
from
(
select
c2.[_Resourceguid]
,c2.[LatestSnapshotDate]
,c2.[NextToLastSnapshotDate]
,ih4.[Snapshotid]
,ih4.[DisplayName]
,ih4.[DisplayVersion]
from
(
select
ih3.[_ResourceGuid]
,ih3.[LatestSnapshotDate]
,c1.[NextToLastSnapshotDate]
from
(
select
[_ResourceGuid]
,max([InventoryDate]) as LatestSnapshotDate
from
InvHist_AddRemoveProgram
group by
[_ResourceGuid]
) ih3
join
(
select
ih2.[_ResourceGuid]
,max([InventoryDate]) as [NextToLastSnapshotDate]
from
InvHist_AddRemoveProgram ih1
Left Join
(
select
[_ResourceGuid]
,max([InventoryDate]) as LatestSnapshotDate
from
InvHist_AddRemoveProgram
group by
[_ResourceGuid]
) ih2
on
ih1.[_ResourceGuid]=ih2.[_ResourceGuid]
where
ih1.[InventoryDate]<>ih2.[LatestSnapshotDate]
group by
ih2.[_ResourceGuid]
) c1
on
ih3.[_ResourceGuid]=c1.[_ResourceGuid]
) c2
join
InvHist_AddRemoveProgram ih4
on
c2.[_ResourceGuid] = ih4.[_ResourceGuid] and c2.[NextToLastSnapshotDate]=ih4.[InventoryDate]
) c3
left join
InvHist_AddRemoveProgram ih5
on
c3.[_ResourceGuid] = ih5.[_ResourceGuid]
and c3.[LatestSnapshotdate]=ih5.[InventoryDate]
and c3.[DisplayName]=ih5.[DisplayName]
and c3.[DisplayVersion]=ih5.[DisplayVersion]
where
ih5.[Snapshotid] is null

END OF FIRST QUERY


I want to change this query to include the computer name so I modify it to the following:

select
ci.[Name] THIS LINE WAS ADDED
,c3.[_ResourceGuid]
,'Added' as [Added or Removed]
,c3.[LatestSnapshotDate] as InventoryDate
,c3.[DisplayName]
,c3.[DisplayVersion]

from
[vRM_Computer_Item] ci THIS LINE WAS ADDED

Join THIS LINE WAS ADDED
(
select
c2.[_Resourceguid]
,c2.[LatestSnapshotDate]
,c2.[NextToLastSnapshotDate]
,ih4.[Snapshotid]
,ih4.[DisplayName]
,ih4.[DisplayVersion]
from
(
select
ih3.[_ResourceGuid]
,ih3.[LatestSnapshotDate]
,c1.[NextToLastSnapshotDate]
from
(
select
[_ResourceGuid]
,max([InventoryDate]) as LatestSnapshotDate
from
InvHist_AddRemoveProgram
group by
[_ResourceGuid]
) ih3
join
(
select
ih2.[_ResourceGuid]
,max([InventoryDate]) as [NextToLastSnapshotDate]
from
InvHist_AddRemoveProgram ih1
Left Join
(
select
[_ResourceGuid]
,max([InventoryDate]) as LatestSnapshotDate
from
InvHist_AddRemoveProgram
group by
[_ResourceGuid]
) ih2
on
ih1.[_ResourceGuid]=ih2.[_ResourceGuid]
where
ih1.[InventoryDate]<>ih2.[LatestSnapshotDate]
group by
ih2.[_ResourceGuid]
) c1
on
ih3.[_ResourceGuid]=c1.[_ResourceGuid]
) c2
join
InvHist_AddRemoveProgram ih4
on
c2.[_ResourceGuid] = ih4.[_ResourceGuid] and c2.[LatestSnapshotDate]=ih4.[InventoryDate]
) c3
left join
InvHist_AddRemoveProgram ih5
on
c3.[_ResourceGuid] = ih5.[_ResourceGuid]
and c3.[NextToLastSnapshotdate]=ih5.[InventoryDate]
and c3.[DisplayName]=ih5.[DisplayName]
and c3.[DisplayVersion]=ih5.[DisplayVersion]
where LINE 76

ih5.[Snapshoti] is null

UNION

select
ci.[Name] THIS LINE WAS ADDED
,c3.[_ResourceGuid]
,'Removed' as [Added or Removed]
,c3.[NextToLastSnapshotDate] as InventoryDate
,c3.[DisplayName]
,c3.[DisplayVersion]
from

[vRM_Computer_Item] ci THIS LINE WAS ADDED

Join THIS LINE WAS ADDED
(
select
c2.[_Resourceguid]
,c2.[LatestSnapshotDate]
,c2.[NextToLastSnapshotDate]
,ih4.[Snapshotid]
,ih4.[DisplayName]
,ih4.[DisplayVersion]
from
(
select
ih3.[_ResourceGuid]
,ih3.[LatestSnapshotDate]
,c1.[NextToLastSnapshotDate]
from
(
select
[_ResourceGuid]
,max([InventoryDate]) as LatestSnapshotDate
from
InvHist_AddRemoveProgram
group by
[_ResourceGuid]
) ih3
join
(
select
ih2.[_ResourceGuid]
,max([InventoryDate]) as [NextToLastSnapshotDate]
from
InvHist_AddRemoveProgram ih1
Left Join
(
select
[_ResourceGuid]
,max([InventoryDate]) as LatestSnapshotDate
from
InvHist_AddRemoveProgram
group by
[_ResourceGuid]
) ih2
on
ih1.[_ResourceGuid]=ih2.[_ResourceGuid]
where
ih1.[InventoryDate]<>ih2.[LatestSnapshotDate]
group by
ih2.[_ResourceGuid]
) c1
on
ih3.[_ResourceGuid]=c1.[_ResourceGuid]
) c2
join
InvHist_AddRemoveProgram ih4
on
c2.[_ResourceGuid] = ih4.[_ResourceGuid] and c2.[NextToLastSnapshotDate]=ih4.[InventoryDate]
) c3
left join
InvHist_AddRemoveProgram ih5
on
c3.[_ResourceGuid] = ih5.[_ResourceGuid]
and c3.[LatestSnapshotdate]=ih5.[InventoryDate]
and c3.[DisplayName]=ih5.[DisplayName]
and c3.[DisplayVersion]=ih5.[DisplayVersion]
where Line 156
ih5.[Snapshotid] is null