1

我正在尽最大努力将一周中的日子变成 X,这样我就可以在每周的时间表上标记它们 - 将它们组合在一条线上是我的症结所在。

预期输出:

AgrmntID    Description    RepairID    Su  M  Tu  W  Th  F  Sa
2           Landscaping    2                  X
3           Landscaping    2               X  X   X  X   X  X

电流输出:

AgrmntID    Description    RepairID
2           Landscaping    2

当前代码:

Select Agreements.AgrmntID, LaborCodeTypes.Description,
  AgreementSchedules.RepairID
From Agreements Inner Join
  AgreementSchedules On Agreements.AgrmntID = AgreementSchedules.AgrmntID
  Inner Join
  LaborCodeTypes On LaborCodeTypes.RepairID = AgreementSchedules.RepairID
  Inner Join
  (Select AgreementSchedules.AgrmntID, AgreementSchedules.RepairID, Case
        When DatePart(dw, AgreementSchedules.SchedDate) = 1 Then 'X'
      End As Sunday
    From AgreementSchedules
    Where AgreementSchedules.RepairID = 2
    Union
    Select AgreementSchedules.AgrmntID, AgreementSchedules.RepairID, Case
        When DatePart(dw, AgreementSchedules.SchedDate) = 2 Then 'X'
      End As Monday
    From AgreementSchedules
    Where AgreementSchedules.RepairID = 2
    Union
    Select AgreementSchedules.AgrmntID, AgreementSchedules.RepairID, Case
        When DatePart(dw, AgreementSchedules.SchedDate) = 3 Then 'X'
      End As Tuesday
    From AgreementSchedules
    Where AgreementSchedules.RepairID = 2) Sched On Sched.AgrmntID =
    Agreements.AgrmntID
Group By Agreements.AgrmntID, LaborCodeTypes.Description,
  AgreementSchedules.RepairID
Having AgreementSchedules.RepairID = 2

我有很多我失败的例子,如果这可以帮助你解决这个问题。任何提示将不胜感激 - 在此先感谢您!

4

3 回答 3

2

如果您提供CREATE TABLE脚本和示例数据,我可以对此进行审查,但首先要从头开始:

SELECT
    A.AgrmntID,
    LCT.Description,
    S.RepairID,
    Days.Sunday, Days.Monday, Days.Tuesday, Days.Wednesday, Days.Thursday, Days.Friday, Days.Saturday
FROM
    Agreements AS A
    INNER JOIN AgreementSchedules AS S ON A.AgrmntID = S.AgrmntID
    INNER JOIN LaborCodeTypes AS LCT ON S.RepairID = LCT.RepairID
    INNER JOIN
        (
        SELECT
            AgrmntID, RepairID, "1" AS Sunday, "2" AS Monday, "3" AS Tuesday, "4" AS Wednesday, "5" AS Thursday, "6" AS Friday, "7" AS Saturday
        FROM
            (
            SELECT DISTINCT
                AgrmntD, RepairID, DATEPART(WEEKDAY, SchedDate) AS DayOfWeek
            FROM
                AgreementSchedules
            ) AS X
        PIVOT
            (MIN(AgrmntID) FOR DayOfWeek IN ("1", "2", "3", "4", "5", "6", "7")) AS Y
        ) AS Days ON A.AgrmntID = Days.AgrmntID AND S.RepairID = Days.RepairID
于 2013-09-03T23:35:10.690 回答
1
SELECT  id as ID,
        descr as DESCRIPTION,
        REPAIRID AS repairId,
        CASE WHEN "1" IS NOT NULL 
        THEN 'X'
        ELSE ''
        END AS Sunday,
        CASE WHEN "2" IS NOT NULL 
        THEN 'X'
        ELSE ''
        END AS Monday,
        CASE WHEN "3" IS NOT NULL 
        THEN 'X'
        ELSE ''
        END AS Tuesday,
        CASE WHEN "4" IS NOT NULL 
        THEN 'X'
        ELSE ''
        END AS Wednesday,
        CASE WHEN "5" IS NOT NULL
        THEN 'X'
        ELSE ''
        END AS Thursday,
        CASE WHEN "6" IS NOT NULL
        THEN 'X'
        ELSE ''
        END AS Friday,
        CASE WHEN "7" IS NOT NULL
        THEN 'X'
        ELSE ''
        END AS Saturday
FROM (
SELECT id "Junk",
       id, 
       descr,
       repairid,
       DATEPART(dw,someday) "Day"
--etc to Saturday
FROM myTestTable) tbl1
PIVOT 
(
    AVG(Junk)
    FOR Day IN ([1],[2],[3],[4],[5],[6],[7])
) as pvt

更新 编辑:这将工作。

于 2013-09-03T23:37:12.830 回答
0

快速浏览一下,您的选择语句中不包括您的星期几。除非您明确告诉他们,否则您的联接不会返回任何内容。

于 2013-09-03T23:32:14.000 回答