我有这个SQL Server表:
我需要将每个类别的流组合在一起,并向用户显示过去几周的概述,如图所示:
此数据表询问Web服务,该服务又查询数据库。
复选标记的颜色取决于值CodState :如果还有一个状态等于4('拒绝'),则复选标记为红色。否则,是绿色。
请注意,Date字段DetailsStream对应于某一天。
问题是具有不同日期范围的查询的制定(在图片中:最近五周,从周一到周五)。
修改
根据建议,我的表架构:
-- Category:
CREATE TABLE [dbo].[StreamCategory](
[Cod] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
CONSTRAINT [PK_StreamCategory] PRIMARY KEY CLUSTERED
(
[Cod] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
-- Stream
CREATE TABLE [dbo].[Stream](
[Id] [int] IDENTITY (1, 1) NOT NULL,
[Name] [varchar](100) NOT NULL,
[TypeStream] [varchar](15) NOT NULL,
[CodCategory] [int] NOT NULL,
CONSTRAINT [PK_Stream] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[Stream] WITH CHECK ADD CONSTRAINT [FK_Stream_StreamCategory] FOREIGN KEY([CodCategoria])
REFERENCES [dbo].[StreamCategory] ([Cod])
GO
ALTER TABLE [dbo].[Stream] CHECK CONSTRAINT [FK_Stream_StreamCategory]
GO
-- State Details stream
CREATE TABLE [dbo].[StateDetailsStream](
[Cod] [int] NOT NULL,
[Description] [varchar](50) NOT NULL,
CONSTRAINT [PK_DetailsStream] PRIMARY KEY CLUSTERED
(
[Cod] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
-- Details stream
CREATE TABLE [dbo].[DetailsStream](
[Id] [int] IDENTITY (1, 1) NOT NULL,
[DateStream] [datetime] NOT NULL,
[CodStateDetailsStream] [int] NOT NULL,
[IdStream] [int] NOT NULL,
CONSTRAINT [PK_DetailsStream] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[DetailsStream] WITH CHECK ADD CONSTRAINT [FK_DetailsStream_StateDetailsStream] FOREIGN KEY([CodStateDetailsStream])
REFERENCES [dbo].[StateDetailsStream] ([Cod])
GO
ALTER TABLE [dbo].[DetailsStream] CHECK CONSTRAINT [FK_DetailsStream_StateDetailsStream]
GO
ALTER TABLE [dbo].[DetailsStream] WITH CHECK ADD CONSTRAINT [FK_DetailsStream_Stream] FOREIGN KEY([IdStream])
REFERENCES [dbo].[Stream] ([Id])
GO
ALTER TABLE [dbo].[DetailsStream] CHECK CONSTRAINT [FK_DetailsStream_Stream]
GO
插入:
-- StreamCategory
INSERT INTO [myDb].[dbo].[StreamCategory]
([Cod]
,[Name])
VALUES
(1,'Category A'),
(2,'Category B'),
(3,'Category C')
GO
-- Stream
INSERT INTO [myDb].[dbo].[Stream]
( [CodCategory]
,[Name]
)
VALUES
-- Category A:
( 1 ,'Stream001' ), -- IdStream: 1
( 1 ,'Stream002' ), -- IdStream: 2
( 1 ,'Stream003' ), -- IdStream: 3
( 1 ,'Stream004' ),
-- Category B:
( 2 ,'Stream005' ), -- IdStream: 5
( 2 ,'Stream006' ),
( 2 ,'Stream007' ),
( 2 ,'Stream008' ),
-- Category C:
( 3 ,'Stream009' ), -- IdStream: 9
( 3 ,'Stream010' ), -- IdStream: 10
( 3 ,'Stream011' ),
( 3 ,'Stream012' )
GO
-- StateDetailsStream
INSERT INTO [myDB].[dbo].[StateDetailsStream]
([Cod]
,[Description])
VALUES
(1, 'InProgress'),
(2, 'Received'),
(3, 'Ko'),
(4, 'Declined')
GO
-- DetailsStream
DECLARE
@mon as datetime = '20/06/2016'
DECLARE
@tue as datetime = @mon+1,
@wed as datetime = @mon+2,
@thu as datetime = @mon+3,
@fry as datetime = @mon+4
INSERT INTO [myDB].[dbo].[DetailsStream]
([IdStream] -- fk
,[DateStream]
,[CodStateDetailsStream]
)
VALUES
-- Category A
(1 ,@mon ,2 ), -- Stream001
(1 ,@mon ,2 ),
(1 ,@tue ,2 ),
(1 ,@tue ,2 ),
(1 ,@wed ,3 ),
(1 ,@wed ,2 ),
(1 ,@thu ,2 ),
(1 ,@thu ,2 ),
(1 ,@fry ,2 ),
(1 ,@fry ,1 ),
(1 ,@mon+7 ,1 ),
(1 ,@mon+7 ,1 ),
(1 ,@tue+7 ,3 ),
(1 ,@tue+7 ,4 ),
(1 ,@wed+7 ,2 ),
(1 ,@wed+7 ,1 ),
(1 ,@thu+7 ,2 ),
(1 ,@thu+7 ,2 ),
(1 ,@fry+7 ,4 ),
(1 ,@fry+7 ,2 ),
(2 ,@mon ,2 ), -- Stream002
(2 ,@mon ,4 ),
(2 ,@tue ,4 ),
(2 ,@tue ,2 ),
(2 ,@wed ,3 ),
(2 ,@wed ,2 ),
(2 ,@thu ,2 ),
(2 ,@thu ,2 ),
(2 ,@fry ,2 ),
(2 ,@fry ,1 ),
-- Category B
(5 ,@mon ,2 ), -- Stream005
(5 ,@tue ,2 ),
(5 ,@wed ,2 ),
(5 ,@thu ,2 ),
(5 ,@fry ,2 ),
-- Category C
(10 ,@mon ,1 ), -- Stream010
(10 ,@mon ,2 ),
(10 ,@tue ,2 ),
(10 ,@tue ,3 ),
(10 ,@fry ,2 ),
(10 ,@fry ,4 ),
(11 ,@wed ,4 ), -- Stream011
(11 ,@wed ,1 ),
(11 ,@thu ,2 ),
(11 ,@thu ,3 )
(注意:我不知道它是否有兴趣,但通常我使用这种方法:Web表格代码隐藏 - > WebService - >核心 - >存储库,与实体和DTO之间进行通信不同的水平)
答案 0 :(得分:0)
您可以查看使用日历表(这对于许多与日期相关的计算很有用......
使用此表,您可以将日期键加入相关日期,然后按日历表中的其他字段进行分组;通过它的声音,年和周。这应该可以让您轻松报告每周时间