需要在text数据类型上更新最后一项

时间:2014-07-01 22:05:38

标签: sql sql-server

我需要在dbo.notes的文本列'Notes'上更新最后一行。由于我正在为此报告加入多个表,因此我在末尾添加了一个子查询,通过按输入日期排序选择,仅提取列notes.com的前1项。但我不断收到错误说我不能使用=运算符来获取子查询以获取文本数据类型(注释)。请帮助提供更好的解决方案。

这是我的查询

SELECT
JobsReport.AppointmentNumber,     
JobsReport.MarketDescription,
Hc.Manager_4_ID AS RegionalDirectorID,
Hc.Manager_4_Name AS RegionalDirector,
Hc.Primary_Manager_ID AS SecurityManagerID,
Hc.Primary_Manager_Name AS SecurityManagerName,
Dim.OSV_Supervisor_Employee_Number AS SupervisorID,
Dim.OSV_Supervisor_Full_Name AS Supervisor,
JobsReport.Technician, 
JobsReport.TechnicianId, 
JobsReport.RescheduleRate, 
JobsReport.ServiceType, 
JobsReport.PrimaryService, 
JobsReport.AppointmentDuration, 
JobsReport.Status, 
JobsReport.Substatus, 
Notes.Comments,
JobsReport.AppointmentDate,
JobsReport.CustomerFirstName +' '+JobsReport.CustomerLastName AS CustomerName, 
JobsReport.CustomerAddressOne, 
JobsReport.CustomerCity, 
JobsReport.CustomerState, 
JobsReport.CustomerZip
FROM AT_ATT_Data_Integration.dbo.JobsReport
LEFT JOIN AT_ATT_Data_Integration.dbo.Appointments
ON JobsReport.AppointmentNumber = Appointments. AppointmentNumber
LEFT JOIN AT_ATT_Data_Integration.dbo.Notes
ON Appointments.AppointmentId=Notes.AppointmentId
LEFT JOIN AT_BI_DataStore.dbo.ATT_Employees AS Employee
ON JobsReport.TechnicianId=Employee.[Tech ID]
LEFT JOIN AT_BI_DataStore.dbo.Dim_Employees AS Dim
ON Employee.OracleID=Dim.OSV_Employee_Number
LEFT JOIN [AT_BI_DataStore].[dbo].[Hierarchy_Honeycomb] AS Hc
ON Dim.OSV_BR_Location=Hc.R12_Organization_Name
WHERE (convert(date,JobsReport.AppointmentDate) = convert(date,GETDATE()))
AND Notes.Comments = (
    SELECT TOP 1
    FROM AT_ATT_Data_Integration.dbo.Notes
    ORDER BY Notes.Date DESC)

2 个答案:

答案 0 :(得分:1)

  1. 帕特里克是对的;你的最后WHERE子句没有选择任何东西。 “SELECT TOP 1”需要一栏。
  2. 您不能使用=来比较TEXT列。您需要在=符号的两侧使用CONVERT(VARCHAR(MAX),Notes.Comments)才能生效。
  3. 因此,您希望包含该约会的最新备注以及其他数据。为此,您可以将子查询作为ON子句的一部分放在该表的连接位置。因此,您的完整查询将变为:
  4. 
        SELECT
        JobsReport.AppointmentNumber,     
        JobsReport.MarketDescription,
        Hc.Manager_4_ID AS RegionalDirectorID,
        Hc.Manager_4_Name AS RegionalDirector,
        Hc.Primary_Manager_ID AS SecurityManagerID,
        Hc.Primary_Manager_Name AS SecurityManagerName,
        Dim.OSV_Supervisor_Employee_Number AS SupervisorID,
        Dim.OSV_Supervisor_Full_Name AS Supervisor,
        JobsReport.Technician, 
        JobsReport.TechnicianId, 
        JobsReport.RescheduleRate, 
        JobsReport.ServiceType, 
        JobsReport.PrimaryService, 
        JobsReport.AppointmentDuration, 
        JobsReport.Status, 
        JobsReport.Substatus, 
        JobsReport.AppointmentDate,
        JobsReport.CustomerFirstName +' '+JobsReport.CustomerLastName AS CustomerName, 
        JobsReport.CustomerAddressOne, 
        JobsReport.CustomerCity, 
        JobsReport.CustomerState, 
        JobsReport.CustomerZip
        FROM AT_ATT_Data_Integration.dbo.JobsReport
        LEFT JOIN AT_ATT_Data_Integration.dbo.Appointments
        ON JobsReport.AppointmentNumber = Appointments. AppointmentNumber
        LEFT JOIN AT_ATT_Data_Integration.dbo.Notes
        ON Appointments.AppointmentId=Notes.AppointmentId 
           AND Notes.Date = (
              SELECT TOP 1 Notes.Date 
              FROM AT_ATT_Data_Integration.dbo.Notes AS N2
              WHERE N2.AppointmentId = Notes.AppointmentId
              ORDER BY Notes.Date DESC)
        LEFT JOIN AT_BI_DataStore.dbo.ATT_Employees AS Employee
        ON JobsReport.TechnicianId=Employee.[Tech ID]
        LEFT JOIN AT_BI_DataStore.dbo.Dim_Employees AS Dim
        ON Employee.OracleID=Dim.OSV_Employee_Number
        LEFT JOIN [AT_BI_DataStore].[dbo].[Hierarchy_Honeycomb] AS Hc
        ON Dim.OSV_BR_Location=Hc.R12_Organization_Name
        WHERE (convert(date,JobsReport.AppointmentDate) = convert(date,GETDATE()))
    

答案 1 :(得分:0)

您没有在最终的子查询中提供列名。例如,如果AT_ATT_Data_Integration.dbo.Notes上的列被称为“OldNote”:

AND Notes.Comments = ( SELECT TOP 1 OldNote FROM AT_ATT_Data_Integration.dbo.Notes ORDER BY Notes.Date DESC)

一旦你纠正了它,它应该有效。