2

我想使用 sql server pivot 生成每周时间表。

我的数据库有这些表。

        create table Students
        (
            StudentID int identity primary key,
            Name nvarchar(50)
        )
        create table Times
        (
            TimeID int identity primary key
            ,Name nvarchar(10)
        )
        create table Days
        (
            DayID int identity primary key 
            ,Name nvarchar(20)
        )
        create table TimeTable
        (
            StudentID int references Students(StudentID)
            ,TimeID int references Times(TimeID)
            ,DayID int references Days(DayID)
            ,Value nvarchar(50)
        )

        insert Times values('t1')
        insert Times values('t2')
        insert Times values('t3')



        insert Days values('sunday')
        insert Days values('monday')
        insert Days values('tuesday')
        insert Days values('wednesday')
        insert Days values('thursday')
        insert Days values('friday')
        insert Days values('saturday')

我想要列(DayID,DayName,t1,t2,t3)

我正在使用此查询,但由于在将记录插入时间表表时使用 Max(Value) 聚合函数,此查询显示列 t1、t2 和 t3 的一个值。但是 t2 和 t3 应该为空

            SELECT      *
            FROM         (SELECT     dbo.Days.DayID, dbo.Days.Name, dbo.Times.Name AS Expr1, dbo.TimeTable.Value
            FROM         dbo.Times CROSS JOIN
                                  dbo.Days LEFT OUTER JOIN
                                  dbo.TimeTable ON dbo.Days.DayID = dbo.TimeTable.DayID) AS d_1
            PIVOT (max (Value) FOR [Expr1] 
            IN (t1, t2, t3)) AS P 

例如,在执行这些 cmds 后,所有列都会显示 asp 值。

        insert Students values('ahmad')
            insert TimeTable values(1,1,1,'asp') 
4

2 回答 2

0

您缺少从 TimeTable 到 Times 的匹配项:

 AND dbo.Times.TimeID = dbo.TimeTable.TimeID
于 2012-04-12T15:58:15.423 回答
0

你非常接近试试这个:

create table #Students
(
    StudentID int identity primary key,
    Name nvarchar(50)
)
create table #Times
(
    TimeID int identity primary key
    ,Name nvarchar(10)
)
create table #Days
(
    DayID int identity primary key 
    ,Name nvarchar(20)
)
create table #TimeTable
(
    StudentID int references #Students(StudentID)
    ,TimeID int references #Times(TimeID)
    ,DayID int references #Days(DayID)
    ,Value nvarchar(50)
)

insert #Times values('t1')
insert #Times values('t2')
insert #Times values('t3')

insert #Days values('sunday')
insert #Days values('monday')
insert #Days values('tuesday')
insert #Days values('wednesday')
insert #Days values('thursday')
insert #Days values('friday')
insert #Days values('saturday')

insert #Students values('ahmad')
insert #TimeTable values(1,1,1,'asp') 

SELECT *
FROM 
(
    SELECT     d.DayID, d.Name, t.Name AS Expr1, tt.Value
    FROM         #Times t 
    CROSS JOIN #Days d
    LEFT OUTER JOIN #TimeTable tt
        ON d.DayID = tt.DayID
        AND t.TimeID = tt.TimeID) AS d_1
PIVOT (max (Value) FOR [Expr1] 
IN (t1, t2, t3)) AS P 


drop table #Students
drop table #Times
drop table #Days
drop table #TimeTable
于 2012-04-12T16:02:35.580 回答