1

我需要制作每周视图计划日历。

使用该查询:

SELECT  
      Num
    , name
    , DATEPART(dw, [DateExpe]) AS DAYOFWEEK
    , DATEPART(WEEK, [DateExpe]) AS WeekNumber
    , ROW_NUMBER() OVER (PARTITION BY WeekNumber, DAYOFWEEK ORDER BY Num) AS WeekLineNumber 
FROM Expeditions

我有一些价值观,例如:

Num  Name    DayOfWeek  WeekNumber WeekLineNumber 
1    Test1   1          1          1
2    Test2   1          1          2
3    Test3   2          1          1
4    Test4   3          2          1

编辑我添加了由 ROW_NUMBER() 生成的 WeekLineNumber

我想把它放在一个类似日历的表格中(在 ms 访问中),我的目标是得到这个结果

WeekNumber Num1  Name1   Num2 Name2   Num3 Name3   Num4 Name4  (...)
1          1     Test1   3    Test3
1          2     Test2
2                                     4    Test4

Num1 和 Name1 是一周的第一天。Num2 和 Name2 为第二个。(..)

OMHO有2个困难:

  • 每条线上都没有计算数据(我们每周可以有不止一条线)
  • 我不能每条记录只有一行(这很容易)。例如,我不想要这样的结果:
WeekNumber Num1 Name1 Num2 Name2 Num3 Name3 Num4 Name4 (...)
1 1 测试1
1 1 3 测试3
1 2 测试2
2 4 测试4

我需要在第一行有Test1和Test3。

4

4 回答 4

2

试试这个——

架构 -

DECLARE @SQL NVARCHAR(MAX)

IF OBJECT_ID (N'tempdb.dbo.#temp') IS NOT NULL
   DROP TABLE #temp

CREATE TABLE #temp
(
      Num INT
    , Name VARCHAR(10)
    , [DayOfWeek] INT
    , WeekNumber INT
    , WeekLineNumber INT
)

INSERT INTO #temp (Num, Name, [DayOfWeek], WeekNumber, WeekLineNumber)
VALUES 
    (1, 'Test1', 1, 1, 1),
    (2, 'Test2', 1, 1, 2),
    (3, 'Test3', 2, 1, 1),
    (4, 'Test4', 3, 2, 1)

使用 PIVOT 查询 -

DECLARE 
      @sel_cols VARCHAR(MAX)
    , @cols VARCHAR(MAX)

SELECT @sel_cols = STUFF((
    SELECT DISTINCT CHAR(13) + ', [Num' + CAST([DayOfWeek] AS VARCHAR(5)) + '] = ISNULL(MAX(CASE WHEN [DayOfWeek] = ' + CAST([DayOfWeek] AS VARCHAR(5)) + ' THEN CAST([Num' + CAST([DayOfWeek] AS VARCHAR(5)) + '] AS CHAR(2)) END), '''')'
        + CHAR(13) + ', [Name' + CAST([DayOfWeek] AS VARCHAR(5)) + '] = ISNULL(MAX(CASE WHEN [DayOfWeek] = ' + CAST([DayOfWeek] AS VARCHAR(5)) + ' THEN [Name' + CAST([DayOfWeek] AS VARCHAR(5)) + '] END), '''')'
    FROM #temp
    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, '')

PRINT @sel_cols

SELECT @cols = STUFF((
    SELECT DISTINCT ', [_' + CAST([DayOfWeek] AS VARCHAR(5)) + ']'
    FROM #temp
    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, '')

SELECT @SQL = '
SELECT WeekNumber, ' + @sel_cols + '
FROM (
    SELECT 
          *
        , id = ''Name'' + CAST([DayOfWeek] AS VARCHAR(5))
        , id2 = ''Num'' + CAST([DayOfWeek] AS VARCHAR(5))
    FROM #temp t
) t3 
PIVOT (
    MAX(Name) 
    FOR id IN (' + REPLACE(@cols, '_', 'Name') + ')
) pivot1
PIVOT (
    MAX(Num) 
    FOR id2 IN (' + REPLACE(@cols, '_', 'Num') + ')
) pivot2 
GROUP BY WeekNumber, WeekLineNumber 
ORDER BY WeekNumber
'

EXEC sys.sp_executesql @SQL

使用枚举查询 -

DECLARE 
      @WeekStart INT
    , @WeekEnd INT

SELECT 
      @WeekStart = 1
    , @WeekEnd = 7

SELECT @SQL = STUFF((
    SELECT CHAR(13) + ', [Num' + num + '] = ISNULL(MAX(CASE WHEN [DayOfWeek] = ' + num + ' THEN CAST([Num] AS CHAR(2)) END), '''')'
        + CHAR(13) + ', [Name' + num + '] = ISNULL(MAX(CASE WHEN [DayOfWeek] = ' + num + ' THEN [Name] END), '''')'
    FROM (
        SELECT num = CAST(sv.number AS CHAR(1))
        FROM [master].dbo.spt_values sv
        WHERE sv.[type] = 'p'
            AND sv.number BETWEEN @WeekStart AND @WeekEnd
    ) sv
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, 'SELECT
  [WeekNumber] 
,') + ' FROM #temp 
GROUP BY WeekNumber, WeekLineNumber 
ORDER BY WeekNumber'

EXEC sys.sp_executesql @SQL   

输出 -

1. 枢轴

WeekNumber  Num1 Name1      Num2 Name2      Num3 Name3
----------- ---- ---------- ---- ---------- ---- ----------
1           1    Test1      3    Test3           
1           2    Test2                           
2                                           4    Test4        

2.枚举

WeekNumber  Num1 Name1      Num2 Name2      Num3 Name3      Num4 Name4      Num5 Name5      Num6 Name6      Num7 Name7
----------- ---- ---------- ---- ---------- ---- ---------- ---- ---------- ---- ---------- ---- ---------- ---- ----------
1           1    Test1      3    Test3                                                                           
1           2    Test2                                                                                           
2                                           4    Test4                                                           
于 2013-05-29T09:42:26.153 回答
1

使用您提供的数据集并将其加载到临时表#Test 中,下面是以所需格式提供结果的查询:

SELECT  [WeekNumber]
        ,CASE WHEN [DayOfWeek]=1 THEN 1      ELSE NULL END AS [Num1]
        ,Case WHEN [DayOfWeek]=1 THEN [Name] ELSE NULL END AS [Name1]
        ,CASE WHEN [DayOfWeek]=2 THEN 2      ELSE NULL END AS [Num2]
        ,Case WHEN [DayOfWeek]=2 THEN [Name] ELSE NULL END AS [Name2]
        ,CASE WHEN [DayOfWeek]=3 THEN 3      ELSE NULL END AS [Num3]
        ,Case WHEN [DayOfWeek]=3 THEN [Name] ELSE NULL END AS [Name3]
        ,CASE WHEN [DayOfWeek]=4 THEN 4      ELSE NULL END AS [Num4]
        ,Case WHEN [DayOfWeek]=4 THEN [Name] ELSE NULL END AS [Name4]
        ,CASE WHEN [DayOfWeek]=5 THEN 5      ELSE NULL END AS [Num5]
        ,Case WHEN [DayOfWeek]=5 THEN [Name] ELSE NULL END AS [Name5]
        ,CASE WHEN [DayOfWeek]=6 THEN 6      ELSE NULL END AS [Num6]
        ,Case WHEN [DayOfWeek]=6 THEN [Name] ELSE NULL END AS [Name6]
        ,CASE WHEN [DayOfWeek]=7 THEN 7      ELSE NULL END AS [Num7]
        ,Case WHEN [DayOfWeek]=7 THEN [Name] ELSE NULL END AS [Name7]
from #Test

如果您不想看到 NULL,请使用以下代码: Nulls repl;aced with BLANKS:

Select  [WeekNumber]
        ,CASE WHEN [DayOfWeek]=1 THEN cast(1 as Char(2))     ELSE '' END AS [Num1]
        ,Case WHEN [DayOfWeek]=1 THEN [Name] ELSE '' END AS [Name1]
        ,CASE WHEN [DayOfWeek]=2 THEN cast(2 as Char(2))         ELSE '' END AS [Num2]
        ,Case WHEN [DayOfWeek]=2 THEN [Name] ELSE '' END AS [Name2]
        ,CASE WHEN [DayOfWeek]=3 THEN cast(3 as Char(2))         ELSE '' END AS [Num3]
        ,Case WHEN [DayOfWeek]=3 THEN [Name] ELSE '' END AS [Name3]
        ,CASE WHEN [DayOfWeek]=4 THEN cast(4 as Char(2))         ELSE '' END AS [Num4]
        ,Case WHEN [DayOfWeek]=4 THEN [Name] ELSE '' END AS [Name4]
        ,CASE WHEN [DayOfWeek]=5 THEN cast(5 as Char(2))         ELSE '' END AS [Num5]
        ,Case WHEN [DayOfWeek]=5 THEN [Name] ELSE '' END AS [Name5]
        ,CASE WHEN [DayOfWeek]=6 THEN cast(6 as Char(2))         ELSE '' END AS [Num6]
        ,Case WHEN [DayOfWeek]=6 THEN [Name] ELSE '' END AS [Name6]
        ,CASE WHEN [DayOfWeek]=7 THEN cast(7 as Char(2))         ELSE '' END AS [Num7]
        ,Case WHEN [DayOfWeek]=7 THEN [Name] ELSE '' END AS [Name7]
from #Test

输出:

WeekNumber  Num1 Name1 Num2 Name2 Num3 Name3 Num4 Name4 Num5 Name5 Num6 Name6 Num7 Name7
----------- ---- ----- ---- ----- ---- ----- ---- ----- ---- ----- ---- ----- ---- -----
1           1    Test2                                                             
1                       2    Test3                                                  
2                                  3    Test4                                       
1           1    Test1                                                             

(4 row(s) affected)
于 2013-05-29T09:36:55.580 回答
1

我想我找到了解决方案。首先,创建表(感谢devart)

IF OBJECT_ID (N'tempdb.dbo.#temp') 不为空
   删除表#temp

创建表#temp
(
      整数
    , 名称 VARCHAR(10)
    , [DayOfWeek] 整数
    , 周数 INT
    , WeekLineNumber INT
)

插入 #temp(编号、名称、[DayOfWeek]、WeekNumber、WeekLineNumber)
价值观
    (1, 'Test1', 1, 1, 1)

插入 #temp(编号、名称、[DayOfWeek]、WeekNumber、WeekLineNumber)
价值观
    (2, 'Test2', 1, 1, 2)

插入 #temp(编号、名称、[DayOfWeek]、WeekNumber、WeekLineNumber)
价值观
    (3, 'Test3', 2, 1, 1)

插入 #temp(编号、名称、[DayOfWeek]、WeekNumber、WeekLineNumber)
价值观
    (4, 'Test4', 3, 2, 1)

然后查询它:

选择
      周数
      ,周线号
    , MAX(CASE WHEN DayOfWeek = 1 THEN [Num] ELSE NULL END) AS Num1
    , MAX(CASE WHEN DayOfWeek = 1 THEN [Name] ELSE NULL END) AS Name1
    , MAX(CASE WHEN DayOfWeek = 2 THEN [Num] ELSE NULL END) AS Num2
    , MAX(CASE WHEN DayOfWeek = 2 THEN [Name] ELSE NULL END) AS Name2
    , MAX(CASE WHEN DayOfWeek = 3 THEN [Num] ELSE NULL END) AS Num3
    , MAX(CASE WHEN DayOfWeek = 3 THEN [Name] ELSE NULL END) AS Name3
    , MAX(CASE WHEN DayOfWeek = 4 THEN [Num] ELSE NULL END) AS Num4
    , MAX(CASE WHEN DayOfWeek = 4 THEN [Name] ELSE NULL END) AS Name4
    , MAX(CASE WHEN DayOfWeek = 5 THEN [Num] ELSE NULL END) AS Num5
    , MAX(CASE WHEN DayOfWeek = 5 THEN [Name] ELSE NULL END) AS Name5
    , MAX(CASE WHEN DayOfWeek = 6 THEN [Num] ELSE NULL END) AS Num6
    , MAX(CASE WHEN DayOfWeek = 6 THEN [Name] ELSE NULL END) AS Name6
    , MAX(CASE WHEN DayOfWeek = 7 THEN [Num] ELSE NULL END) AS Num7
    , MAX(CASE WHEN DayOfWeek = 7 THEN [Name] ELSE NULL END) AS Name7
  来自#temp
GROUP BY WeekNumber, WeekLineNumber  

结果:

WeekNumber WeekLineNumber Num1 Name1 Num2 Name2 Num3 Name3 Num4 Name4 Num5 Name5 Num6 Name6 Num7 Name7
1 1 1 测试1 3 测试3
1 2 2 测试2
2 1 4 测试4

于 2013-05-29T10:13:55.950 回答
0

您正在尝试做的事情相当于交叉制表或枢轴。

如果您可以导入或链接到原始表格(非交叉制表)形式的数据,则可以直接在 Access 中进行交叉制表。查看访问交叉表查询和相应的查询向导。

于 2013-05-29T10:58:03.990 回答