我需要显示每周的单用户时间表,如时间表,
场景:教师在一周内被分配到多个批次(例如:BBA,数学和上午为第1小时和第2小时)& (同一日期的MBA,数学,上午3小时和4周)说(2015年6月30日)。我的一行gridview将存储为1和2行作为商店2等等....... ..
我的表定义:
CREATE TABLE [dbo].[test] (
[datedif] NVARCHAR (50) NOT NULL,
[hour] INT NULL,
[subject] NVARCHAR (MAX) NULL,
[faculty] NVARCHAR (MAX) NULL,
[attendence] BIT NULL,
[dayweek] NVARCHAR (50) NULL,
[weekmonth] NVARCHAR (MAX) NULL,
[batch] NVARCHAR (MAX) NULL,
[section] NVARCHAR (MAX) NULL,
[session] NVARCHAR (MAX) NULL
);
表格如下:
Datefdiff | hour | subject | faulty| batch
----------+-------+----------+---------+-------+----------+--------+-------+----------+---------+-------
30-06-2015| 1| Maths | Kevin | BBA
30-06-2015| 2| Science | Amal | MBA
30-06-2015|3 | chemistry|Jaya |BBA
30-06-2015|4 | chemistry|Jaya |BBA
30-06-2015|5 | chemistry|Jaya |BBA
31-06-2015 |1| science | Amal |BBA
31-06-2015 |2| Maths | kevin |BBA
31-06-2015 |3| Science | Amal |BBA
31-06-2015 |4 | chemistry|Jaya |BBA
31-06-2015 |5| science | Amal |BBA
仅为教师提供预期的输出:Amal
Datefdiff |hour|subject| batch |hour|subject | batch |faculty|hour | subject | batch | hour | subject | batch| hour | subject | batch |
----------+-------+----------+-------+-------+----------+-------+-------+----------+
30-06-2015| 1 | Maths| BBA| 2| Science | MBA | 3| Science | BBA| 4| chemistry| BBA | 5 |Physics |MBA
31-06-2015| 1 | Maths| BBA| 2| Science | MBA | 3| Science | BBA| 4| chemistry| BBA | 5 |Physics |MBA
答案 0 :(得分:1)
这可能是您的表格设计的起点:
declare @tbFaculty table (
FacultyID int --identity(1,1) primary key
, Name varchar(50)
)
insert into @tbFaculty ( FacultyID, Name )
values ( 1, 'Kevin' )
, ( 2, 'Amal' )
declare @tbBatch table(
BatchID int --identity(1,1) primary key
, Name char(3)
)
insert into @tbBatch ( BatchID, Name )
values ( 1, 'BBA' )
, ( 2, 'MBA' )
declare @tbClass table (
[Hour] tinyint
, [Subject] nvarchar (128)
, [FacultyID] int
, [Attendence] bit
, [BatchID] char(3)
, [ClassDate] date
)
insert into @tbClass ( [Hour], [Subject], FacultyID, Attendence, BatchID, ClassDate )
values ( 1, 'Maths', 1, 1, 1, '2015-06-30' )
, ( 2, 'Maths', 1, 1, 1, '2015-06-30' )
, ( 3, 'Science', 2, 1, 1, '2015-06-30' )
, ( 1, 'Science', 2, 1, 2, '2015-06-30' )
, ( 2, 'Science', 2, 1, 2, '2015-06-30' )
, ( 3, 'Maths', 1, 1, 2, '2015-06-30' )
select
cl.ClassDate
, cl.[Hour]
, cl.[Subject]
, ba.Name as BatchName
, fa.Name as FacultyName
from
@tbClass cl
inner join @tbBatch ba on ba.BatchID = cl.BatchID
inner join @tbFaculty fa on fa.FacultyID = cl.FacultyID
where
fa.Name = 'Amal'
您也可以规范化主题。
答案 1 :(得分:1)
我只想添加一个单独的答案。试试这个:
grep -P ^GOR[A-Z]+C[0-9]?
答案 2 :(得分:0)
以下是如何规范化您的数据,这将使查询变得更加简单。
使用您的表格定义,我插入了您的数据
INSERT INTO test(Datedif,hour1,subject1,faculty1,hour2,subject2,faculty2,hour3,subject3,faculty3,batch)
VALUES ('30-06-2015',1,'Maths','Kevin',1,'Maths','Kevin',1,'Science','Amal','BBA'),
('30-06-2015',1,'Science','Amal',1,'Science','Amal',1,'Maths','Kevin','MBA');
然后我使用较窄的数据类型为您的数据创建了一个新的规范化结构。它有助于保持数据清洁以及不浪费存储空间,这意味着查询速度更快,因为它们不必处理如此多的数据。
CREATE TABLE new_test
(
dt DATE NULL,
hr TINYINT NULL, --holds values between 0 to 255
subj VARCHAR(100) NULL, --plenty big enough. No need for NVARCHAR unless you are using Unicode characters
faculty VARCHAR(100) NULL,
attendance BIT NULL,
dayweek TINYINT NULL,
weekmonth TINYINT NULL,
section VARCHAR(100) NULL, --not sure what this is
sess VARCHAR(100) NULL, --not sure what this is
batch CHAR(3) NULL --looks like there are three character codes
);
这是我规范化数据的地方。我不确定你是否有出勤2,3,4等...如果你在实际的表中,那么你应该修改我的代码。
注意:我计算了从日期开始的每周和每周。我做了最好的猜测,如果它们不正确,那么随意调整它们!
WITH norm_data
AS
(
SELECT datedif,hour1,subject1,faculty1,attendence1,dayweek,weekmonth,section,[session],batch FROM test
UNION ALL
SELECT datedif,hour2,subject2,faculty2,attendence1,dayweek,weekmonth,section,[session],batch FROM test
UNION ALL
SELECT datedif,hour3,subject3,faculty3,attendence1,dayweek,weekmonth,section,[session],batch FROM test
UNION ALL
SELECT datedif,hour4,subject4,faculty4,attendence1,dayweek,weekmonth,section,[session],batch FROM test
UNION ALL
SELECT datedif,hour5,subject5,faculty5,attendence1,dayweek,weekmonth,section,[session],batch FROM test
)
INSERT INTO new_test
SELECT PARSE(datedif AS DATE USING 'de-DE') AS [datedif],
hour1,
subject1,
faculty1,
attendence1,
DATEPART(WEEKDAY,PARSE(datedif AS DATE USING 'de-DE')) AS dayweek,
datepart(day, datediff(day, 0, PARSE(datedif AS DATE USING 'de-DE'))/7 * 7)/7 + 1 AS weekmonth,
section,
[session],
batch
FROM norm_data
现在让我们来看看你的新表
SELECT *
FROM new_test
如果它是正确的,那么这里是如何重命名表
EXEC SP_rename @objname = 'test', --if you don't want to drop the old table
@newname = 'test_old'
EXEC SP_rename @objname = 'new_test', --now give the new table the actual name
@newname = 'test'
SELECT *
FROM test