0

下面的代码(以及许多类似的代码)继续返回空白。

基本上,如果 LaborCode = '01 - SC' 那么它应该按 JobSite、LaborCode 和 Schedule(确切日期)排序

如果不是 01 - SC,则应按 JobSite、LaborCode 和 DayNo(星期几)排序

Select Distinct Agreements.AgrmntID, Agreements.Description, Agreements.Status,
  JobSites.SiteName, JobSites.Address2, JobSites.City, Customers.CustName,
  Customers.CompanyName, LaborCodeTypes.RepairCode As LaborCode, Schedule = Case
    LaborCodeTypes.RepairCode
    When '01 - SC' Then Left(Convert(varchar,AgreementSchedules.SchedDate,110),
    10) Else DateName(dw, AgreementSchedules.SchedDate)
  End, Employees1.EmpName As Vendor, Employees.EmpName As [Area Manager],
  DatePart(dw, AgreementSchedules.SchedDate) As DayNo
From Agreements Inner Join
  Customers On Agreements.CustID = Customers.CustID Inner Join
  AgreementSchedules On Agreements.AgrmntID = AgreementSchedules.AgrmntID
  Inner Join
  JobSites On Agreements.CustSiteID = JobSites.CustSiteID Left Outer Join
  LaborCodeTypes On AgreementSchedules.RepairID = LaborCodeTypes.RepairID
  Left Outer Join
  Employees On AgreementSchedules.FormanEmpID = Employees.EmployeeID Left Join
  WorkOrderSchedules On WorkOrderSchedules.ScheduleID =
    AgreementSchedules.ScheduleID And AgreementSchedules.ScheduleID =
    WorkOrderSchedules.ScheduleID Left Join
  WorkOrderScheduleTechs On WorkOrderSchedules.ScheduleID =
    WorkOrderScheduleTechs.ScheduleID Left Join
  Employees Employees1 On WorkOrderScheduleTechs.EmployeeID =
    Employees1.EmployeeID
Where Agreements.Status = 2 And LaborCodeTypes.RepairCode <> 'Vendor Bill' And
  Month(AgreementSchedules.SchedDate) = Month(GetDate())
Order By Case
    When [LaborCodeTypes.RepairCode] In ('01 - SC') Then JobSites.SiteName +
    LaborCodeTypes.RepairCode + Schedule
    Else JobSites.SiteName + LaborCodeTypes.RepairCode + DayNo End

感谢您的帮助!!

4

2 回答 2

2

试试这个:

ORDER BY JobSites.SiteName, 
         LaborCodeTypes.RepairCode, 
         CASE WHEN LaborCodeTypes.RepairCode IN ('01 - SC') THEN Schedule ELSE DayNo END
于 2012-09-19T23:22:13.137 回答
1

好的,在 SQL Server 中:

CREATE PROCEDURE dbo.Agreements_GetList
AS
BEGIN
  SET NOCOUNT ON;

  SELECT DISTINCT
    a.AgrmntID, 
    a.Description, 
    a.Status,
    js.SiteName, 
    js.Address2, 
    js.City, 
    c.CustName,
    c.CompanyName, 
    LaborCode = lct.RepairCode, 
    Schedule = CASE lct.RepairCode 
      WHEN '01 - SC' THEN CONVERT(CHAR(10), aSch.SchedDate, 110)
      --------------------^^^ much better than LEFT and not specifying length
      ELSE DATENAME(WEEKDAY, aSch.SchedDate) END, 
  Vendor = e2.EmpName, 
  [Area Manager] = e1.EmpName,
  DayNo = CONVERT(CHAR(1), DATEPART(WEEKDAY, aSch.SchedDate))
  --------^^^ this convert is important
FROM dbo.Agreements AS a 
INNER JOIN dbo.Customers AS c 
  ON a.CustID = c.CustID 
INNER JOIN dbo.AgreementSchedules AS aSch 
  ON a.AgrmntID = aSch.AgrmntID
INNER JOIN dbo.JobSites AS js 
  ON a.CustSiteID = js.CustSiteID 
LEFT OUTER JOIN dbo.LaborCodeTypes AS lct 
  ON aSch.RepairID = lct.RepairID
LEFT OUTER JOIN dbo.Employees AS e1 
  ON aSch.FormanEmpID = e1.EmployeeID 
LEFT OUTER JOIN dbo.WorkOrderSchedules AS w
  ON w.ScheduleID = aSch.ScheduleID
LEFT OUTER JOIN dbo.WorkOrderScheduleTechs AS wt
  ON w.ScheduleID = wt.ScheduleID 
LEFT OUTER JOIN dbo.Employees AS e2
  ON wt.EmployeeID = e2.EmployeeID
WHERE 
  a.Status = 2 
  AND lct.RepairCode <> 'Vendor Bill' 
  AND aSch.SchedDate >= DATEADD(MONTH, 0, DATEDIFF(MONTH, 0, GETDATE()))
  AND aSch.SchedDate < DATEADD(MONTH, 1, DATEDIFF(MONTH, 0, GETDATE()))
ORDER BY 
  js.SiteName,
  lct.RepairCode,
  CASE WHEN lct.RepairCode = '01 - SC'
    THEN js.SiteName ELSE DayNo END;
END
GO

现在我完全不了解您的应用程序,因此您必须弄清楚如何从中调用存储过程,而不是嵌入 SQL。

于 2012-09-20T00:07:10.373 回答