With Vehicles As (
Select Row_Number() Over (Order By Model.[Year] Desc, Vehicle.Msrp Asc, Vehicle.Id) As RowNumber
, Make.[Name] As Style_Model_Make_Name
, Model.[Name] As Style_Model_Name
, Model.[Year] As Style_Model_Year
, Style.Trim As Style_Trim
, Style.[Name] As Style_Name
, Style.BodyType As Style_BodyType
, Vehicle.CompanyId As Company_Id
, Vehicle.Id
, Vehicle.StyleId As Style_Id
, Vehicle.[Status]
, Vehicle.Vin
, Vehicle.StockNumber
, Vehicle.ModelCode
, Vehicle.Mileage
, Vehicle.InteriorColor
, Vehicle.InteriorColorCode
, Vehicle.ExteriorColor
, Vehicle.ExteriorColorCode
, Vehicle.Certification
, Vehicle.Selling
, Vehicle.Internet
, Vehicle.Special
, Vehicle.Comment
, Vehicle.IsNew
, Vehicle.IsCertified
, Vehicle.StockDate
From dbo.Vehicle (NoLock)
Join dbo.Style (NoLock) On Style.Id = Vehicle.StyleId
Left Join dbo.Engine ON dbo.Style.Id = dbo.Engine.StyleId
Inner Join dbo.Model on dbo.Model.Id = dbo.Style.ModelId
Inner Join dbo.Make ON dbo.Model.MakeId = dbo.Make.Id ON dbo.Style.ModelId = dbo.Model.Id
Where Vehicle.CompanyId in (9)
And Vehicle._IsActive = 1
And DateDiff(day, Vehicle.StockDate, GetDate()) >= @DaysInStock
And Vehicle.Msrp Between @PriceMin And @PriceMax
And Vehicle.Mileage Between @MileageMin And @MileageMax And Convert(int, Model.[Year]) Between @YearMin And @YearMax And Vehicle.IsNew = 1 )
Select Style_Model_Make_Name
, Style_Model_Name
, Style_Model_Year
, Style_Id
, Style_Trim
, Style_Name
, Style_BodyType
, Company_Id
, Vehicle.Id
, Vehicle.Vin
, Vehicle.Mileage
, Vehicle.StockNumber
, Vehicle.ModelCode
, Vehicle.InteriorColor
, Vehicle.InteriorColorCode
, Vehicle.ExteriorColor
, Vehicle.ExteriorColorCode
, Vehicle.Certification
, Case When Vehicle.Msrp < @CallPrice Then 0 Else Vehicle.Msrp End As Msrp
, Case When Vehicle.Selling < @CallPrice Then 0 Else Vehicle.Selling End As Selling
, Vehicle.Internet
, Vehicle.Special
, Vehicle.Comment
, Vehicle.IsNew
, Vehicle.IsCertified
, Vehicle.StockDate
From Vehicles
Where RowNumber Between @StartRowIndex And (@StartRowIndex + @MaximumRows) - 1
我是sql server的新手并试图解决问题。 我正在尝试创建一个CTE,以便更容易地选择不同的列。我认为我的FROM语句有问题,但我不确定究竟是什么问题。第二个SELECT语句中的所有列都有红色sqiggles,返回错误并且“多部分标识符”“无法绑定”错误。有任何关于如何解决这个问题的想法吗?
答案 0 :(得分:2)
从主SELECT中的列名中删除Vehicle.
:
Select Style_Model_Make_Name
, Style_Model_Name
, Style_Model_Year
, Style_Id
, Style_Trim
, Style_Name
, Style_BodyType
, Company_Id
, Vehicle.Id
, Vehicle.Vin
, Vehicle.Mileage
, Vehicle.StockNumber
, Vehicle.ModelCode
, Vehicle.InteriorColor
, Vehicle.InteriorColorCode
, Vehicle.ExteriorColor
, Vehicle.ExteriorColorCode
, Vehicle.Certification
, Case When Vehicle.Msrp < @CallPrice Then 0 Else Vehicle.Msrp End As Msrp
, Case When Vehicle.Selling < @CallPrice Then 0 Else Vehicle.Selling End As Selling
, Vehicle.Internet
, Vehicle.Special
, Vehicle.Comment
, Vehicle.IsNew
, Vehicle.IsCertified
, Vehicle.StockDate
From Vehicles
Where RowNumber Between @StartRowIndex And (@StartRowIndex + @MaximumRows) - 1