I am trying to make a massive query (Probably a bad idea, but I don't know another way to do this) that pulls data from two separate databases that are on the same server. Here is what I have
Select Distinct PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment As ItemCode,
concat(
max(case when PartQuality.Quality_Seq = '1' Then Quality.DSC end),' ',
max(Case When PartQuality.Quality_Seq = '2' then Quality.DSC End),' ',
max(Case When PartQuality.Quality_Seq = '3' Then Quality.DSC End),' ',
max(case when PartQuality.Quality_Seq = '4' Then Quality.DSC end),' ',
max(Case When PartQuality.Quality_Seq = '5' then Quality.DSC End),' ',
max(Case When PartQuality.Quality_Seq = '6' Then Quality.DSC End),' ',
max(case when PartQuality.Quality_Seq = '7' Then Quality.DSC end),' ',
max(Case When PartQuality.Quality_Seq = '8' then Quality.DSC End),' ',
max(Case When PartQuality.Quality_Seq = '9' Then Quality.DSC End)) As ProdDesc,
InventoryDB.dbo.ITEM.QtyOnHand
From SelectDB.dbo.PartDetail
Join InventoryDB.dbo.ITEM on ITEM.Itemcode = PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment
Join SelectDB.dbo.PartQuality on PartQuality.Item_ID = PartDetail.Item_ID
Join SelectDB.dbo.Quality on Quality.QualityCode = PartQuality.QualityCode
Join SelectDB.dbo.Part on Part.Item_ID = PartDetails.Item_ID
Join SelectDB.dbo.PartPrefix on PartPrefix.PrefixCode = Part.PrefixCode
Join SelectDB.dbo.ProductPart on ProductPart.Item_ID = PartDetail.Item_ID
Join SelectDB.dbo.Product on Product.Product_ID = ProductPart.Product_ID
Join SelectDB.dbo.ProductMod on PrdouctMod.ProductMod_ID = Product.ProductMod_ID
Join SelectDB.dbo.MakeModel on MakeModel.MakeModel_ID = Product.MakeModel_ID
Join SelectDB.dbo.ProductStyle on ProductStyle.Style_ID = ProductStyle.Style_ID
Join SelectDB.dbo.ProductMake on ProductMake.Make_ID = MakeModel.Make_ID
Where Product.ModelYear = @YEAR
And ProductMake.Name = @MAKE
And MakeModel.Name = @MODEL
And ProductStyle.DSC = @STYLE
And ProductMod.DSC = @MOD
And PartPrefix.DSC = @LOC
Group By PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment, ITEM.QtyOnHand
It currently workings but when I added in the following lines to obtain the QtyOnHand field from the InventoryDB, the query went from completing instantly to taking 20-30+ seconds to run.
Select Distinct . . . . InventoryDB.dbo.ITEM.QtyOnHand
From . . . .
Join InventoryDB.dbo.ITEM on ITEM.Itemcode = PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment
. . ..
Where . . . .
Group By . . . . ITEM.QtyOnHand
Since the query runs fine without these lines I figured it must be something with these lines. If I had to guess, it would be the Join InventoryDB.dbo.ITEM on ITEM.Itemcode = PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment due to having to concatenate those three fields together and then search for matching value, but I'm not sure of anyway around that.