SQL Script Help Please - Too Hard!

As you have not provided a test rig you can hardly expect tested code. Typos etc are your problem.

The query has 163 lines so I suspect you have not copied all of it.

I suspect your biggest problem with this and other queries are scalar functions. You should refactor them as inline TVFs:

Thanks Ifor. Yeah - Fair enough. If I'll work through it.

Okay, that's cool. I will have a look into this too.

Okay so I've sorted the typos. All good there.

Now I have another error - Msg 245, level 16, State 1, Line 1
Conversion failed when converting the varchar value 'PM153' to data type int.

Which bit do I need to change for that please?

which column is PM153 in and in which table?

this is really impossible to sort out without sample data like others have asked you multiple time.

Thanks Yosiaz, FROM tblparts.

For reference how would I send the sample data?

@Ifor has already shown you how to do this. but here it is again. Sample data can be provided as such.

declare @sampleParts(id int, bla varchar(50), ducky float)

insert into @sampleParts
select 1, 'Tofu', 10.33 union
select 1,'bacon', 0.32

with real columns, data types and near to real data for all of the tables in question.